-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathkeytrans.c
165 lines (145 loc) · 3.69 KB
/
keytrans.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
/**
* PROJECT: Native Shell
* COPYRIGHT: LGPL; See LICENSE in the top level directory
* FILE: keytrans.c
* DESCRIPTION: Keyboard codes translation.
* DEVELOPERS: See CONTRIBUTORS.md in the top level directory
*/
#include "precomp.h"
typedef struct _SCANTOASCII
{
USHORT ScanCode;
UCHAR Normal;
UCHAR Shift;
} SCANTOASCII, *PSCANTOASCII;
SCANTOASCII ScanToAscii[] = {
{0x1e, 'a', 'A'},
{0x30, 'b', 'B'},
{0x2e, 'c', 'C'},
{0x20, 'd', 'D'},
{0x12, 'e', 'E'},
{0x21, 'f', 'F'},
{0x22, 'g', 'G'},
{0x23, 'h', 'H'},
{0x17, 'i', 'I'},
{0x24, 'j', 'J'},
{0x25, 'k', 'K'},
{0x26, 'l', 'L'},
{0x32, 'm', 'M'},
{0x31, 'n', 'N'},
{0x18, 'o', 'O'},
{0x19, 'p', 'P'},
{0x10, 'q', 'Q'},
{0x13, 'r', 'R'},
{0x1f, 's', 'S'},
{0x14, 't', 'T'},
{0x16, 'u', 'U'},
{0x2f, 'v', 'V'},
{0x11, 'w', 'W'},
{0x2d, 'x', 'X'},
{0x15, 'y', 'Y'},
{0x2c, 'z', 'Z'},
{0x02, '1', '!'},
{0x03, '2', '@'},
{0x04, '3', '#'},
{0x05, '4', '$'},
{0x06, '5', '%'},
{0x07, '6', '^'},
{0x08, '7', '&'},
{0x09, '8', '*'},
{0x0a, '9', '('},
{0x0b, '0', ')'},
{0x29, '\'', '~'},
{0x0c, '-', '_'},
{0x0d, '=', '+'},
{0x1a, '[', '{'},
{0x1b, ']', '}'},
{0x2b, '\\', '|'},
{0x27, ';', ':'},
{0x28, '\'', '"'},
{0x33, ',', '<'},
{0x34, '.', '>'},
{0x35, '/', '?'},
{0x4a, '-', '-'},
{0x4e, '+', '+'},
{0x37, '*', '*'},
{0x39, ' ', ' '},
{0x1c, '\r', '\r'},
{0x0e, 0x08, 0x08}, /* backspace */
{0, 0, 0}};
static void IntUpdateControlKeyState(LPDWORD State, PKEYBOARD_INPUT_DATA InputData)
{
DWORD Value = 0;
if (InputData->Flags & KEY_E1) /* Only the pause key has E1 */
return;
if (!(InputData->Flags & KEY_E0))
{
switch (InputData->MakeCode)
{
case 0x2a:
case 0x36:
Value = SHIFT_PRESSED;
break;
case 0x1d:
Value = LEFT_CTRL_PRESSED;
break;
case 0x38:
Value = LEFT_ALT_PRESSED;
break;
case 0x45:
Value = NUMLOCK_ON;
if (!(InputData->Flags & KEY_BREAK))
*State ^= Value;
return;
default:
return;
}
}
else
{
switch (InputData->MakeCode)
{
case 0x1d:
Value = RIGHT_CTRL_PRESSED;
break;
case 0x38:
Value = RIGHT_ALT_PRESSED;
break;
default:
return;
}
}
if (InputData->Flags & KEY_BREAK)
*State &= ~Value;
else
*State |= Value;
}
static UCHAR IntAsciiFromInput(PKEYBOARD_INPUT_DATA InputData, DWORD KeyState)
{
UINT Counter = 0;
while (ScanToAscii[Counter].ScanCode != 0)
{
if (ScanToAscii[Counter].ScanCode == InputData->MakeCode)
{
if (KeyState & SHIFT_PRESSED)
return ScanToAscii[Counter].Shift;
return ScanToAscii[Counter].Normal;
}
Counter++;
}
return 0;
}
/*
* Only the bKeyDown and AsciiChar members are used in the zenwinx library.
*/
void IntTranslateKey(PKEYBOARD_INPUT_DATA InputData, KBD_RECORD *kbd_rec)
{
static DWORD dwControlKeyState;
kbd_rec->wVirtualScanCode = InputData->MakeCode;
kbd_rec->bKeyDown = (InputData->Flags & KEY_BREAK) ? FALSE : TRUE;
IntUpdateControlKeyState(&dwControlKeyState, InputData);
kbd_rec->dwControlKeyState = dwControlKeyState;
if (InputData->Flags & KEY_E0)
kbd_rec->dwControlKeyState |= ENHANCED_KEY;
kbd_rec->AsciiChar = IntAsciiFromInput(InputData, kbd_rec->dwControlKeyState);
}