-
Notifications
You must be signed in to change notification settings - Fork 83
/
scan-windows.c
76 lines (53 loc) · 1.5 KB
/
scan-windows.c
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
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include "buckle.h"
void open_console();
LRESULT CALLBACK handle_kbh(int nCode, WPARAM wParam, LPARAM lParam);
static HHOOK kbh = NULL;
static int state[256] = { 0 };
int scan(int verbose)
{
HINSTANCE hInst = GetModuleHandle(NULL);
kbh = SetWindowsHookEx(WH_KEYBOARD_LL, handle_kbh, hInst, 0);
MSG msg;
while(GetMessage(&msg, (HWND) NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK handle_kbh(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *ev = (KBDLLHOOKSTRUCT *)lParam;
printd("vkCode=%d scanCode=%d flags=%d time=%d",
(int)ev->vkCode, (int)ev->scanCode, (int)ev->flags, (int)ev->time);
int updown = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);
int code = ev->scanCode;
if(code < 256) {
if(state[code] != updown) {
play(code, updown);
state[code] = updown;
}
}
return CallNextHookEx(kbh, nCode, wParam, lParam);
}
void open_console()
{
int hConHandle;
INT_PTR lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
AllocConsole();
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 500;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
lStdHandle = (INT_PTR)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
*stderr = *fp;
setvbuf(fp, NULL, _IONBF, 0 );
}