77 lines
1.3 KiB
C
77 lines
1.3 KiB
C
|
#pragma once
|
||
|
|
||
|
#include <functional>
|
||
|
#include <thread>
|
||
|
#include <map>
|
||
|
|
||
|
#ifdef HAVE_X11
|
||
|
#include <X11/Xlib.h>
|
||
|
#elif defined(WIN32)
|
||
|
#include <Windows.h>
|
||
|
#endif
|
||
|
class KeyboardHook {
|
||
|
#if defined(WIN32)
|
||
|
friend LRESULT CALLBACK keyboard_hook_callback(int, WPARAM, LPARAM);
|
||
|
#endif
|
||
|
public:
|
||
|
struct KeyType {
|
||
|
enum value {
|
||
|
KEY_UNKNOWN,
|
||
|
|
||
|
KEY_NORMAL,
|
||
|
KEY_SHIFT,
|
||
|
KEY_ALT,
|
||
|
KEY_WIN,
|
||
|
KEY_CTRL
|
||
|
};
|
||
|
};
|
||
|
|
||
|
struct KeyEvent {
|
||
|
enum type {
|
||
|
PRESS,
|
||
|
RELEASE,
|
||
|
TYPE
|
||
|
};
|
||
|
|
||
|
type type;
|
||
|
std::string key;
|
||
|
std::string code;
|
||
|
|
||
|
bool key_ctrl;
|
||
|
bool key_windows;
|
||
|
bool key_shift;
|
||
|
bool key_alt;
|
||
|
};
|
||
|
typedef std::function<void(const std::shared_ptr<KeyEvent>& /* event */)> callback_event_t;
|
||
|
|
||
|
KeyboardHook();
|
||
|
virtual ~KeyboardHook();
|
||
|
|
||
|
bool attach();
|
||
|
inline bool attached() { return this->_attached; }
|
||
|
void detach();
|
||
|
|
||
|
callback_event_t callback_event;
|
||
|
private:
|
||
|
#ifdef HAVE_X11
|
||
|
typedef int KeyID;
|
||
|
Display* display = nullptr;
|
||
|
Window window_root = 0;
|
||
|
Window window_focused = 0;
|
||
|
int focus_revert;
|
||
|
|
||
|
long end_id = 0;
|
||
|
#elif defined(WIN32)
|
||
|
typedef UINT KeyID;
|
||
|
HHOOK hook_id = nullptr;
|
||
|
bool _hook_callback(int, WPARAM, LPARAM);
|
||
|
#endif
|
||
|
|
||
|
std::map<KeyID, bool> map_key;
|
||
|
std::map<KeyID, KeyID> map_special;
|
||
|
|
||
|
bool _attached = false;
|
||
|
bool active = false;
|
||
|
std::thread poll_thread;
|
||
|
void poll_events();
|
||
|
};
|