-
Notifications
You must be signed in to change notification settings - Fork 0
/
iqtest.cpp
executable file
·175 lines (151 loc) · 3.48 KB
/
iqtest.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
172
173
174
175
#ifndef UNICODE
#undef _UNICODE
#pragma message "WARNING:: Unicode NOT enabled"
#else
#ifndef _UNICODE
#define _UNICODE
#endif
#endif
#define OEMRESOURCE // for OCR_*
#include <windows.h>
namespace IQTest
{
const UINT MsgStartTest = WM_USER + 15;
LRESULT CALLBACK keyfilter(int code, WPARAM wparam, LPARAM lparam)
{
if (code == HC_ACTION) {
const auto p = (KBDLLHOOKSTRUCT*)lparam;
const bool alt = p->flags & LLKHF_ALTDOWN;
const bool ctrl = GetAsyncKeyState(VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);
if (p->vkCode == VK_LWIN || p->vkCode == VK_RWIN)
return 1;
if (p->vkCode == VK_TAB && alt)
return 1;
if (p->vkCode == VK_ESCAPE && (alt || ctrl))
return 1;
}
return CallNextHookEx(NULL, code, wparam, lparam);
}
class KeyBlocker
{
HHOOK _hook = NULL;
public:
explicit KeyBlocker()
: _hook(SetWindowsHookEx(WH_KEYBOARD_LL, keyfilter, GetModuleHandle(NULL), 0))
{ }
virtual ~KeyBlocker()
{
if (_hook != NULL)
UnhookWindowsHookEx(_hook);
}
};
class MouseLocker
{
class Hider
{
public:
explicit Hider() { while (ShowCursor(FALSE) >= 0); }
virtual ~Hider() { while (ShowCursor(TRUE) < 0); }
} _hider;
class Restorer
{
POINT _pos;
public:
explicit Restorer() { GetCursorPos(&_pos); }
virtual ~Restorer() { SetCursorPos(_pos.x, _pos.y); }
} _restorer;
class Clipper
{
public:
virtual ~Clipper() { ClipCursor(NULL); }
void clip()
{
RECT r { 0, 0, 1, 1 };
ClipCursor(&r);
}
} _clipper;
class Capturer
{
public:
virtual ~Capturer() { ReleaseCapture(); }
void capture(HWND hwnd) { SetCapture(hwnd); }
} _capturer;
public:
explicit MouseLocker(HWND hwnd)
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 0, 0, 0, 0);
_clipper.clip();
_capturer.capture(hwnd);
}
};
static void iq_test(HWND hwnd)
{
KeyBlocker blocker;
MouseLocker locker(hwnd);
MessageBox(hwnd, L"Do you know how to use the keyboard?", L"Hardware IQ Test",
MB_OK | MB_ICONEXCLAMATION);
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case MsgStartTest:
iq_test(hwnd);
DestroyWindow(hwnd);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
class CaptureWindow
{
static const wchar_t* ClassName;
static class WindowClass
{
public:
explicit WindowClass()
{
WNDCLASSEX wc { 0 };
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = wndproc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = ClassName;
RegisterClassEx(&wc);
}
} s_wc;
HWND _hwnd;
public:
explicit CaptureWindow()
: _hwnd(CreateWindowEx(0, ClassName, L"", WS_POPUP | WS_VISIBLE,
0, 0, 1, 1, NULL, NULL, GetModuleHandle(NULL), NULL))
{ }
virtual ~CaptureWindow()
{
if (_hwnd != NULL)
DestroyWindow(_hwnd);
}
operator HWND() const { return _hwnd; }
};
/*static*/ const wchar_t* CaptureWindow::ClassName = L"wc_011518_hw";
/*static*/ CaptureWindow::WindowClass CaptureWindow::s_wc;
static int message_loop()
{
MSG msg;
BOOL ret;
while ((ret = GetMessage(&msg, NULL, 0, 0) != 0) != 0) {
if (ret == -1)
return -1;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
} // namespace IQTest
int wmain(int argc, wchar_t* argv[])
{
using namespace IQTest;
CaptureWindow wnd;
PostMessage(wnd, MsgStartTest, 0, 0);
return message_loop();
}