Files
pip/pikbdlistener.h

100 lines
2.5 KiB
C++

/*! \file pikbdlistener.h
* \brief Keyboard console input listerner
*/
/*
PIP - Platform Independent Primitives
Keyboard grabber for console
Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PIKBDLISTENER_H
#define PIKBDLISTENER_H
#include "pithread.h"
#ifndef WINDOWS
# include <termios.h>
#endif
#define WAIT_FOR_EXIT while (!PIKbdListener::exiting) msleep(1);
typedef void (*KBFunc)(char, void * );
/// events:
/// keyPressed(char key, void * data)
///
/// handlers:
/// void enableExitCapture(char key = 'Q')
/// void setActive(bool yes = true)
class PIP_EXPORT PIKbdListener: public PIThread
{
PIOBJECT(PIKbdListener)
friend class PIConsole;
public:
enum SpecialSymbol {
UpArrow = -1,
DownArrow = -2,
RightArrow = -3,
LeftArrow = -4,
CtrlUpArrow = -5,
CtrlDownArrow = -6,
CtrlRightArrow = -7,
CtrlLeftArrow = -8
};
// slot is any function format "void <func>(char, void * )"
PIKbdListener(KBFunc slot = 0, void * data = 0);
~PIKbdListener() {terminate(); end();}
void setData(void * data_) {data = data_;}
void setSlot(KBFunc slot_) {ret_func = slot_;}
EVENT_HANDLER( void, enableExitCapture) {enableExitCapture('Q');}
EVENT_HANDLER1(void, enableExitCapture, char, key) {exit_enabled = true; exit_key = key;}
void disableExitCapture() {exit_enabled = false;}
bool exitCaptured() const {return exit_enabled;}
char exitKey() const {return exit_key;}
bool isActive() {return is_active;}
EVENT_HANDLER(void, setActive) {setActive(true);}
EVENT_HANDLER1(void, setActive, bool, yes);
EVENT2(keyPressed, char, key, void * , data)
static bool exiting;
private:
void begin();
void run();
void end();
void raiseSpecial();
KBFunc ret_func;
char exit_key;
bool exit_enabled, is_active;
void * data;
#ifdef WINDOWS
DWORD ret, rc;
void * hIn;
DWORD smode, tmode;
#else
int rc;
int ret;
struct termios sterm, tterm;
#endif
};
#endif // PIKBDLISTENER_H