2 * Keyboard related functions
4 * Copyright 1993 Bob Amstadt
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 1997 David Faure
13 #include <X11/keysym.h>
15 #include <X11/Xresource.h>
16 #include <X11/Xutil.h>
17 #include <X11/Xatom.h>
25 /* #define DEBUG_KEYBOARD */
30 BOOL32 MouseButtonsStates
[3];
31 BOOL32 AsyncMouseButtonsStates
[3];
32 BYTE InputKeyStateTable
[256];
33 BYTE QueueKeyStateTable
[256];
34 BYTE AsyncKeyStateTable
[256];
36 static int NumLockMask
;
38 static int min_keycode
, max_keycode
;
39 static int keyc2vkey
[256];
45 unsigned long count
: 16;
46 unsigned long code
: 8;
47 unsigned long extended
: 1;
48 unsigned long unused
: 2;
49 unsigned long win_internal
: 2;
50 unsigned long context
: 1;
51 unsigned long previous
: 1;
52 unsigned long transition
: 1;
57 typedef enum {OFF
,INTERM
,ON
} ToggleKeyState
;
59 /* Keyboard translation tables */
60 static const int special_key
[] =
62 VK_BACK
, VK_TAB
, 0, VK_CLEAR
, 0, VK_RETURN
, 0, 0, /* FF08 */
63 0, 0, 0, VK_PAUSE
, VK_SCROLL
, 0, 0, 0, /* FF10 */
64 0, 0, 0, VK_ESCAPE
/* FF18 */
67 static const int cursor_key
[] =
69 VK_HOME
, VK_LEFT
, VK_UP
, VK_RIGHT
, VK_DOWN
, VK_PRIOR
,
70 VK_NEXT
, VK_END
/* FF50 */
73 static const int misc_key
[] =
75 VK_SELECT
, VK_SNAPSHOT
, VK_EXECUTE
, VK_INSERT
, 0, 0, 0, 0, /* FF60 */
76 VK_CANCEL
, VK_HELP
, VK_CANCEL
, VK_MENU
/* FF68 */
79 static const int keypad_key
[] =
81 0, VK_NUMLOCK
, /* FF7E */
82 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
83 0, 0, 0, 0, 0, VK_RETURN
, 0, 0, /* FF88 */
84 0, 0, 0, 0, 0, VK_HOME
, VK_LEFT
, VK_UP
, /* FF90 */
85 VK_RIGHT
, VK_DOWN
, VK_PRIOR
, VK_NEXT
, VK_END
, 0,
86 VK_INSERT
, VK_DELETE
, /* FF98 */
87 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
88 0, 0, VK_MULTIPLY
, VK_ADD
, VK_SEPARATOR
, VK_SUBTRACT
,
89 VK_DECIMAL
, VK_DIVIDE
, /* FFA8 */
90 VK_NUMPAD0
, VK_NUMPAD1
, VK_NUMPAD2
, VK_NUMPAD3
, VK_NUMPAD4
,
91 VK_NUMPAD5
, VK_NUMPAD6
, VK_NUMPAD7
, /* FFB0 */
92 VK_NUMPAD8
, VK_NUMPAD9
/* FFB8 */
95 static const int function_key
[] =
97 VK_F1
, VK_F2
, /* FFBE */
98 VK_F3
, VK_F4
, VK_F5
, VK_F6
, VK_F7
, VK_F8
, VK_F9
, VK_F10
, /* FFC0 */
99 VK_F11
, VK_F12
, VK_F13
, VK_F14
, VK_F15
, VK_F16
/* FFC8 */
102 static const int modifier_key
[] =
104 VK_SHIFT
, VK_SHIFT
, VK_CONTROL
, VK_CONTROL
, VK_CAPITAL
, 0, /* FFE1 */
105 VK_MENU
, VK_MENU
, VK_MENU
, VK_MENU
/* FFE7 */
109 static WORD
EVENT_event_to_vkey( XKeyEvent
*e
)
113 XLookupString(e
, NULL
, 0, &keysym
, NULL
);
115 if ((keysym
>= 0xFFAE) && (keysym
<= 0xFFB9) && (e
->state
& NumLockMask
))
116 /* Only the Keypad keys 0-9 and . send different keysyms
117 * depending on the NumLock state */
118 return keypad_key
[(keysym
& 0xFF) - 0x7E];
120 return keyc2vkey
[e
->keycode
];
123 /**********************************************************************
126 BOOL32
KEYBOARD_Init(void)
128 int i
, keysyms_per_keycode
;
130 XModifierKeymap
*mmp
;
136 XDisplayKeycodes(display
, &min_keycode
, &max_keycode
);
137 ksp
= XGetKeyboardMapping(display
, min_keycode
,
138 max_keycode
+ 1 - min_keycode
, &keysyms_per_keycode
);
139 /* We are only interested in keysyms_per_keycode.
140 There is no need to hold a local copy of the keysyms table */
142 mmp
= XGetModifierMapping(display
);
143 kcp
= mmp
->modifiermap
;
144 for (i
= 0; i
< 8; i
+= 1) /* There are 8 modifier keys */
148 for (j
= 0; j
< mmp
->max_keypermod
; j
+= 1, kcp
+= 1)
153 for (k
= 0; k
< keysyms_per_keycode
; k
+= 1)
154 if (XKeycodeToKeysym(display
, *kcp
, k
) == XK_Mode_switch
)
157 dprintf_key(stddeb
, "AltGrMask is %x\n", AltGrMask
);
159 else if (XKeycodeToKeysym(display
, *kcp
, k
) == XK_Num_Lock
)
161 NumLockMask
= 1 << i
;
162 dprintf_key(stddeb
, "NumLockMask is %x\n", NumLockMask
);
166 XFreeModifiermap(mmp
);
168 /* Now build two conversion arrays :
169 * keycode -> vkey + extended
170 * vkey + extended -> keycode */
172 e2
.display
= display
;
175 OEMvkey
= 0xb9; /* first OEM virtual key available is ba */
176 for (e2
.keycode
=min_keycode
; e2
.keycode
<=max_keycode
; e2
.keycode
++)
178 XLookupString(&e2
, NULL
, 0, &keysym
, NULL
);
180 if (keysym
) /* otherwise, keycode not used */
182 if ((keysym
>> 8) == 0xFF) /* non-character key */
184 int key
= keysym
& 0xff;
186 if (key
>= 0x08 && key
<= 0x1B) /* special key */
187 vkey
= special_key
[key
- 0x08];
188 else if (key
>= 0x50 && key
<= 0x57) /* cursor key */
189 vkey
= cursor_key
[key
- 0x50];
190 else if (key
>= 0x60 && key
<= 0x6B) /* miscellaneous key */
191 vkey
= misc_key
[key
- 0x60];
192 else if (key
>= 0x7E && key
<= 0xB9) /* keypad key */
193 vkey
= keypad_key
[key
- 0x7E];
194 else if (key
>= 0xBE && key
<= 0xCD) /* function key */
196 vkey
= function_key
[key
- 0xBE];
197 vkey
|= 0x100; /* set extended bit */
199 else if (key
>= 0xE1 && key
<= 0xEA) /* modifier key */
200 vkey
= modifier_key
[key
- 0xE1];
201 else if (key
== 0xFF) /* DEL key */
203 /* extended must also be set for ALT_R, CTRL_R,
204 INS, DEL, HOME, END, PAGE_UP, PAGE_DOWN, ARROW keys,
205 keypad / and keypad ENTER (SDK 3.1 Vol.3 p 138) */
206 /* FIXME should we set extended bit for NumLock ? My
207 * Windows does ... DF */
227 for (i
= 0; (i
< keysyms_per_keycode
) && (!vkey
); i
++)
229 keysym
= XLookupKeysym(&e2
, i
);
230 if ((keysym
>= VK_0
&& keysym
<= VK_9
)
231 || (keysym
>= VK_A
&& keysym
<= VK_Z
)
232 || keysym
== VK_SPACE
)
238 /* Others keys: let's assign OEM virtual key codes in the allowed range,
239 * that is ([0xba,0xc0], [0xdb,0xe4], 0xe6 (given up) et [0xe9,0xf5]) */
242 case 0xc1 : OEMvkey
=0xdb; break;
243 case 0xe5 : OEMvkey
=0xe9; break;
244 case 0xf6 : OEMvkey
=0xf5; fprintf(stderr
,"No more OEM vkey available!\n");
249 if (debugging_keyboard
)
251 fprintf(stddeb
,"OEM specific virtual key %X assigned to keycode %X :\n ("
252 ,OEMvkey
,e2
.keycode
);
253 for (i
= 0; i
< keysyms_per_keycode
; i
+= 1)
257 keysym
= XLookupKeysym(&e2
, i
);
258 ksname
= XKeysymToString(keysym
);
261 fprintf(stddeb
, "%lX (%s) ", keysym
, ksname
);
263 fprintf(stddeb
, ")\n");
267 keyc2vkey
[e2
.keycode
] = vkey
;
273 /***********************************************************************
274 * KEYBOARD_HandleEvent
276 * Handle a X key event
278 void KEYBOARD_HandleEvent( XKeyEvent
*event
)
286 static BOOL force_extended
= FALSE
; /* hack for AltGr translation */
288 ToggleKeyState
* State
;
289 static ToggleKeyState NumState
=OFF
, CapsState
=OFF
;
291 int ascii_chars
= XLookupString(event
, Str
, 1, &keysym
, &cs
);
293 dprintf_key(stddeb
, "EVENT_key : state = %X\n", event
->state
);
294 if (keysym
== XK_Mode_switch
)
296 dprintf_key(stddeb
, "Alt Gr key event received\n");
297 event
->keycode
= XKeysymToKeycode(event
->display
, XK_Control_L
);
298 dprintf_key(stddeb
, "Control_L is keycode 0x%x\n", event
->keycode
);
299 KEYBOARD_HandleEvent(event
);
300 event
->keycode
= XKeysymToKeycode(event
->display
, XK_Alt_L
);
301 dprintf_key(stddeb
, "Alt_L is keycode 0x%x\n", event
->keycode
);
302 force_extended
= TRUE
;
303 KEYBOARD_HandleEvent(event
);
304 force_extended
= FALSE
;
308 Str
[ascii_chars
] = '\0';
313 ksname
= XKeysymToString(keysym
);
316 fprintf(stddeb
, "%s : keysym=%lX (%s), ascii chars=%u / %X / '%s'\n",
317 (event
->type
== KeyPress
) ? "KeyPress" : "KeyRelease",
318 keysym
, ksname
, ascii_chars
, Str
[0] & 0xff, Str
);
322 /* Ctrl-Alt-Return enters the debugger */
323 if ((keysym
== XK_Return
) && (event
->type
== KeyPress
) &&
324 (event
->state
& ControlMask
) && (event
->state
& Mod1Mask
))
325 DEBUG_EnterDebugger();
328 vkey
= EVENT_event_to_vkey(event
);
329 if (force_extended
) vkey
|= 0x100;
331 dprintf_key(stddeb
, "keycode 0x%x converted to vkey 0x%x\n",
332 event
->keycode
, vkey
);
337 keylp
.lp1
.code
= LOBYTE(event
->keycode
) - 8;
338 keylp
.lp1
.extended
= (vkey
& 0x100 ? 1 : 0);
339 keylp
.lp1
.win_internal
= 0; /* this has something to do with dialogs,
340 * don't remember where I read it - AK */
341 /* it's '1' under windows, when a dialog box appears
342 * and you press one of the underlined keys - DF*/
344 if (event
->type
== KeyPress
)
346 keylp
.lp1
.previous
= (InputKeyStateTable
[vkey
] & 0x80) != 0;
347 if (!(InputKeyStateTable
[vkey
] & 0x80))
348 InputKeyStateTable
[vkey
] ^= 0x01;
349 InputKeyStateTable
[vkey
] |= 0x80;
350 keylp
.lp1
.transition
= 0;
351 message
= (InputKeyStateTable
[VK_MENU
] & 0x80)
352 && !(InputKeyStateTable
[VK_CONTROL
] & 0x80)
353 ? WM_SYSKEYDOWN
: WM_KEYDOWN
;
357 UINT sysKey
= (InputKeyStateTable
[VK_MENU
] & 0x80)
358 && !(InputKeyStateTable
[VK_CONTROL
] & 0x80)
359 && (force_extended
== FALSE
); /* for Alt from AltGr */
361 InputKeyStateTable
[vkey
] &= ~0x80;
362 keylp
.lp1
.previous
= 1;
363 keylp
.lp1
.transition
= 1;
364 message
= sysKey
? WM_SYSKEYUP
: WM_KEYUP
;
366 keylp
.lp1
.context
= ( (event
->state
& Mod1Mask
) ||
367 (InputKeyStateTable
[VK_MENU
] & 0x80)) ? 1 : 0;
368 DontPropagate
= FALSE
;
369 if ((vkey
==VK_NUMLOCK
) || (vkey
==VK_CAPITAL
))
372 switch (*( State
= (vkey
==VK_NUMLOCK
? &NumState
: &CapsState
))) {
373 case OFF
:if (event
->type
==KeyPress
)
375 dprintf_keyboard(stddeb
,"OFF + Keypress => DOWN and UP generated. \n");
376 hardware_event( message
, vkey
, keylp
.lp2
, event
->x_root
- desktopX
,
377 event
->y_root
- desktopY
, event
->time
- MSG_WineStartTicks
, 0);
378 message
+= WM_KEYUP
- WM_KEYDOWN
; /* create a *UP message from the *DOWN one */
379 keylp
.lp1
.previous
= 1;
380 keylp
.lp1
.transition
= 1;
384 /* The 'INTERM' state means : just after a 'press' event, if a 'release' event comes,
385 don't "propagate" it. It's from the same key press. Then the state goes to ON.
386 And from there, a 'release' event will switch off the toggle key. */
387 DontPropagate
= TRUE
;
389 InputKeyStateTable
[vkey
] |= 0x01; /* force to 'on' event if a release event was received */
390 dprintf_keyboard(stddeb
,"INTERM : don\'t propagate press/release of toggle key. InputKeyStateTable[%#x] = %#x",vkey
,InputKeyStateTable
[vkey
]);
392 case ON
: if (event
->type
==KeyPress
) DontPropagate
= TRUE
; else
394 KEYLP downkeylp
= keylp
;
395 dprintf_keyboard(stddeb
,"ON + KeyRelease => generating DOWN msg before the UP\n");
396 message
+= WM_KEYDOWN
- WM_KEYUP
; /* create the *DOWN from the *UP */
397 downkeylp
.lp1
.previous
= 0; /* ? */
398 downkeylp
.lp1
.transition
= 0;
399 hardware_event( message
, vkey
, downkeylp
.lp2
, event
->x_root
- desktopX
,
400 event
->y_root
- desktopY
, event
->time
- MSG_WineStartTicks
, 0);
401 message
+= WM_KEYUP
- WM_KEYDOWN
; /* back to the UP message */
405 dprintf_keyboard(stddeb
,"Internal State : %d (0=OFF 1=INTERM 2=ON). InputTable state : %#x \n",*State
,InputKeyStateTable
[vkey
]);
407 if (NumState
== INTERM
) NumState
= ON
;
408 if (CapsState
== INTERM
) CapsState
= ON
;
413 dprintf_key(stddeb
," wParam=%04X, lParam=%08lX\n",
415 dprintf_key(stddeb
," InputKeyState=%X\n",
416 InputKeyStateTable
[vkey
]);
418 hardware_event( message
, vkey
, keylp
.lp2
, event
->x_root
- desktopX
,
419 event
->y_root
- desktopY
, event
->time
- MSG_WineStartTicks
, 0 );
425 /**********************************************************************
426 * GetKeyState [USER.106]
427 * An application calls the GetKeyState function in response to a
428 * keyboard-input message. This function retrieves the state of the key
429 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
431 INT
GetKeyState(INT vkey
)
437 case VK_LBUTTON
: /* VK_LBUTTON is 1 */
438 retval
= MouseButtonsStates
[0];
440 case VK_MBUTTON
: /* VK_MBUTTON is 4 */
441 retval
= MouseButtonsStates
[1];
443 case VK_RBUTTON
: /* VK_RBUTTON is 2 */
444 retval
= MouseButtonsStates
[2];
447 if (vkey
>= 'a' && vkey
<= 'z')
449 retval
= ( (INT
)(QueueKeyStateTable
[vkey
] & 0x80) << 8 ) |
450 (INT
)(QueueKeyStateTable
[vkey
] & 0x01);
452 dprintf_key(stddeb
, "GetKeyState(0x%x) -> %x\n", vkey
, retval
);
456 /**********************************************************************
457 * GetKeyboardState [USER.222]
458 * An application calls the GetKeyboardState function in response to a
459 * keyboard-input message. This function retrieves the state of the keyboard
460 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
462 void GetKeyboardState(BYTE
*lpKeyState
)
464 dprintf_key(stddeb
, "GetKeyboardState()\n");
465 if (lpKeyState
!= NULL
) {
466 QueueKeyStateTable
[VK_LBUTTON
] = MouseButtonsStates
[0] >> 8;
467 QueueKeyStateTable
[VK_MBUTTON
] = MouseButtonsStates
[1] >> 8;
468 QueueKeyStateTable
[VK_RBUTTON
] = MouseButtonsStates
[2] >> 8;
469 memcpy(lpKeyState
, QueueKeyStateTable
, 256);
473 /**********************************************************************
474 * SetKeyboardState [USER.223]
476 void SetKeyboardState(BYTE
*lpKeyState
)
478 dprintf_key(stddeb
, "SetKeyboardState()\n");
479 if (lpKeyState
!= NULL
) {
480 memcpy(QueueKeyStateTable
, lpKeyState
, 256);
481 MouseButtonsStates
[0] = QueueKeyStateTable
[VK_LBUTTON
]? 0x8000: 0;
482 MouseButtonsStates
[1] = QueueKeyStateTable
[VK_MBUTTON
]? 0x8000: 0;
483 MouseButtonsStates
[2] = QueueKeyStateTable
[VK_RBUTTON
]? 0x8000: 0;
487 /**********************************************************************
488 * GetAsyncKeyState (USER.249)
490 * Determine if a key is or was pressed. retval has high-order
491 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
494 * This uses the variable AsyncMouseButtonsStates and
495 * AsyncKeyStateTable (set in event.c) which have the mouse button
496 * number or key number (whichever is applicable) set to true if the
497 * mouse or key had been depressed since the last call to
500 int GetAsyncKeyState(int nKey
)
506 retval
= AsyncMouseButtonsStates
[0] |
507 MouseButtonsStates
[0]? 0x0001: 0;
510 retval
= AsyncMouseButtonsStates
[1] |
511 MouseButtonsStates
[1]? 0x0001: 0;
514 retval
= AsyncMouseButtonsStates
[2] |
515 MouseButtonsStates
[2]? 0x0001: 0;
518 retval
= AsyncKeyStateTable
[nKey
] |
519 (InputKeyStateTable
[nKey
] ? 0x8000 : 0);
523 memset( AsyncMouseButtonsStates
, 0, 3 ); /* all states to false */
524 memset( AsyncKeyStateTable
, 0, 256 );
526 dprintf_key(stddeb
, "GetAsyncKeyState(%x) -> %x\n", nKey
, retval
);
531 /**********************************************************************
532 * TranslateAccelerator [USER.178]
534 * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP -messages
536 INT16
TranslateAccelerator(HWND hWnd
, HACCEL16 hAccel
, LPMSG16 msg
)
538 ACCELHEADER
*lpAccelTbl
;
542 if (hAccel
== 0 || msg
== NULL
) return 0;
543 if (msg
->message
!= WM_KEYDOWN
&&
544 msg
->message
!= WM_KEYUP
&&
545 msg
->message
!= WM_SYSKEYDOWN
&&
546 msg
->message
!= WM_SYSKEYUP
&&
547 msg
->message
!= WM_CHAR
) return 0;
549 dprintf_accel(stddeb
, "TranslateAccelerators hAccel=%04x, hWnd=%04x,\
550 msg->hwnd=%04x, msg->message=%04x\n", hAccel
,hWnd
,msg
->hwnd
,msg
->message
);
552 lpAccelTbl
= (LPACCELHEADER
)GlobalLock16(hAccel
);
553 for (sendmsg
= i
= 0; i
< lpAccelTbl
->wCount
; i
++)
555 if(msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
)
557 if (msg
->message
== WM_CHAR
)
559 if ( !(lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) &&
560 !(lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) )
562 dprintf_accel(stddeb
,"found accel for WM_CHAR: ('%c')",msg
->wParam
&0xff);
568 if(lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
)
571 dprintf_accel(stddeb
,"found accel for virt_key %04x (scan %04x)",
572 msg
->wParam
,0xff & HIWORD(msg
->lParam
));
573 if(GetKeyState(VK_SHIFT
) & 0x8000) mask
|= SHIFT_ACCEL
;
574 if(GetKeyState(VK_CONTROL
) & 0x8000) mask
|= CONTROL_ACCEL
;
575 if(GetKeyState(VK_MENU
) & 0x8000) mask
|= ALT_ACCEL
;
576 if(mask
== (lpAccelTbl
->tbl
[i
].type
&
577 (SHIFT_ACCEL
| CONTROL_ACCEL
| ALT_ACCEL
)))
580 dprintf_accel(stddeb
,", but incorrect SHIFT/CTRL/ALT-state\n");
584 if (!(msg
->lParam
& 0x01000000)) /* no special_key */
586 if ((lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) && (msg
->lParam
& 0x20000000))
587 { /* ^^ ALT pressed */
588 dprintf_accel(stddeb
,"found accel for Alt-%c", msg
->wParam
&0xff);
595 if (sendmsg
) /* found an accelerator, but send a message... ? */
597 INT16 iSysStat
,iStat
,mesg
=0;
598 HMENU16 hSysMenu
,hMenu
;
600 if (msg
->message
== WM_KEYUP
|| msg
->message
== WM_SYSKEYUP
)
606 if (!IsWindowEnabled16(hWnd
))
610 hMenu
=GetMenu32(hWnd
);
611 hSysMenu
=GetSystemMenu32(hWnd
,FALSE
);
613 iSysStat
=GetMenuState32(hSysMenu
,lpAccelTbl
->tbl
[i
].wIDval
,MF_BYCOMMAND
);
617 iStat
=GetMenuState32(hMenu
,lpAccelTbl
->tbl
[i
].wIDval
,MF_BYCOMMAND
);
622 if (iSysStat
& (MF_DISABLED
|MF_GRAYED
))
631 if (IsIconic32(hWnd
))
635 if (iStat
& (MF_DISABLED
|MF_GRAYED
))
645 if ( mesg
==WM_COMMAND
|| mesg
==WM_SYSCOMMAND
)
647 dprintf_accel(stddeb
,", sending %s, wParam=%0x\n",
648 mesg
==WM_COMMAND
? "WM_COMMAND" : "WM_SYSCOMMAND",
649 lpAccelTbl
->tbl
[i
].wIDval
);
650 SendMessage16(hWnd
, mesg
, lpAccelTbl
->tbl
[i
].wIDval
,0x00010000L
);
654 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
655 * #0: unknown (please report!)
656 * #1: for WM_KEYUP,WM_SYSKEYUP
657 * #2: mouse is captured
658 * #3: window is disabled
659 * #4: it's a disabled system menu option
660 * #5: it's a menu option, but window is iconic
661 * #6: it's a menu option, but disabled
663 dprintf_accel(stddeb
,", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg
);
665 GlobalUnlock16(hAccel
);
670 GlobalUnlock16(hAccel
);
675 DWORD
OemKeyScan(WORD wOemChar
)
677 dprintf_keyboard(stddeb
,"*OemKeyScan (%d)\n",wOemChar
);
682 /* VkKeyScan translates an ANSI character to a virtual-key and shift code
683 * for the current keyboard.
684 * FIXME high-order byte should yield :
688 * 3-5 Shift-key combinations that are not used for characters
691 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
694 WORD
VkKeyScan(WORD cChar
)
697 dprintf_keyboard(stddeb
,"VkKeyScan '%c'(%d) ",cChar
,cChar
);
699 /* let's do this : char = keysym (for ANSI chars) -> keyc [ + shift ... (?? how ??)]
700 keyc -> (keyc2vkey) vkey */
702 keycode
= XKeysymToKeycode(display
, cChar
& 0xFF);
704 dprintf_keyboard(stddeb
," ... got keycode 0x%x ... returning 0x%x\n",
705 keycode
,keyc2vkey
[keycode
]);
706 return keyc2vkey
[keycode
];
709 WORD
VkKeyScan32W(WORD cChar
)
711 /* lower part of cChar is used anyway */
712 return VkKeyScan(cChar
);
715 int GetKeyboardType(int nTypeFlag
)
717 dprintf_keyboard(stddeb
,"GetKeyboardType(%d)\n",nTypeFlag
);
720 case 0: /* Keyboard type */
721 return 4; /* AT-101 */
723 case 1: /* Keyboard Subtype */
724 return 0; /* There are no defined subtypes */
726 case 2: /* Number of F-keys */
727 return 12; /* We're doing an 101 for now, so return 12 F-keys */
730 fprintf(stderr
, "Unknown type on GetKeyboardType\n");
731 return 0; /* The book says 0 here, so 0 */
735 /* MapVirtualKey translates keycodes from one format to another. */
737 WORD
MapVirtualKey(WORD wCode
, WORD wMapType
)
739 #define returnMVK(value) { dprintf_keyboard(stddeb,"returning 0x%x.\n",value); return value; }
741 dprintf_keyboard(stddeb
,"MapVirtualKey wCode=0x%x wMapType=%d ... ",wCode
,wMapType
);
743 case 0: { /* vkey-code to scan-code */
744 /* let's do vkey -> keycode -> scan */
746 for (keyc
=min_keycode
; keyc
<=max_keycode
; keyc
++) /* see event.c */
747 if ((keyc2vkey
[keyc
] & 0xFF)== wCode
)
748 returnMVK (keyc
- 8);
751 case 1: /* scan-code to vkey-code */
752 /* let's do scan -> keycode -> vkey */
754 returnMVK (keyc2vkey
[(wCode
& 0xFF) + 8]);
756 case 2: { /* vkey-code to unshifted ANSI code */
757 /* (was FIXME) : what does unshifted mean ? 'a' or 'A' ? */
758 /* My Windows returns 'A'. */
759 /* let's do vkey -> keycode -> (XLookupString) ansi char */
764 e
.state
= 0; /* unshifted */
765 e
.keycode
= MapVirtualKey( wCode
, 0);
766 if (!XLookupString(&e
, s
, 2 , &keysym
, NULL
))
771 default: /* reserved */
772 fprintf(stderr
, "MapVirtualKey: unknown wMapType %d !\n",
779 int GetKbCodePage(void)
781 dprintf_keyboard(stddeb
,"GetKbCodePage()\n");
785 /****************************************************************************
786 * GetKeyNameText32W (USER32.247)
788 INT32
GetKeyNameText32W(LONG lParam
, LPWSTR lpBuffer
, INT32 nSize
)
790 LPSTR buf
= xmalloc(nSize
);
791 int res
= GetKeyNameText32A(lParam
,buf
,nSize
);
793 lstrcpynAtoW(lpBuffer
,buf
,nSize
);
798 /****************************************************************************
799 * GetKeyNameText32A (USER32.246)
801 INT32
GetKeyNameText32A(LONG lParam
, LPSTR lpBuffer
, INT32 nSize
)
803 return GetKeyNameText16(lParam
,lpBuffer
,nSize
);
806 /****************************************************************************
807 * GetKeyNameText16 (KEYBOARD.133)
809 INT16
GetKeyNameText16(LONG lParam
, LPSTR lpBuffer
, INT16 nSize
)
813 dprintf_keyboard(stddeb
,"GetKeyNameText(%ld,<ptr>,%d)\n",lParam
,nSize
);
818 /* for (i = 0 ; i != KeyTableSize ; i++)
819 if (KeyTable[i].scancode == lParam) {
820 lstrcpyn32A( lpBuffer, KeyTable[i].name, nSize );
821 return strlen(lpBuffer);
824 /* FIXME ! GetKeyNameText is still to do...
830 int ToAscii(WORD wVirtKey
, WORD wScanCode
, LPSTR lpKeyState
,
831 LPVOID lpChar
, WORD wFlags
)
835 static XComposeStatus cs
;
841 for (keyc
=min_keycode
; keyc
<=max_keycode
; keyc
++)
842 { /* this could be speeded up by making another table, an array of struct vkey,keycode
843 * (vkey -> keycode) with vkeys sorted .... but it takes memory (512*3 bytes)! DF */
844 if ((keyc2vkey
[keyc
] & 0xFF)== wVirtKey
) /* no need to make a more precise test (with the extended bit correctly set above wVirtKey ... VK* are different enough... */
846 if ((e
.keycode
) && ((wVirtKey
<0x10) || (wVirtKey
>0x12)))
847 /* it's normal to have 2 shift, control, and alt ! */
848 dprintf_keyboard(stddeb
,"ToAscii : The keycodes %X and %X are matching the same vkey %X\n",
849 e
.keycode
,keyc
,wVirtKey
);
853 if ((!e
.keycode
) && (lpKeyState
[VK_NUMLOCK
] & 0x01))
855 if ((wVirtKey
>=VK_NUMPAD0
) && (wVirtKey
<=VK_NUMPAD9
))
856 e
.keycode
= XKeysymToKeycode(e
.display
, wVirtKey
-VK_NUMPAD0
+XK_KP_0
);
857 if (wVirtKey
==VK_DECIMAL
)
858 e
.keycode
= XKeysymToKeycode(e
.display
, XK_KP_Decimal
);
862 fprintf(stderr
,"ToAscii : Unknown virtual key %X !!! \n",wVirtKey
);
863 return wVirtKey
; /* whatever */
866 if (lpKeyState
[VK_SHIFT
] & 0x80)
867 e
.state
|= ShiftMask
;
868 dprintf_keyboard(stddeb
,"ToAscii : lpKeyState[0x14(VK_CAPITAL)]=%#x\n",lpKeyState
[VK_CAPITAL
]);
869 if (lpKeyState
[VK_CAPITAL
] & 0x01)
871 if (lpKeyState
[VK_CONTROL
] & 0x80)
872 if (lpKeyState
[VK_MENU
] & 0x80)
873 e
.state
|= AltGrMask
;
875 e
.state
|= ControlMask
;
876 if (lpKeyState
[VK_NUMLOCK
] & 0x01)
877 e
.state
|= NumLockMask
;
878 dprintf_key(stddeb
, "ToAscii(%04X, %04X) : faked state = %X\n",
879 wVirtKey
, wScanCode
, e
.state
);
880 ret
= XLookupString(&e
, lpChar
, 2, &keysym
, &cs
);
885 ((char*)lpChar
)[1] = '\0';
889 case 0x1000FE7E : /* Xfree's XK_Dtilde */
893 case 0x1000FE27 : /* Xfree's XK_Dacute_accent */
896 case XK_dead_circumflex
:
897 case 0x1000FE5E : /* Xfree's XK_Dcircumflex_accent */
901 case 0x1000FE60 : /* Xfree's XK_Dgrave_accent */
904 case XK_dead_diaeresis
:
905 case 0x1000FE22 : /* Xfree's XK_Ddiaeresis */
911 *(char*)lpChar
= dead_char
;
918 ksname
= XKeysymToString(keysym
);
921 if ((keysym
>> 8) != 0xff)
923 fprintf(stderr
, "Please report : no char for keysym %04lX (%s) :\n",
925 fprintf(stderr
, " wVirtKey = %X, wScanCode = %X, keycode = %X, state = %X\n",
926 wVirtKey
, wScanCode
, e
.keycode
, e
.state
);
930 dprintf_key(stddeb
, "ToAscii about to return %d with char %x\n",
931 ret
, *(char*)lpChar
);