/* PIP - Platform Independent Primitives Keyboard grabber for console Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #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 SetConsoleMode(hIn, ENABLE_PROCESSED_INPUT); GetConsoleMode(hIn, &tmode); #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; return; } 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 } }