TeaSpeak-Client/native/ppt/src/KeyboardHook.h

96 lines
1.9 KiB
C
Raw Normal View History

2019-10-26 01:51:40 +02:00
#pragma once
#include <functional>
#include <thread>
#include <map>
//#define HOOK_X11
#if defined(HOOK_X11)
2019-10-26 01:51:40 +02:00
#include <X11/Xlib.h>
#endif
enum struct KeyboardHookType {
X11,
RAW_INPUT,
SYSTEM_HOOK
};
2019-10-26 01:51:40 +02:00
class KeyboardHook {
#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:
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;
KeyboardHook(KeyboardHookType);
2019-10-26 01:51:40 +02:00
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();
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;
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;
#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);
#ifdef USE_MOUSE_HOOK
HHOOK mouse_hook_id{nullptr};
2019-10-29 18:11:35 +01:00
bool mouse_hook_callback(int, WPARAM, LPARAM);
#endif
2019-10-29 18:11:35 +01:00
#endif
#endif
2019-10-26 01:51:40 +02:00
std::map<KeyID, bool> map_key;
std::map<KeyType, KeyID> map_special;
2019-10-26 01:51:40 +02:00
bool _attached = false;
};