40 lines
728 B
C++
40 lines
728 B
C++
#include "pikbdlistener.h"
|
|
|
|
|
|
PIKbdListener::PIKbdListener(KBFunc slot, void * data_): PIThread() {
|
|
ret_func = slot;
|
|
data = data_;
|
|
#ifdef __WIN32__
|
|
hIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
GetConsoleMode(hIn, &smode);
|
|
SetConsoleMode(hIn, ENABLE_PROCESSED_INPUT);
|
|
#else
|
|
struct termios term;
|
|
tcgetattr(0, &term);
|
|
sterm = term;
|
|
term.c_lflag &= ~(ECHO | ICANON) | NOFLSH;
|
|
tcsetattr(0, TCSAFLUSH, &term);
|
|
#endif
|
|
start();
|
|
}
|
|
|
|
|
|
PIKbdListener::~PIKbdListener() {
|
|
#ifdef __WIN32__
|
|
SetConsoleMode(hIn, smode);
|
|
#else
|
|
tcsetattr(0, TCSANOW, &sterm);
|
|
#endif
|
|
}
|
|
|
|
|
|
void PIKbdListener::run() {
|
|
#ifdef __WIN32__
|
|
ReadConsole(hIn, &rc, 1, &ret, 0);
|
|
#else
|
|
ret = read(0, &rc, 1);
|
|
#endif
|
|
if (ret_func != 0 && ret > 0) ret_func(rc, data);
|
|
}
|
|
|