Release 970112
[wine.git] / windows / keyboard.c
blob1a418fd0ec193366348a603233c824bc0d44c2b5
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 "stddebug.h"
14 #include "debug.h"
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)
31 INT retval;
33 switch (vkey)
35 case VK_LBUTTON : /* VK_LBUTTON is 1 */
36 retval = MouseButtonsStates[0];
37 break;
38 case VK_MBUTTON : /* VK_MBUTTON is 4 */
39 retval = MouseButtonsStates[1];
40 break;
41 case VK_RBUTTON : /* VK_RBUTTON is 2 */
42 retval = MouseButtonsStates[2];
43 break;
44 default :
45 if (vkey >= 'a' && vkey <= 'z')
46 vkey += 'A' - 'a';
47 retval = ( (INT)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
48 (INT)(QueueKeyStateTable[vkey] & 0x01);
50 dprintf_key(stddeb, "GetKeyState(0x%x) -> %x\n", vkey, retval);
51 return 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
90 * been pressed.
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
96 * GetAsyncKeyState.
98 int GetAsyncKeyState(int nKey)
100 short retval;
102 switch (nKey) {
103 case VK_LBUTTON:
104 retval = AsyncMouseButtonsStates[0] |
105 MouseButtonsStates[0]? 0x0001: 0;
106 break;
107 case VK_MBUTTON:
108 retval = AsyncMouseButtonsStates[1] |
109 MouseButtonsStates[1]? 0x0001: 0;
110 break;
111 case VK_RBUTTON:
112 retval = AsyncMouseButtonsStates[2] |
113 MouseButtonsStates[2]? 0x0001: 0;
114 break;
115 default:
116 retval = AsyncKeyStateTable[nKey] |
117 (InputKeyStateTable[nKey] ? 0x8000 : 0);
118 break;
121 memset( AsyncMouseButtonsStates, 0, 3 ); /* all states to false */
122 memset( AsyncKeyStateTable, 0, 256 );
124 dprintf_key(stddeb, "GetAsyncKeyState(%x) -> %x\n", nKey, retval);
125 return 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;
137 int i;
138 BOOL sendmsg;
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);
161 sendmsg=TRUE;
164 else
166 if(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL)
168 INT mask = 0;
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)))
176 sendmsg=TRUE;
177 else
178 dprintf_accel(stddeb,", but incorrect SHIFT/CTRL/ALT-state\n");
180 else
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);
187 sendmsg=TRUE;
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)
199 mesg=1;
200 else
201 if (GetCapture16())
202 mesg=2;
203 else
204 if (!IsWindowEnabled(hWnd))
205 mesg=3;
206 else
208 hMenu=GetMenu32(hWnd);
209 hSysMenu=GetSystemMenu32(hWnd,FALSE);
210 if (hSysMenu)
211 iSysStat=GetMenuState32(hSysMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
212 else
213 iSysStat=-1;
214 if (hMenu)
215 iStat=GetMenuState32(hMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
216 else
217 iStat=-1;
218 if (iSysStat!=-1)
220 if (iSysStat & (MF_DISABLED|MF_GRAYED))
221 mesg=4;
222 else
223 mesg=WM_SYSCOMMAND;
225 else
227 if (iStat!=-1)
229 if (IsIconic(hWnd))
230 mesg=5;
231 else
233 if (iStat & (MF_DISABLED|MF_GRAYED))
234 mesg=6;
235 else
236 mesg=WM_COMMAND;
239 else
240 mesg=WM_COMMAND;
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);
250 else
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);
264 return 1;
268 GlobalUnlock16(hAccel);
269 return 0;