forked from hackndev/0verkill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winkbd.c
240 lines (227 loc) · 6.57 KB
/
winkbd.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "cfg.h"
#include "kbd.h"
BYTE keyboard[256];
DWORD controlStates[256];
DWORD lastControlState;
HANDLE hMyConsoleInput;
/* DWORD idProcess; */
void kbd_init(void) {
/* GetWindowThreadProcessId(GetForegroundWindow(), &idProcess); */
hMyConsoleInput=GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hMyConsoleInput, ENABLE_WINDOW_INPUT);
memset(keyboard, 0, sizeof(keyboard));
memset(controlStates, 0, sizeof(controlStates));
lastControlState=0;
kbd_update();
}
void kbd_close(void) {
}
#ifdef CLIENT
void sigwinch_handler(int signum);
#endif
int kbd_update(void) {
INPUT_RECORD ir[50];
DWORD rd=0;
int chg=0;
if ( WaitForSingleObject(hMyConsoleInput, 0)!=WAIT_TIMEOUT ) {
if ( ReadConsoleInput(hMyConsoleInput, ir, 50, &rd) ) {
DWORD i;
for(i=0; i<rd; i++) {
if ( ir[i].EventType==KEY_EVENT ) {
DWORD vkc=ir[i].Event.KeyEvent.wVirtualKeyCode;
if ( ir[i].Event.KeyEvent.bKeyDown ) {
if ( !(keyboard[vkc]&1) )
keyboard[vkc]|=2;
keyboard[vkc]|=1;
lastControlState=controlStates[vkc]=ir[i].Event.KeyEvent.dwControlKeyState;
}
else {
keyboard[vkc]&=~1;
lastControlState=ir[i].Event.KeyEvent.dwControlKeyState;
}
}
else if ( ir[i].EventType==WINDOW_BUFFER_SIZE_EVENT ) {
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), ir[i].Event.WindowBufferSizeEvent.dwSize);
#ifdef CLIENT
sigwinch_handler(0);
#endif
}
};
/* process ext. keys according to lastControlState */
if ( lastControlState&RIGHT_CTRL_PRESSED ) {
if ( !(keyboard[VK_RCONTROL]&1) )
keyboard[VK_RCONTROL]|=2;
keyboard[VK_RCONTROL]|=1;
}
else
keyboard[VK_RCONTROL]&=~1;
if ( lastControlState&LEFT_CTRL_PRESSED ) {
if ( !(keyboard[VK_LCONTROL]&1) )
keyboard[VK_LCONTROL]|=2;
keyboard[VK_LCONTROL]|=1;
}
else
keyboard[VK_LCONTROL]&=~1;
if ( lastControlState&RIGHT_ALT_PRESSED ) {
if ( !(keyboard[VK_RMENU]&1) )
keyboard[VK_RMENU]|=2;
keyboard[VK_RMENU]|=1;
}
else
keyboard[VK_RMENU]&=~1;
if ( lastControlState&LEFT_ALT_PRESSED ) {
if ( !(keyboard[VK_LMENU]&1) )
keyboard[VK_LMENU]|=2;
keyboard[VK_LMENU]|=1;
}
else
keyboard[VK_LMENU]&=~1;
}
};
return chg;
}
SHORT IntToVKey(int k) {
if ( k>255 ) {
SHORT vKey;
switch( k ) {
case K_ESCAPE: vKey=VK_ESCAPE; break;
case K_BACKSPACE: vKey=VK_BACK; break;
case K_TAB: vKey=VK_TAB; break;
case K_ENTER: vKey=VK_RETURN; break;
case K_RIGHT_SHIFT: vKey=VK_RSHIFT; break;
case K_LEFT_SHIFT: vKey=VK_LSHIFT; break;
case K_RIGHT_CTRL: vKey=VK_RCONTROL; break;
case K_LEFT_CTRL: vKey=VK_LCONTROL; break;
case K_LEFT_ALT: vKey=VK_LMENU; break;
case K_RIGHT_ALT: vKey=VK_RMENU; break;
case K_NUM_ASTERISK: vKey=VK_MULTIPLY; break;
case K_CAPS_LOCK: vKey=VK_CAPITAL; break;
case K_NUM_LOCK: vKey=VK_NUMLOCK; break;
case K_SCROLL_LOCK: vKey=VK_SCROLL; break;
case K_PAUSE: vKey=VK_PAUSE; break;
case K_SYSRQ: vKey=VK_SNAPSHOT; break;
case K_F1: vKey=VK_F1; break;
case K_F2: vKey=VK_F2; break;
case K_F3: vKey=VK_F3; break;
case K_F4: vKey=VK_F4; break;
case K_F5: vKey=VK_F5; break;
case K_F6: vKey=VK_F6; break;
case K_F7: vKey=VK_F7; break;
case K_F8: vKey=VK_F8; break;
case K_F9: vKey=VK_F9; break;
case K_F10: vKey=VK_F10; break;
case K_F11: vKey=VK_F11; break;
case K_F12: vKey=VK_F12; break;
case K_NUM1: vKey=VK_NUMPAD1; break;
case K_NUM2: vKey=VK_NUMPAD2; break;
case K_NUM3: vKey=VK_NUMPAD3; break;
case K_NUM4: vKey=VK_NUMPAD4; break;
case K_NUM5: vKey=VK_NUMPAD5; break;
case K_NUM6: vKey=VK_NUMPAD6; break;
case K_NUM7: vKey=VK_NUMPAD7; break;
case K_NUM8: vKey=VK_NUMPAD8; break;
case K_NUM9: vKey=VK_NUMPAD9; break;
case K_NUM0: vKey=VK_NUMPAD0; break;
case K_NUM_MINUS: vKey=VK_SUBTRACT; break;
case K_NUM_PLUS: vKey=VK_ADD; break;
case K_NUM_DOT: vKey=VK_DECIMAL; break;
case K_NUM_ENTER: vKey=VK_RETURN; break;
case K_NUM_SLASH: vKey=VK_DIVIDE; break;
case K_LEFT: vKey=VK_LEFT; break;
case K_RIGHT: vKey=VK_RIGHT; break;
case K_UP: vKey=VK_UP; break;
case K_DOWN: vKey=VK_DOWN; break;
case K_HOME: vKey=VK_HOME; break;
case K_END: vKey=VK_END; break;
case K_INSERT: vKey=VK_INSERT; break;
case K_DELETE: vKey=VK_DELETE; break;
case K_PGUP: vKey=VK_PRIOR; break;
case K_PGDOWN: vKey=VK_NEXT; break;
default: vKey=-1;
}
return vKey;
}
else
return VkKeyScanEx((unsigned char)(k&0xFF), GetKeyboardLayout(0));
}
int kbd_is_pressed(int k) {
/* return GetKeyState(IntToVKey(k))&0x8000; */
SHORT key=IntToVKey(k);
int n;
if ( key==-1 )
return 0;
if (( n=(keyboard[key&0xFF]&1) )&&( k>=32 )&&( k<=127 )) {
if ( key&0x100 ) { /* shift must be down */
if ( !(controlStates[key&0xFF]&SHIFT_PRESSED) )
n=0;
}
else { /* shift must be up */
if ( controlStates[key&0xFF]&SHIFT_PRESSED )
n=0;
}
if ( key&0x200 ) { /* ctrl must be down */
if (!(( controlStates[key&0xFF]&RIGHT_CTRL_PRESSED )||
( controlStates[key&0xFF]&LEFT_CTRL_PRESSED )))
n=0;
}
else { /* ctrl must be up */
if (( controlStates[key&0xFF]&RIGHT_CTRL_PRESSED )||
( controlStates[key&0xFF]&LEFT_CTRL_PRESSED ))
n=0;
}
if ( key&0x400 ) { /* alt must be down */
if (!(( controlStates[key&0xFF]&RIGHT_ALT_PRESSED )||
( controlStates[key&0xFF]&LEFT_ALT_PRESSED )))
n=0;
}
else { /* alt must be up */
if (( controlStates[key&0xFF]&RIGHT_ALT_PRESSED )||
( controlStates[key&0xFF]&LEFT_ALT_PRESSED ))
n=0;
}
};
return n;
}
int kbd_was_pressed(int k) {
/* return GetKeyState(IntToVKey(k))&0x8000; */
SHORT key=IntToVKey(k);
int n;
if ( key==-1 )
return 0;
if (( n=(keyboard[key&0xFF]&2) )&&( k>=32 )&&( k<=127 )) {
if ( key&0x100 ) { /* shift must be down */
if ( !(controlStates[key&0xFF]&SHIFT_PRESSED) )
n=0;
}
else { /* shift must be up */
if ( controlStates[key&0xFF]&SHIFT_PRESSED )
n=0;
}
if ( key&0x200 ) { /* ctrl must be down */
if (!(( controlStates[key&0xFF]&RIGHT_CTRL_PRESSED )||
( controlStates[key&0xFF]&LEFT_CTRL_PRESSED )))
n=0;
}
else { /* ctrl must be up */
if (( controlStates[key&0xFF]&RIGHT_CTRL_PRESSED )||
( controlStates[key&0xFF]&LEFT_CTRL_PRESSED ))
n=0;
}
if ( key&0x400 ) { /* alt must be down */
if (!(( controlStates[key&0xFF]&RIGHT_ALT_PRESSED )||
( controlStates[key&0xFF]&LEFT_ALT_PRESSED )))
n=0;
}
else { /* alt must be up */
if (( controlStates[key&0xFF]&RIGHT_ALT_PRESSED )||
( controlStates[key&0xFF]&LEFT_ALT_PRESSED ))
n=0;
}
};
if ( n )
keyboard[key&0xFF]&=~2;
return n;
}
void kbd_wait_for_key(void) {
WaitForSingleObject(hMyConsoleInput, INFINITE);
}