2019-10-26 01:51:40 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <thread>
|
|
|
|
#include <map>
|
|
|
|
|
2020-05-01 17:37:18 +02:00
|
|
|
//#define HOOK_X11
|
|
|
|
|
|
|
|
#if defined(HOOK_X11)
|
2019-10-26 01:51:40 +02:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#endif
|
2020-05-01 17:37:18 +02:00
|
|
|
|
|
|
|
enum struct KeyboardHookType {
|
|
|
|
X11,
|
|
|
|
|
|
|
|
RAW_INPUT,
|
|
|
|
SYSTEM_HOOK
|
|
|
|
};
|
|
|
|
|
2019-10-26 01:51:40 +02:00
|
|
|
class KeyboardHook {
|
2020-05-01 17:37:18 +02:00
|
|
|
#ifdef HOOK_WIN32_LL
|
2019-10-29 18:11:35 +01:00
|
|
|
friend LRESULT CALLBACK _keyboard_hook_callback(int, WPARAM, LPARAM);
|
|
|
|
friend LRESULT CALLBACK _mouse_hook_callback(int, WPARAM, LPARAM);
|
2019-10-26 01:51:40 +02:00
|
|
|
#endif
|
|
|
|
public:
|
2020-05-01 17:37:18 +02:00
|
|
|
typedef unsigned int KeyID;
|
|
|
|
|
|
|
|
enum struct KeyType {
|
|
|
|
KEY_UNKNOWN,
|
|
|
|
|
|
|
|
KEY_NORMAL,
|
|
|
|
KEY_SHIFT,
|
|
|
|
KEY_ALT,
|
|
|
|
KEY_WIN,
|
|
|
|
KEY_CTRL
|
2019-10-26 01:51:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-05-01 17:37:18 +02:00
|
|
|
KeyboardHook(KeyboardHookType);
|
2019-10-26 01:51:40 +02:00
|
|
|
virtual ~KeyboardHook();
|
|
|
|
|
2020-05-01 17:37:18 +02:00
|
|
|
[[nodiscard]] inline KeyboardHookType type() const { return this->type_; }
|
|
|
|
[[nodiscard]] virtual bool keytype_supported() const = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] virtual bool attach();
|
|
|
|
[[nodiscard]] inline bool attached() const { return this->_attached; }
|
|
|
|
virtual void detach();
|
2019-10-26 01:51:40 +02:00
|
|
|
|
2020-04-25 12:48:44 +02:00
|
|
|
void trigger_key_event(const enum KeyEvent::type&, const std::string& /* key */);
|
2019-10-26 01:51:40 +02:00
|
|
|
callback_event_t callback_event;
|
2020-05-01 17:37:18 +02:00
|
|
|
protected:
|
|
|
|
const KeyboardHookType type_;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#if defined(HOOK_X11)
|
2019-10-26 01:51:40 +02:00
|
|
|
typedef int KeyID;
|
|
|
|
Display* display = nullptr;
|
|
|
|
Window window_root = 0;
|
|
|
|
Window window_focused = 0;
|
|
|
|
int focus_revert;
|
|
|
|
|
|
|
|
long end_id = 0;
|
2020-05-01 17:37:18 +02:00
|
|
|
#elseif defined(HOOK_WIN32_LL)
|
2019-10-26 01:51:40 +02:00
|
|
|
typedef UINT KeyID;
|
2019-10-29 18:11:35 +01:00
|
|
|
HHOOK keyboad_hook_id{nullptr};
|
|
|
|
bool keyboard_hook_callback(int, WPARAM, LPARAM);
|
2020-05-01 17:37:18 +02:00
|
|
|
|
|
|
|
#ifdef USE_MOUSE_HOOK
|
|
|
|
HHOOK mouse_hook_id{nullptr};
|
2019-10-29 18:11:35 +01:00
|
|
|
bool mouse_hook_callback(int, WPARAM, LPARAM);
|
2020-05-01 17:37:18 +02:00
|
|
|
#endif
|
2019-10-29 18:11:35 +01:00
|
|
|
#endif
|
2020-05-01 17:37:18 +02:00
|
|
|
#endif
|
2019-10-26 01:51:40 +02:00
|
|
|
|
|
|
|
std::map<KeyID, bool> map_key;
|
2020-05-01 17:37:18 +02:00
|
|
|
std::map<KeyType, KeyID> map_special;
|
2019-10-26 01:51:40 +02:00
|
|
|
|
|
|
|
bool _attached = false;
|
|
|
|
};
|