77 lines
1.4 KiB
C++
77 lines
1.4 KiB
C++
#include "pikbdlistener.h"
|
|
|
|
|
|
bool PIKbdListener::exiting;
|
|
|
|
PIKbdListener::PIKbdListener(KBFunc slot, void * data_): PIThread() {
|
|
#ifdef WINDOWS
|
|
hIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
GetConsoleMode(hIn, &smode);
|
|
#else
|
|
struct termios term;
|
|
tcgetattr(0, &term);
|
|
sterm = term;
|
|
#endif
|
|
is_active = true;
|
|
ret_func = slot;
|
|
data = data_;
|
|
PIKbdListener::exiting = exit_enabled = false;
|
|
start();
|
|
}
|
|
|
|
|
|
void PIKbdListener::begin() {
|
|
//cout << "list begin" << endl;
|
|
#ifdef WINDOWS
|
|
hIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
GetConsoleMode(hIn, &smode);
|
|
tmode = smode;
|
|
SetConsoleMode(hIn, ENABLE_PROCESSED_INPUT);
|
|
#else
|
|
struct termios term;
|
|
tcgetattr(0, &term);
|
|
term.c_lflag &= ~(ECHO | ICANON) | NOFLSH;
|
|
tterm = term;
|
|
tcsetattr(0, TCSAFLUSH, &term);
|
|
#endif
|
|
}
|
|
|
|
|
|
void PIKbdListener::run() {
|
|
#ifdef WINDOWS
|
|
ReadConsole(hIn, &rc, 1, &ret, 0);
|
|
#else
|
|
ret = read(0, &rc, 1);
|
|
#endif
|
|
if (exit_enabled && rc == exit_key) PIKbdListener::exiting = true;
|
|
if (ret_func != 0 && ret > 0) ret_func(rc, data);
|
|
}
|
|
|
|
|
|
void PIKbdListener::end() {
|
|
//cout << "list end" << endl;
|
|
#ifdef WINDOWS
|
|
SetConsoleMode(hIn, smode);
|
|
#else
|
|
tcsetattr(0, TCSANOW, &sterm);
|
|
#endif
|
|
}
|
|
|
|
|
|
void PIKbdListener::setActive(bool yes) {
|
|
is_active = yes;
|
|
if (is_active) {
|
|
#ifdef WINDOWS
|
|
SetConsoleMode(hIn, tmode);
|
|
#else
|
|
tcsetattr(0, TCSANOW, &tterm);
|
|
#endif
|
|
} else {
|
|
#ifdef WINDOWS
|
|
SetConsoleMode(hIn, smode);
|
|
#else
|
|
tcsetattr(0, TCSANOW, &sterm);
|
|
#endif
|
|
}
|
|
}
|