-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetch_test.d
48 lines (39 loc) · 1005 Bytes
/
getch_test.d
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
import win32.windows;
import std.stdio;
void main()
{
int ch;
HANDLE hConin = GetStdHandle(STD_INPUT_HANDLE);
if( hConin == INVALID_HANDLE_VALUE ){
writefln("Invalid hConIn");
}
DWORD dwPrevMode;
if( !GetConsoleMode(hConin, &dwPrevMode) ){
writefln("Invalid GetConsoleMode");
}
DWORD dwRead, ks;
INPUT_RECORD ir;
int vk;
BOOL b;
SetConsoleMode(hConin, 0); /* set raw mode */
for(;;) {
version(BUILD_FOR_WCHAR){
b = ReadConsoleInputW(hConin, &ir, 1, &dwRead);
}else{
b = ReadConsoleInputA(hConin, &ir, 1, &dwRead);
}
if (b && dwRead > 0) {
if (ir.EventType == KEY_EVENT && ir.KeyEvent.bKeyDown)
break;
}
}
SetConsoleMode(hConin, dwPrevMode);
version(BUILD_FOR_WCHAR){
ch = ir.KeyEvent.UnicodeChar;
}else{
ch = cast(ubyte)(ir.KeyEvent.AsciiChar);
}
vk = cast(int)(ir.KeyEvent.wVirtualKeyCode);
ks = ir.KeyEvent.dwControlKeyState;
writefln("input = ch:%s(%02X), vk:%02X, ks:%02X", cast(char)ch, ch, vk, ks);
}