2 * Keyboard related functions
4 * Copyright 1993 Bob Amstadt
5 * Copyright 1996 Albrecht Kleine
15 extern BOOL MouseButtonsStates
[3];
16 extern BOOL AsyncMouseButtonsStates
[3];
17 extern BYTE InputKeyStateTable
[256];
18 BYTE AsyncKeyStateTable
[256];
20 extern BYTE QueueKeyStateTable
[256];
22 /**********************************************************************
23 * GetKeyState [USER.106]
24 * An application calls the GetKeyState function in response to a
25 * keyboard-input message. This function retrieves the state of the key
26 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
28 INT
GetKeyState(INT keycode
)
32 if (keycode
>= 'a' && keycode
<= 'z')
36 retval
= MouseButtonsStates
[0];
38 retval
= MouseButtonsStates
[1];
40 retval
= MouseButtonsStates
[2];
42 retval
= ( (INT
)(QueueKeyStateTable
[keycode
] & 0x80) << 8 ) |
43 (INT
)(QueueKeyStateTable
[keycode
] & 0x01);
46 dprintf_key(stddeb
, "GetKeyState(%x) -> %x\n", keycode
, retval
);
50 /**********************************************************************
51 * GetKeyboardState [USER.222]
52 * An application calls the GetKeyboardState function in response to a
53 * keyboard-input message. This function retrieves the state of the keyboard
54 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
56 void GetKeyboardState(BYTE
*lpKeyState
)
58 if (lpKeyState
!= NULL
) {
59 QueueKeyStateTable
[VK_LBUTTON
] = MouseButtonsStates
[0] >> 8;
60 QueueKeyStateTable
[VK_MBUTTON
] = MouseButtonsStates
[1] >> 8;
61 QueueKeyStateTable
[VK_RBUTTON
] = MouseButtonsStates
[2] >> 8;
62 memcpy(lpKeyState
, QueueKeyStateTable
, 256);
66 /**********************************************************************
67 * SetKeyboardState [USER.223]
69 void SetKeyboardState(BYTE
*lpKeyState
)
71 if (lpKeyState
!= NULL
) {
72 memcpy(QueueKeyStateTable
, lpKeyState
, 256);
73 MouseButtonsStates
[0] = QueueKeyStateTable
[VK_LBUTTON
]? 0x8000: 0;
74 MouseButtonsStates
[1] = QueueKeyStateTable
[VK_MBUTTON
]? 0x8000: 0;
75 MouseButtonsStates
[2] = QueueKeyStateTable
[VK_RBUTTON
]? 0x8000: 0;
79 /**********************************************************************
80 * GetAsyncKeyState (USER.249)
82 * Determine if a key is or was pressed. retval has high-order
83 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
86 * This uses the variable AsyncMouseButtonsStates and
87 * AsyncKeyStateTable (set in event.c) which have the mouse button
88 * number or key number (whichever is applicable) set to true if the
89 * mouse or key had been depressed since the last call to
92 int GetAsyncKeyState(int nKey
)
98 retval
= AsyncMouseButtonsStates
[0] |
99 MouseButtonsStates
[0]? 0x0001: 0;
102 retval
= AsyncMouseButtonsStates
[1] |
103 MouseButtonsStates
[1]? 0x0001: 0;
106 retval
= AsyncMouseButtonsStates
[2] |
107 MouseButtonsStates
[2]? 0x0001: 0;
110 retval
= AsyncKeyStateTable
[nKey
] |
111 (InputKeyStateTable
[nKey
] ? 0x8000 : 0);
115 memset( AsyncMouseButtonsStates
, 0, 3 ); /* all states to false */
116 memset( AsyncKeyStateTable
, 0, 256 );
118 dprintf_key(stddeb
, "GetAsyncKeyState(%x) -> %x\n", nKey
, retval
);
123 /**********************************************************************
124 * TranslateAccelerator [USER.178]
126 * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP -messages
128 INT16
TranslateAccelerator(HWND hWnd
, HACCEL16 hAccel
, LPMSG16 msg
)
130 ACCELHEADER
*lpAccelTbl
;
134 if (hAccel
== 0 || msg
== NULL
) return 0;
135 if (msg
->message
!= WM_KEYDOWN
&&
136 msg
->message
!= WM_KEYUP
&&
137 msg
->message
!= WM_SYSKEYDOWN
&&
138 msg
->message
!= WM_SYSKEYUP
&&
139 msg
->message
!= WM_CHAR
) return 0;
141 dprintf_accel(stddeb
, "TranslateAccelerators hAccel=%04x, hWnd=%04x,\
142 msg->hwnd=%04x, msg->message=%04x\n", hAccel
,hWnd
,msg
->hwnd
,msg
->message
);
144 lpAccelTbl
= (LPACCELHEADER
)GlobalLock16(hAccel
);
145 for (sendmsg
= i
= 0; i
< lpAccelTbl
->wCount
; i
++)
147 if(msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
)
149 if (msg
->message
== WM_CHAR
)
151 if ( !(lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) &&
152 !(lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) )
154 dprintf_accel(stddeb
,"found accel for WM_CHAR: ('%c')",msg
->wParam
&0xff);
160 if(lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
)
163 dprintf_accel(stddeb
,"found accel for virt_key %04x (scan %04x)",
164 msg
->wParam
,0xff & HIWORD(msg
->lParam
));
165 if(GetKeyState(VK_SHIFT
) & 0x8000) mask
|= SHIFT_ACCEL
;
166 if(GetKeyState(VK_CONTROL
) & 0x8000) mask
|= CONTROL_ACCEL
;
167 if(GetKeyState(VK_MENU
) & 0x8000) mask
|= ALT_ACCEL
;
168 if(mask
== (lpAccelTbl
->tbl
[i
].type
&
169 (SHIFT_ACCEL
| CONTROL_ACCEL
| ALT_ACCEL
)))
172 dprintf_accel(stddeb
,", but incorrect SHIFT/CTRL/ALT-state\n");
176 if (!(msg
->lParam
& 0x01000000)) /* no special_key */
178 if ((lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) && (msg
->lParam
& 0x20000000))
179 { /* ^^ ALT pressed */
180 dprintf_accel(stddeb
,"found accel for Alt-%c", msg
->wParam
&0xff);
187 if (sendmsg
) /* found an accelerator, but send a message... ? */
189 INT16 iSysStat
,iStat
,mesg
=0;
190 HMENU16 hSysMenu
,hMenu
;
192 if (msg
->message
== WM_KEYUP
|| msg
->message
== WM_SYSKEYUP
)
198 if (!IsWindowEnabled(hWnd
))
203 hSysMenu
=GetSystemMenu(hWnd
,FALSE
);
205 iSysStat
=GetMenuState(hSysMenu
,lpAccelTbl
->tbl
[i
].wIDval
,MF_BYCOMMAND
);
209 iStat
=GetMenuState(hMenu
,lpAccelTbl
->tbl
[i
].wIDval
,MF_BYCOMMAND
);
214 if (iSysStat
& (MF_DISABLED
|MF_GRAYED
))
227 if (iStat
& (MF_DISABLED
|MF_GRAYED
))
237 if ( mesg
==WM_COMMAND
|| mesg
==WM_SYSCOMMAND
)
239 dprintf_accel(stddeb
,", sending %s, wParam=%0x\n",
240 mesg
==WM_COMMAND
? "WM_COMMAND" : "WM_SYSCOMMAND",
241 lpAccelTbl
->tbl
[i
].wIDval
);
242 SendMessage16(hWnd
, mesg
, lpAccelTbl
->tbl
[i
].wIDval
,0x00010000L
);
246 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
247 * #0: unknown (please report!)
248 * #1: for WM_KEYUP,WM_SYKEYUP
249 * #2: mouse is captured
250 * #3: window is disabled
251 * #4: it's a disabled system menu option
252 * #5: it's a menu option, but window is iconic
253 * #6: it's a menu option, but disabled
255 dprintf_accel(stddeb
,", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg
);
257 GlobalUnlock16(hAccel
);
262 GlobalUnlock16(hAccel
);