-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardListenerWin.cpp
171 lines (145 loc) · 5.06 KB
/
KeyboardListenerWin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifdef Q_OS_WIN
#include "KeyboardListenerWin.h"
#include <sstream>
// For debug purpose only
static void debugNotify(const char* message) {
std::stringstream os;
os << std::this_thread::get_id();
qDebug() << message << " thread::id() = " << os.str().c_str();
}
CKeyboardListenerWin::CKeyboardListenerWin(std::promise<CAnyKeyboardKiller> killerPromise) {
createMessageWindow(std::move(killerPromise));
provideHookToListener();
registerKeyboard();
debugNotify("CKLW");
}
CKeyboardListenerWin::~CKeyboardListenerWin() {
unregisterKeyboard();
}
int CKeyboardListenerWin::exec() {
CKeyboardListenerWin::CMessageStatus Status;
MSG CurrentMessage;
while ((Status = getMessage(&CurrentMessage)) != Quit) {
if (Status == Error)
handleError(&CurrentMessage);
else
dispatchMessage(&CurrentMessage);
}
return exitCode(&CurrentMessage);
}
CKeyboardListenerWin::CMessageStatus CKeyboardListenerWin::getMessage(MSG* CurrentMessage) {
return ::GetMessage(CurrentMessage, MessageWindow_, 0, 0);
}
void CKeyboardListenerWin::dispatchMessage(MSG* CurrentMessage) {
::TranslateMessage(CurrentMessage);
::DispatchMessage(CurrentMessage);
}
void CKeyboardListenerWin::handleError(MSG* CurrentMessage) {
(void)CurrentMessage;
DWORD ErrorCode = GetLastError();
std::string ErrorMessage = "KeyboardListener error with code = ";
ErrorMessage += std::to_string(ErrorCode);
throw std::runtime_error(ErrorMessage.c_str());
}
void CKeyboardListenerWin::stopListening() {
::PostMessage(MessageWindow_, WM_STOP_LISTENING, 0, 0);
}
void CKeyboardListenerWin::createMessageWindow(std::promise<CAnyKeyboardKiller> killerPromise) {
WNDCLASS wc = getWndClass();
if (!RegisterClass(&wc)) {
killerPromise.set_value(CKiller(MessageWindow_));
throw std::runtime_error("Cannot register KeyboardListenerWndClass");
}
MessageWindow_ = createWindow(wc);
if (!MessageWindow_) {
killerPromise.set_value(CKiller(MessageWindow_));
throw std::runtime_error("Cannot Create KeyboardListenerWndClass");
}
killerPromise.set_value(CKiller(MessageWindow_));
}
void CKeyboardListenerWin::provideHookToListener() {
::SetWindowLongPtr(MessageWindow_, GWLP_USERDATA, LONG_PTR(this));
}
void CKeyboardListenerWin::registerKeyboard() {
RAWINPUTDEVICE LowLevelKeyboard;
LowLevelKeyboard.usUsagePage = 0x01;
LowLevelKeyboard.usUsage = 0x06;
LowLevelKeyboard.dwFlags = RIDEV_INPUTSINK;
LowLevelKeyboard.hwndTarget = MessageWindow_;
if (RegisterRawInputDevices(&LowLevelKeyboard,
1,
sizeof(LowLevelKeyboard))
== FALSE)
throw std::runtime_error("Cannot register RAWINPUT");
}
void CKeyboardListenerWin::unregisterKeyboard() {
RAWINPUTDEVICE LowLevelKeyboard;
LowLevelKeyboard.usUsagePage = 0x01;
LowLevelKeyboard.usUsage = 0x06;
LowLevelKeyboard.dwFlags = RIDEV_REMOVE;
LowLevelKeyboard.hwndTarget = nullptr;
if (RegisterRawInputDevices(&LowLevelKeyboard,
1,
sizeof(LowLevelKeyboard))
== FALSE)
qDebug() << "Cannot unregister RAWINPUT";
// This is a bad idea to throw here since it is used in the destructor
// and may throw in a stack unwinding process
//throw std::runtime_error("Cannot unregister RAWINPUT");
}
void CKeyboardListenerWin::HandleRawInput(LPARAM lParam) {
(void)lParam;
// Translate the lParam to system independent representation
// and send it to the required object
qDebug() << "Handle lParam";
emit signalKeyboardMessage("Message");
}
int CKeyboardListenerWin::exitCode(MSG* CurrentMessage) const {
return static_cast<int>(CurrentMessage->wParam);
}
CKeyboardListenerWin* CKeyboardListenerWin::getKeyboardListener(HWND hwnd) {
return reinterpret_cast<CKeyboardListenerWin*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA));
}
LRESULT CKeyboardListenerWin::WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
switch (message) {
case WM_STOP_LISTENING:
debugNotify("WndProc");
::PostQuitMessage(0);
break;
case WM_INPUT:
debugNotify("WndProc");
qDebug() << "WM_INPUT";
getKeyboardListener(hwnd)->HandleRawInput(lparam);
break;
default:
;
}
return ::DefWindowProc(hwnd, message, wparam, lparam);
}
WNDCLASS CKeyboardListenerWin::getWndClass() {
WNDCLASS wc = {};
wc.hInstance = ::GetModuleHandle(nullptr);
wc.lpfnWndProc = WndProc;
wc.lpszClassName = kWindowClassName;
return wc;
}
HWND CKeyboardListenerWin::createWindow(const WNDCLASS& wc) {
return ::CreateWindow(kWindowClassName,
TEXT(""),
NULL,
0, 0, 0, 0,
HWND_MESSAGE,
NULL,
wc.hInstance,
NULL);
}
CKeyboardListenerWin::CKiller::CKiller(HWND MessageWindow)
: MessageWindow_(MessageWindow) {
assert(MessageWindow_ != NULL);
}
void CKeyboardListenerWin::CKiller::stopListening() const {
assert(MessageWindow_ != NULL);
if (MessageWindow_ != NULL)
::PostMessage(MessageWindow_, WM_STOP_LISTENING, 0, 0);
}
#endif // Q_OS_WIN