-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyboard.c
62 lines (47 loc) · 1.38 KB
/
keyboard.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
#include <windows.h>
#include <stdbool.h>
#include <winioctl.h>
#include <assert.h>
#include <stdint.h>
#include <signal.h>
#include "keyboard.h"
#define IOCTL_KEYBOARD_SET_INDICATORS CTL_CODE(FILE_DEVICE_KEYBOARD, 0x0002, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_KEYBOARD_QUERY_TYPEMATIC CTL_CODE(FILE_DEVICE_KEYBOARD, 0x0008, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_KEYBOARD_QUERY_INDICATORS CTL_CODE(FILE_DEVICE_KEYBOARD, 0x0010, METHOD_BUFFERED, FILE_ANY_ACCESS)
static HANDLE kbd;
void OpenKeyboardDevice()
{
if(!DefineDosDevice(DDD_RAW_TARGET_PATH, "Kbd000000",
"\\Device\\KeyboardClass0"))
{
assert(false);
}
kbd = CreateFile("\\\\.\\Kbd000000", GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
assert(kbd);
}
void CloseKeyboardDevice()
{
DefineDosDevice(DDD_REMOVE_DEFINITION, "Kbd000000", NULL);
CloseHandle(kbd);
}
int toggle_led(bool toggle, int led)
{
uint32_t input = 0, output = 0;
DWORD len;
if(!DeviceIoControl(kbd, IOCTL_KEYBOARD_QUERY_INDICATORS,
&input, sizeof(input),
&output, sizeof(output),
&len, NULL))
return GetLastError();
input = output;
input &= ~(led << 16);
if(toggle)
input |= led << 16;
if(!DeviceIoControl(kbd, IOCTL_KEYBOARD_SET_INDICATORS,
&input, sizeof(input),
NULL, 0,
&len, NULL))
return GetLastError();
return 0;
}