2019-10-26 01:51:40 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <thread>
|
|
|
|
#include <map>
|
|
|
|
|
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-06-13 17:21:29 +02:00
|
|
|
explicit 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_;
|
|
|
|
|
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;
|
|
|
|
};
|