Release 961215
[wine.git] / windows / keyboard.c
blob0342cb9732dff36ed6576bd34548d7af825b9e04
1 /*
2 * Keyboard related functions
4 * Copyright 1993 Bob Amstadt
5 * Copyright 1996 Albrecht Kleine
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include "win.h"
11 #include "windows.h"
12 #include "accel.h"
13 #include "debug.h"
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)
30 INT retval;
32 if (keycode >= 'a' && keycode <= 'z')
33 keycode += 'A' - 'a';
34 switch(keycode) {
35 case VK_LBUTTON:
36 retval = MouseButtonsStates[0];
37 case VK_MBUTTON:
38 retval = MouseButtonsStates[1];
39 case VK_RBUTTON:
40 retval = MouseButtonsStates[2];
41 default:
42 retval = ( (INT)(QueueKeyStateTable[keycode] & 0x80) << 8 ) |
43 (INT)(QueueKeyStateTable[keycode] & 0x01);
46 dprintf_key(stddeb, "GetKeyState(%x) -> %x\n", keycode, retval);
47 return 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
84 * been pressed.
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
90 * GetAsyncKeyState.
92 int GetAsyncKeyState(int nKey)
94 short retval;
96 switch (nKey) {
97 case VK_LBUTTON:
98 retval = AsyncMouseButtonsStates[0] |
99 MouseButtonsStates[0]? 0x0001: 0;
100 break;
101 case VK_MBUTTON:
102 retval = AsyncMouseButtonsStates[1] |
103 MouseButtonsStates[1]? 0x0001: 0;
104 break;
105 case VK_RBUTTON:
106 retval = AsyncMouseButtonsStates[2] |
107 MouseButtonsStates[2]? 0x0001: 0;
108 break;
109 default:
110 retval = AsyncKeyStateTable[nKey] |
111 (InputKeyStateTable[nKey] ? 0x8000 : 0);
112 break;
115 memset( AsyncMouseButtonsStates, 0, 3 ); /* all states to false */
116 memset( AsyncKeyStateTable, 0, 256 );
118 dprintf_key(stddeb, "GetAsyncKeyState(%x) -> %x\n", nKey, retval);
119 return 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;
131 int i;
132 BOOL sendmsg;
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);
155 sendmsg=TRUE;
158 else
160 if(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL)
162 INT mask = 0;
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)))
170 sendmsg=TRUE;
171 else
172 dprintf_accel(stddeb,", but incorrect SHIFT/CTRL/ALT-state\n");
174 else
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);
181 sendmsg=TRUE;
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)
193 mesg=1;
194 else
195 if (GetCapture16())
196 mesg=2;
197 else
198 if (!IsWindowEnabled(hWnd))
199 mesg=3;
200 else
202 hMenu=GetMenu(hWnd);
203 hSysMenu=GetSystemMenu(hWnd,FALSE);
204 if (hSysMenu)
205 iSysStat=GetMenuState(hSysMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
206 else
207 iSysStat=-1;
208 if (hMenu)
209 iStat=GetMenuState(hMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
210 else
211 iStat=-1;
212 if (iSysStat!=-1)
214 if (iSysStat & (MF_DISABLED|MF_GRAYED))
215 mesg=4;
216 else
217 mesg=WM_SYSCOMMAND;
219 else
221 if (iStat!=-1)
223 if (IsIconic(hWnd))
224 mesg=5;
225 else
227 if (iStat & (MF_DISABLED|MF_GRAYED))
228 mesg=6;
229 else
230 mesg=WM_COMMAND;
233 else
234 mesg=WM_COMMAND;
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);
244 else
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);
258 return 1;
262 GlobalUnlock16(hAccel);
263 return 0;