2 * Keyboard related functions
4 * Copyright 1993 Bob Amstadt
5 * Copyright 1996 Albrecht Kleine
16 extern BOOL MouseButtonsStates
[3];
17 extern BOOL AsyncMouseButtonsStates
[3];
18 extern BYTE InputKeyStateTable
[256];
19 BYTE AsyncKeyStateTable
[256];
21 extern BYTE QueueKeyStateTable
[256];
23 /**********************************************************************
24 * GetKeyState [USER.106]
25 * An application calls the GetKeyState function in response to a
26 * keyboard-input message. This function retrieves the state of the key
27 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
29 INT
GetKeyState(INT vkey
)
35 case VK_LBUTTON
: /* VK_LBUTTON is 1 */
36 retval
= MouseButtonsStates
[0];
38 case VK_MBUTTON
: /* VK_MBUTTON is 4 */
39 retval
= MouseButtonsStates
[1];
41 case VK_RBUTTON
: /* VK_RBUTTON is 2 */
42 retval
= MouseButtonsStates
[2];
45 if (vkey
>= 'a' && vkey
<= 'z')
47 retval
= ( (INT
)(QueueKeyStateTable
[vkey
] & 0x80) << 8 ) |
48 (INT
)(QueueKeyStateTable
[vkey
] & 0x01);
50 dprintf_key(stddeb
, "GetKeyState(0x%x) -> %x\n", vkey
, retval
);
54 /**********************************************************************
55 * GetKeyboardState [USER.222]
56 * An application calls the GetKeyboardState function in response to a
57 * keyboard-input message. This function retrieves the state of the keyboard
58 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
60 void GetKeyboardState(BYTE
*lpKeyState
)
62 dprintf_key(stddeb
, "GetKeyboardState()\n");
63 if (lpKeyState
!= NULL
) {
64 QueueKeyStateTable
[VK_LBUTTON
] = MouseButtonsStates
[0] >> 8;
65 QueueKeyStateTable
[VK_MBUTTON
] = MouseButtonsStates
[1] >> 8;
66 QueueKeyStateTable
[VK_RBUTTON
] = MouseButtonsStates
[2] >> 8;
67 memcpy(lpKeyState
, QueueKeyStateTable
, 256);
71 /**********************************************************************
72 * SetKeyboardState [USER.223]
74 void SetKeyboardState(BYTE
*lpKeyState
)
76 dprintf_key(stddeb
, "SetKeyboardState()\n");
77 if (lpKeyState
!= NULL
) {
78 memcpy(QueueKeyStateTable
, lpKeyState
, 256);
79 MouseButtonsStates
[0] = QueueKeyStateTable
[VK_LBUTTON
]? 0x8000: 0;
80 MouseButtonsStates
[1] = QueueKeyStateTable
[VK_MBUTTON
]? 0x8000: 0;
81 MouseButtonsStates
[2] = QueueKeyStateTable
[VK_RBUTTON
]? 0x8000: 0;
85 /**********************************************************************
86 * GetAsyncKeyState (USER.249)
88 * Determine if a key is or was pressed. retval has high-order
89 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
92 * This uses the variable AsyncMouseButtonsStates and
93 * AsyncKeyStateTable (set in event.c) which have the mouse button
94 * number or key number (whichever is applicable) set to true if the
95 * mouse or key had been depressed since the last call to
98 int GetAsyncKeyState(int nKey
)
104 retval
= AsyncMouseButtonsStates
[0] |
105 MouseButtonsStates
[0]? 0x0001: 0;
108 retval
= AsyncMouseButtonsStates
[1] |
109 MouseButtonsStates
[1]? 0x0001: 0;
112 retval
= AsyncMouseButtonsStates
[2] |
113 MouseButtonsStates
[2]? 0x0001: 0;
116 retval
= AsyncKeyStateTable
[nKey
] |
117 (InputKeyStateTable
[nKey
] ? 0x8000 : 0);
121 memset( AsyncMouseButtonsStates
, 0, 3 ); /* all states to false */
122 memset( AsyncKeyStateTable
, 0, 256 );
124 dprintf_key(stddeb
, "GetAsyncKeyState(%x) -> %x\n", nKey
, retval
);
129 /**********************************************************************
130 * TranslateAccelerator [USER.178]
132 * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP -messages
134 INT16
TranslateAccelerator(HWND hWnd
, HACCEL16 hAccel
, LPMSG16 msg
)
136 ACCELHEADER
*lpAccelTbl
;
140 if (hAccel
== 0 || msg
== NULL
) return 0;
141 if (msg
->message
!= WM_KEYDOWN
&&
142 msg
->message
!= WM_KEYUP
&&
143 msg
->message
!= WM_SYSKEYDOWN
&&
144 msg
->message
!= WM_SYSKEYUP
&&
145 msg
->message
!= WM_CHAR
) return 0;
147 dprintf_accel(stddeb
, "TranslateAccelerators hAccel=%04x, hWnd=%04x,\
148 msg->hwnd=%04x, msg->message=%04x\n", hAccel
,hWnd
,msg
->hwnd
,msg
->message
);
150 lpAccelTbl
= (LPACCELHEADER
)GlobalLock16(hAccel
);
151 for (sendmsg
= i
= 0; i
< lpAccelTbl
->wCount
; i
++)
153 if(msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
)
155 if (msg
->message
== WM_CHAR
)
157 if ( !(lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) &&
158 !(lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) )
160 dprintf_accel(stddeb
,"found accel for WM_CHAR: ('%c')",msg
->wParam
&0xff);
166 if(lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
)
169 dprintf_accel(stddeb
,"found accel for virt_key %04x (scan %04x)",
170 msg
->wParam
,0xff & HIWORD(msg
->lParam
));
171 if(GetKeyState(VK_SHIFT
) & 0x8000) mask
|= SHIFT_ACCEL
;
172 if(GetKeyState(VK_CONTROL
) & 0x8000) mask
|= CONTROL_ACCEL
;
173 if(GetKeyState(VK_MENU
) & 0x8000) mask
|= ALT_ACCEL
;
174 if(mask
== (lpAccelTbl
->tbl
[i
].type
&
175 (SHIFT_ACCEL
| CONTROL_ACCEL
| ALT_ACCEL
)))
178 dprintf_accel(stddeb
,", but incorrect SHIFT/CTRL/ALT-state\n");
182 if (!(msg
->lParam
& 0x01000000)) /* no special_key */
184 if ((lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) && (msg
->lParam
& 0x20000000))
185 { /* ^^ ALT pressed */
186 dprintf_accel(stddeb
,"found accel for Alt-%c", msg
->wParam
&0xff);
193 if (sendmsg
) /* found an accelerator, but send a message... ? */
195 INT16 iSysStat
,iStat
,mesg
=0;
196 HMENU16 hSysMenu
,hMenu
;
198 if (msg
->message
== WM_KEYUP
|| msg
->message
== WM_SYSKEYUP
)
204 if (!IsWindowEnabled(hWnd
))
208 hMenu
=GetMenu32(hWnd
);
209 hSysMenu
=GetSystemMenu32(hWnd
,FALSE
);
211 iSysStat
=GetMenuState32(hSysMenu
,lpAccelTbl
->tbl
[i
].wIDval
,MF_BYCOMMAND
);
215 iStat
=GetMenuState32(hMenu
,lpAccelTbl
->tbl
[i
].wIDval
,MF_BYCOMMAND
);
220 if (iSysStat
& (MF_DISABLED
|MF_GRAYED
))
233 if (iStat
& (MF_DISABLED
|MF_GRAYED
))
243 if ( mesg
==WM_COMMAND
|| mesg
==WM_SYSCOMMAND
)
245 dprintf_accel(stddeb
,", sending %s, wParam=%0x\n",
246 mesg
==WM_COMMAND
? "WM_COMMAND" : "WM_SYSCOMMAND",
247 lpAccelTbl
->tbl
[i
].wIDval
);
248 SendMessage16(hWnd
, mesg
, lpAccelTbl
->tbl
[i
].wIDval
,0x00010000L
);
252 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
253 * #0: unknown (please report!)
254 * #1: for WM_KEYUP,WM_SYSKEYUP
255 * #2: mouse is captured
256 * #3: window is disabled
257 * #4: it's a disabled system menu option
258 * #5: it's a menu option, but window is iconic
259 * #6: it's a menu option, but disabled
261 dprintf_accel(stddeb
,", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg
);
263 GlobalUnlock16(hAccel
);
268 GlobalUnlock16(hAccel
);