#pragma once #include #include #include //#define HOOK_X11 #if defined(HOOK_X11) #include #endif enum struct KeyboardHookType { X11, RAW_INPUT, SYSTEM_HOOK }; class KeyboardHook { #ifdef HOOK_WIN32_LL friend LRESULT CALLBACK _keyboard_hook_callback(int, WPARAM, LPARAM); friend LRESULT CALLBACK _mouse_hook_callback(int, WPARAM, LPARAM); #endif public: typedef unsigned int KeyID; enum struct KeyType { 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& /* event */)> callback_event_t; KeyboardHook(KeyboardHookType); virtual ~KeyboardHook(); [[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(); void trigger_key_event(const enum KeyEvent::type&, const std::string& /* key */); callback_event_t callback_event; protected: const KeyboardHookType type_; #if 0 #if defined(HOOK_X11) typedef int KeyID; Display* display = nullptr; Window window_root = 0; Window window_focused = 0; int focus_revert; long end_id = 0; #elseif defined(HOOK_WIN32_LL) typedef UINT KeyID; HHOOK keyboad_hook_id{nullptr}; bool keyboard_hook_callback(int, WPARAM, LPARAM); #ifdef USE_MOUSE_HOOK HHOOK mouse_hook_id{nullptr}; bool mouse_hook_callback(int, WPARAM, LPARAM); #endif #endif #endif std::map map_key; std::map map_special; bool _attached = false; };