2 static char RCSId[] = "$Id: keyboard.c,v 1.2 1993/09/13 18:52:02 scott Exp $";
3 static char Copyright[] = "Copyright Scott A. Laird, Erik Bos 1993, 1994";
13 /* #define DEBUG_KEYBOARD */
18 struct KeyTableEntry
{
25 static const struct KeyTableEntry KeyTable
[] =
27 { VK_CANCEL
, 0x3, 0, "" },
28 { VK_BACK
, 0x8, 0xe, "Backspace" },
29 { VK_TAB
, 0x9, 0xf, "Tab" },
30 { VK_CLEAR
, 0, 0x4c, "Clear" },
31 { VK_RETURN
, 0xd, 0x1c, "Enter" },
32 { VK_SHIFT
, 0, 0x2a, "Shift" },
33 { VK_CONTROL
, 0, 0x1d, "Ctrl" },
34 { VK_MENU
, 0, 0x38, "Alt" },
35 { VK_CAPITAL
, 0, 0x3a, "Caps Lock" },
36 { VK_ESCAPE
, 0x1b, 0x1, "Esc" },
37 { VK_SPACE
, 0x20, 0x39, "Space" },
38 { VK_PRIOR
, 0, 0x49, "Page Up" },
39 { VK_NEXT
, 0, 0x51, "Page Down" },
40 { VK_END
, 0, 0x4f, "End" },
41 { VK_HOME
, 0, 0x47, "Home" },
42 { VK_LEFT
, 0, 0x4b, "Left Arrow" },
43 { VK_UP
, 0, 0x48, "Up Arrow" },
44 { VK_RIGHT
, 0, 0x4d, "Right Arrow" },
45 { VK_DOWN
, 0, 0x50, "Down Arrow" },
46 { VK_INSERT
, 0, 0x52, "Ins" },
47 { VK_DELETE
, 0, 0x53, "Del" },
48 { VK_0
, 0x30, 0xb, "0" },
49 { VK_1
, 0x31, 0x2, "1" },
50 { VK_2
, 0x32, 0x3, "2" },
51 { VK_3
, 0x33, 0x4, "3" },
52 { VK_4
, 0x34, 0x5, "4" },
53 { VK_5
, 0x35, 0x6, "5" },
54 { VK_6
, 0x36, 0x7, "6" },
55 { VK_7
, 0x37, 0x8, "7" },
56 { VK_8
, 0x38, 0x9, "8" },
57 { VK_9
, 0x39, 0xa, "9" },
58 { VK_A
, 0x41, 0x1e, "A" },
59 { VK_B
, 0x42, 0x30, "B" },
60 { VK_C
, 0x43, 0x2e, "C" },
61 { VK_D
, 0x44, 0x20, "D" },
62 { VK_E
, 0x45, 0x12, "E" },
63 { VK_F
, 0x46, 0x21, "F" },
64 { VK_G
, 0x47, 0x22, "G" },
65 { VK_H
, 0x48, 0x23, "H" },
66 { VK_I
, 0x49, 0x17, "I" },
67 { VK_J
, 0x4a, 0x24, "J" },
68 { VK_K
, 0x4b, 0x25, "K" },
69 { VK_L
, 0x4c, 0x26, "L" },
70 { VK_M
, 0x4d, 0x32, "M" },
71 { VK_N
, 0x4e, 0x31, "N" },
72 { VK_O
, 0x4f, 0x18, "O" },
73 { VK_P
, 0x50, 0x19, "P" },
74 { VK_Q
, 0x51, 0x10, "Q" },
75 { VK_R
, 0x52, 0x13, "R" },
76 { VK_S
, 0x53, 0x1f, "S" },
77 { VK_T
, 0x54, 0x14, "T" },
78 { VK_U
, 0x55, 0x16, "U" },
79 { VK_V
, 0x56, 0x2f, "V" },
80 { VK_W
, 0x57, 0x11, "W" },
81 { VK_X
, 0x58, 0x2d, "X" },
82 { VK_Y
, 0x59, 0x15, "Y" },
83 { VK_Z
, 0x5a, 0x2c, "Z" },
84 { VK_NUMPAD0
, 0, 0x52, "Num 0" },
85 { VK_NUMPAD1
, 0, 0x4f, "Num 1" },
86 { VK_NUMPAD2
, 0, 0x50, "Num 2" },
87 { VK_NUMPAD3
, 0, 0x51, "Num 3" },
88 { VK_NUMPAD4
, 0, 0x4b, "Num 4" },
89 { VK_NUMPAD5
, 0, 0x4c, "Num 5" },
90 { VK_NUMPAD6
, 0, 0x4d, "Num 6" },
91 { VK_NUMPAD7
, 0, 0x47, "Num 7" },
92 { VK_NUMPAD8
, 0, 0x48, "Num 8" },
93 { VK_NUMPAD9
, 0, 0x49, "Num 9" },
94 { VK_MULTIPLY
, 0x2a, 0x37, "Num *" },
95 { VK_ADD
, 0x2b, 0x4e, "Num +" },
96 { VK_SEPARATOR
, 0, 0, "" },
97 { VK_SUBTRACT
, 0x2d, 0x4a, "Num -" },
98 { VK_DECIMAL
, 0x2e, 0x53, "Num Del" },
99 { VK_DIVIDE
, 0x2f, 0, "Num /" },
100 { VK_F1
, 0, 0x3b, "F1" },
101 { VK_F2
, 0, 0x3c, "F2" },
102 { VK_F3
, 0, 0x3d, "F3" },
103 { VK_F4
, 0, 0x3e, "F4" },
104 { VK_F5
, 0, 0x3f, "F5" },
105 { VK_F6
, 0, 0x40, "F6" },
106 { VK_F7
, 0, 0x41, "F7" },
107 { VK_F8
, 0, 0x42, "F8" },
108 { VK_F9
, 0, 0x43, "F9" },
109 { VK_F10
, 0, 0x44, "F10" },
110 { VK_F11
, 0, 0x57, "F11" },
111 { VK_F12
, 0, 0x58, "F12" },
112 { VK_NUMLOCK
, 0, 0x45, "Num Lock" },
113 { VK_SCROLL
, 0, 0x46, "Scroll Lock" },
114 /* Allowable ranges for OEM-specific virtual-key codes */
115 /* - 0xBA-0xC0 OEM specific */
116 { 0xba, 0x3b, 0x27, ";" },
117 { 0xbb, 0x3d, 0xd, "=" },
118 { 0xbc, 0x2c, 0x33, "," },
119 { 0xbd, 0x2d, 0xc, "-" },
120 { 0xbe, 0x2e, 0x34, "." },
121 { 0xbf, 0x2f, 0x35, "/" },
122 { 0xc0, 0x60, 0x29, "`" },
123 /* - 0xDB-0xE4 OEM specific */
124 { 0xdb, 0x5b, 0x1a, "[" },
125 { 0xdc, 0x5c, 0x2b, "\\" },
126 { 0xdd, 0x5d, 0x1b, "]" },
127 { 0xde, 0x27, 0x28, "'" },
128 { 0xe2, 0x5c, 0x56, "\\" },
129 /* - 0xE6 OEM specific */
130 /* - 0xE9-0xF5 OEM specific */
133 #define KeyTableSize sizeof(KeyTable) / sizeof(struct KeyTableEntry)
135 int ToAscii(WORD wVirtKey
, WORD wScanCode
, LPSTR lpKeyState
,
136 LPVOID lpChar
, WORD wFlags
)
138 char shift
= lpKeyState
[VK_SHIFT
] < 0;
141 dprintf_keyboard(stddeb
, "ToAscii (%x,%x) -> ", wVirtKey
, wScanCode
);
143 /* FIXME: codepage is broken */
148 #define vkcase2(k1,k2,val) case val : *(BYTE*)lpChar = shift ? k2 : k1; break;
149 #define vkcase(k, val) vkcase2(val, k, val)
156 if (i
== KeyTableSize
)
158 dprintf_keyboard(stddeb
, "0\n");
161 if (KeyTable
[i
].virtualkey
== wVirtKey
)
163 if (!isprint(KeyTable
[i
].ASCII
) && !isspace(KeyTable
[i
].ASCII
))
165 dprintf_keyboard(stddeb
, "0\n");
168 dprintf_keyboard(stddeb
,"\"%s\" ", KeyTable
[i
].name
);
170 *(BYTE
*)lpChar
= KeyTable
[i
].ASCII
;
171 *(((BYTE
*)lpChar
) + 1) = 0;
173 if (isalpha(*(BYTE
*)lpChar
))
174 if( (lpKeyState
[VK_CAPITAL
]<0 && !lpKeyState
[VK_SHIFT
]) ||
175 (!lpKeyState
[VK_CAPITAL
] && lpKeyState
[VK_SHIFT
]<0) )
176 *(BYTE
*)lpChar
= toupper( *(BYTE
*)lpChar
);
178 *(BYTE
*)lpChar
= tolower( *(BYTE
*)lpChar
);
183 if (lpKeyState
[VK_CONTROL
] < 0)
184 *(BYTE
*)lpChar
= *(BYTE
*)lpChar
& 0x1f;
185 dprintf_keyboard(stddeb
, "1 (%x)\n", *(BYTE
*)lpChar
);
189 DWORD
OemKeyScan(WORD wOemChar
)
191 dprintf_keyboard(stddeb
,"*OemKeyScan (%d)\n",wOemChar
);
196 /* VkKeyScan translates an ANSI character to a virtual-key and shift code
197 * for the current keyboard.
198 * FIXME high-order byte should yield :
202 * 3-5 Shift-key combinations that are not used for characters
205 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
208 WORD
VkKeyScan(WORD cChar
)
212 dprintf_keyboard(stddeb
,"VkKeyScan (%d)\n",cChar
);
214 for (i
= 0 ; i
!= KeyTableSize
; i
++)
215 if (KeyTable
[i
].ASCII
== cChar
)
216 return KeyTable
[i
].virtualkey
;
221 WORD
VkKeyScan32W(WORD cChar
)
223 /* lower part of cChar is used anyway */
224 return VkKeyScan(cChar
);
227 int GetKeyboardType(int nTypeFlag
)
229 dprintf_keyboard(stddeb
,"GetKeyboardType(%d)\n",nTypeFlag
);
232 case 0: /* Keyboard type */
233 return 4; /* AT-101 */
235 case 1: /* Keyboard Subtype */
236 return 0; /* There are no defined subtypes */
238 case 2: /* Number of F-keys */
239 return 12; /* We're doing an 101 for now, so return 12 F-keys */
242 fprintf(stderr
, "Unknown type on GetKeyboardType\n");
243 return 0; /* The book says 0 here, so 0 */
247 /* MapVirtualKey translates keycodes from one format to another. */
249 WORD
MapVirtualKey(WORD wCode
, WORD wMapType
)
254 case 0: /* vkey-code to scan-code */
255 for (i
= 0 ; i
!= KeyTableSize
; i
++)
256 if (KeyTable
[i
].virtualkey
== wCode
)
257 return KeyTable
[i
].scancode
;
260 case 1: /* scan-code to vkey-code */
261 for (i
= 0 ; i
!= KeyTableSize
; i
++)
262 if (KeyTable
[i
].scancode
== wCode
)
263 return KeyTable
[i
].virtualkey
;
266 case 2: /* vkey-code to unshifted ANSI code */
267 /* FIXME : what does unshifted mean ? 'a' or 'A' ? */
268 for (i
= 0 ; i
!= KeyTableSize
; i
++)
269 if (KeyTable
[i
].virtualkey
== wCode
)
270 return KeyTable
[i
].ASCII
;
273 default: /* reserved */
274 fprintf(stderr
, "MapVirtualKey: unknown wMapType %d !\n",
281 int GetKbCodePage(void)
283 dprintf_keyboard(stddeb
,"GetKbCodePage()\n");
287 /****************************************************************************
288 * GetKeyNameText32W (USER32.247)
290 INT32
GetKeyNameText32W(LONG lParam
, LPWSTR lpBuffer
, INT32 nSize
)
292 LPSTR buf
= xmalloc(nSize
);
293 int res
= GetKeyNameText32A(lParam
,buf
,nSize
);
295 lstrcpynAtoW(lpBuffer
,buf
,nSize
);
300 /****************************************************************************
301 * GetKeyNameText32A (USER32.246)
303 INT32
GetKeyNameText32A(LONG lParam
, LPSTR lpBuffer
, INT32 nSize
)
305 return GetKeyNameText16(lParam
,lpBuffer
,nSize
);
308 /****************************************************************************
309 * GetKeyNameText16 (KEYBOARD.133)
311 INT16
GetKeyNameText16(LONG lParam
, LPSTR lpBuffer
, INT16 nSize
)
315 dprintf_keyboard(stddeb
,"GetKeyNameText(%ld,<ptr>,%d)\n",lParam
,nSize
);
320 for (i
= 0 ; i
!= KeyTableSize
; i
++)
321 if (KeyTable
[i
].scancode
== lParam
) {
322 lstrcpyn32A( lpBuffer
, KeyTable
[i
].name
, nSize
);
323 return strlen(lpBuffer
);