- Add patch back after sync with force.
[reactos.git] / rostests / winetests / user32 / input.c
1 /* Test Key event to Key message translation
2 *
3 * Copyright 2003 Rein Klazes
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 /* test whether the right type of messages:
21 * WM_KEYUP/DOWN vs WM_SYSKEYUP/DOWN are sent in case of combined
22 * keystrokes.
23 *
24 * For instance <ALT>-X can be accomplished by
25 * the sequence ALT-KEY-DOWN, X-KEY-DOWN, ALT-KEY-UP, X-KEY-UP
26 * but also X-KEY-DOWN, ALT-KEY-DOWN, X-KEY-UP, ALT-KEY-UP
27 * Whether a KEY or a SYSKEY message is sent is not always clear, it is
28 * also not the same in WINNT as in WIN9X */
29
30 /* NOTE that there will be test failures under WIN9X
31 * No applications are known to me that rely on this
32 * so I don't fix it */
33
34 /* TODO:
35 * 1. extend it to the wm_command and wm_syscommand notifications
36 * 2. add some more tests with special cases like dead keys or right (alt) key
37 * 3. there is some adapted code from input.c in here. Should really
38 * make that code exactly the same.
39 * 4. resolve the win9x case when there is a need or the testing frame work
40 * offers a nice way.
41 * 5. The test app creates a window, the user should not take the focus
42 * away during its short existence. I could do something to prevent that
43 * if it is a problem.
44 *
45 */
46 #undef _WIN32_WINNT
47 #undef _WIN32_IE
48 #define _WIN32_IE 0x0500
49 #define _WIN32_WINNT 0x500
50
51 #include <stdarg.h>
52 #include <assert.h>
53
54 #include "windef.h"
55 #include "winbase.h"
56 #include "winuser.h"
57
58 #include "wine/test.h"
59
60 /* globals */
61 static HWND hWndTest;
62 static LONG timetag = 0x10000000;
63
64 static struct {
65 LONG last_key_down;
66 LONG last_key_up;
67 LONG last_syskey_down;
68 LONG last_syskey_up;
69 LONG last_char;
70 LONG last_syschar;
71 LONG last_hook_down;
72 LONG last_hook_up;
73 LONG last_hook_syskey_down;
74 LONG last_hook_syskey_up;
75 BOOL expect_alt;
76 BOOL sendinput_broken;
77 } key_status;
78
79 static UINT (WINAPI *pSendInput) (UINT, INPUT*, size_t);
80 static int (WINAPI *pGetMouseMovePointsEx) (UINT, LPMOUSEMOVEPOINT, LPMOUSEMOVEPOINT, int, DWORD);
81
82 #define MAXKEYEVENTS 12
83 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
84 and only one message */
85
86 /* keyboard message names, sorted as their value */
87 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
88 "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
89
90 /* keyevents, add more as needed */
91 typedef enum KEVtag
92 { ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
93 /* matching VK's */
94 static const int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
95 /* matching scan codes */
96 static const int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
97 /* matching updown events */
98 static const int GETFLAGS[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
99 /* matching descriptions */
100 static const char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
101
102 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define our own type */
103 typedef struct
104 {
105 DWORD type;
106 union
107 {
108 MOUSEINPUT mi;
109 KEYBDINPUT ki;
110 HARDWAREINPUT hi;
111 } u;
112 } TEST_INPUT;
113
114 #define ADDTOINPUTS(kev) \
115 inputs[evtctr].type = INPUT_KEYBOARD; \
116 ((TEST_INPUT*)inputs)[evtctr].u.ki.wVk = GETVKEY[ kev]; \
117 ((TEST_INPUT*)inputs)[evtctr].u.ki.wScan = GETSCAN[ kev]; \
118 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwFlags = GETFLAGS[ kev]; \
119 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwExtraInfo = 0; \
120 ((TEST_INPUT*)inputs)[evtctr].u.ki.time = ++timetag; \
121 if( kev) evtctr++;
122
123 typedef struct {
124 UINT message;
125 WPARAM wParam;
126 LPARAM lParam;
127 } KMSG;
128
129 /*******************************************
130 * add new test sets here
131 * the software will make all combinations of the
132 * keyevent defined here
133 */
134 static const struct {
135 int nrkev;
136 KEV keydwn[MAXKEYEVENTS];
137 KEV keyup[MAXKEYEVENTS];
138 } testkeyset[]= {
139 { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
140 { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
141 { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
142 { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
143 { 0 } /* mark the end */
144 };
145
146 /**********************adapted from input.c **********************************/
147
148 static BYTE InputKeyStateTable[256];
149 static BYTE AsyncKeyStateTable[256];
150 static BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
151 or a WM_KEYUP message */
152
153 static void init_function_pointers(void)
154 {
155 HMODULE hdll = GetModuleHandleA("user32");
156
157 #define GET_PROC(func) \
158 p ## func = (void*)GetProcAddress(hdll, #func); \
159 if(!p ## func) \
160 trace("GetProcAddress(%s) failed\n", #func);
161
162 GET_PROC(SendInput)
163 GET_PROC(GetMouseMovePointsEx)
164
165 #undef GET_PROC
166 }
167
168 static int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
169 {
170 UINT message;
171 int VKey = GETVKEY[kev];
172 WORD flags;
173
174 flags = LOBYTE(GETSCAN[kev]);
175 if (GETFLAGS[kev] & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
176
177 if (GETFLAGS[kev] & KEYEVENTF_KEYUP )
178 {
179 message = WM_KEYUP;
180 if( (InputKeyStateTable[VK_MENU] & 0x80) && (
181 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
182 !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
183 if( TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
184 (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
185 message = WM_SYSKEYUP;
186 TrackSysKey = 0;
187 }
188 InputKeyStateTable[VKey] &= ~0x80;
189 flags |= KF_REPEAT | KF_UP;
190 }
191 else
192 {
193 if (InputKeyStateTable[VKey] & 0x80) flags |= KF_REPEAT;
194 if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
195 InputKeyStateTable[VKey] |= 0x80;
196 AsyncKeyStateTable[VKey] |= 0x80;
197
198 message = WM_KEYDOWN;
199 if( (InputKeyStateTable[VK_MENU] & 0x80) &&
200 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
201 message = WM_SYSKEYDOWN;
202 TrackSysKey = VKey;
203 }
204 }
205
206 if (InputKeyStateTable[VK_MENU] & 0x80) flags |= KF_ALTDOWN;
207
208 if( plParam) *plParam = MAKELPARAM( 1, flags );
209 if( pwParam) *pwParam = VKey;
210 return message;
211 }
212
213 /****************************** end copy input.c ****************************/
214
215 /*
216 * . prepare the keyevents for SendInputs
217 * . calculate the "expected" messages
218 * . Send the events to our window
219 * . retrieve the messages from the input queue
220 * . verify
221 */
222 static BOOL do_test( HWND hwnd, int seqnr, const KEV td[] )
223 {
224 INPUT inputs[MAXKEYEVENTS];
225 KMSG expmsg[MAXKEYEVENTS];
226 MSG msg;
227 char buf[100];
228 UINT evtctr=0;
229 int kmctr, i;
230
231 buf[0]='\0';
232 TrackSysKey=0; /* see input.c */
233 for( i = 0; i < MAXKEYEVENTS; i++) {
234 ADDTOINPUTS(td[i])
235 strcat(buf, getdesc[td[i]]);
236 if(td[i])
237 expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
238 else
239 expmsg[i].message = 0;
240 }
241 for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
242 ;
243 assert( evtctr <= MAXKEYEVENTS );
244 assert( evtctr == pSendInput(evtctr, &inputs[0], sizeof(INPUT)));
245 i = 0;
246 if (winetest_debug > 1)
247 trace("======== key stroke sequence #%d: %s =============\n",
248 seqnr + 1, buf);
249 while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
250 if (winetest_debug > 1)
251 trace("message[%d] %-15s wParam %04lx lParam %08lx time %x\n", i,
252 MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
253 if( i < kmctr ) {
254 ok( msg.message == expmsg[i].message &&
255 msg.wParam == expmsg[i].wParam &&
256 msg.lParam == expmsg[i].lParam,
257 "%u/%u: wrong message %x/%08lx/%08lx expected %s/%08lx/%08lx\n",
258 seqnr, i, msg.message, msg.wParam, msg.lParam,
259 MSGNAME[(expmsg[i]).message - WM_KEYFIRST], expmsg[i].wParam, expmsg[i].lParam );
260 }
261 i++;
262 }
263 if (winetest_debug > 1)
264 trace("%d messages retrieved\n", i);
265 if (!i && kmctr)
266 {
267 skip( "simulated keyboard input doesn't work\n" );
268 return FALSE;
269 }
270 ok( i == kmctr, "message count is wrong: got %d expected: %d\n", i, kmctr);
271 return TRUE;
272 }
273
274 /* test all combinations of the specified key events */
275 static BOOL TestASet( HWND hWnd, int nrkev, const KEV kevdwn[], const KEV kevup[] )
276 {
277 int i,j,k,l,m,n;
278 static int count=0;
279 KEV kbuf[MAXKEYEVENTS];
280 assert( nrkev==2 || nrkev==3);
281 for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
282 /* two keys involved gives 4 test cases */
283 if(nrkev==2) {
284 for(i=0;i<nrkev;i++) {
285 for(j=0;j<nrkev;j++) {
286 kbuf[0] = kevdwn[i];
287 kbuf[1] = kevdwn[1-i];
288 kbuf[2] = kevup[j];
289 kbuf[3] = kevup[1-j];
290 if (!do_test( hWnd, count++, kbuf)) return FALSE;
291 }
292 }
293 }
294 /* three keys involved gives 36 test cases */
295 if(nrkev==3){
296 for(i=0;i<nrkev;i++){
297 for(j=0;j<nrkev;j++){
298 if(j==i) continue;
299 for(k=0;k<nrkev;k++){
300 if(k==i || k==j) continue;
301 for(l=0;l<nrkev;l++){
302 for(m=0;m<nrkev;m++){
303 if(m==l) continue;
304 for(n=0;n<nrkev;n++){
305 if(n==l ||n==m) continue;
306 kbuf[0] = kevdwn[i];
307 kbuf[1] = kevdwn[j];
308 kbuf[2] = kevdwn[k];
309 kbuf[3] = kevup[l];
310 kbuf[4] = kevup[m];
311 kbuf[5] = kevup[n];
312 if (!do_test( hWnd, count++, kbuf)) return FALSE;
313 }
314 }
315 }
316 }
317 }
318 }
319 }
320 return TRUE;
321 }
322
323 /* test each set specified in the global testkeyset array */
324 static void TestSysKeys( HWND hWnd)
325 {
326 int i;
327 for(i=0; testkeyset[i].nrkev;i++)
328 if (!TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn, testkeyset[i].keyup)) break;
329 }
330
331 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
332 LPARAM lParam )
333 {
334 return DefWindowProcA( hWnd, msg, wParam, lParam );
335 }
336
337 static void test_Input_whitebox(void)
338 {
339 MSG msg;
340 WNDCLASSA wclass;
341 HANDLE hInstance = GetModuleHandleA( NULL );
342
343 wclass.lpszClassName = "InputSysKeyTestClass";
344 wclass.style = CS_HREDRAW | CS_VREDRAW;
345 wclass.lpfnWndProc = WndProc;
346 wclass.hInstance = hInstance;
347 wclass.hIcon = LoadIconA( 0, IDI_APPLICATION );
348 wclass.hCursor = LoadCursorA( NULL, IDC_ARROW );
349 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
350 wclass.lpszMenuName = 0;
351 wclass.cbClsExtra = 0;
352 wclass.cbWndExtra = 0;
353 RegisterClassA( &wclass );
354 /* create the test window that will receive the keystrokes */
355 hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
356 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
357 NULL, NULL, hInstance, NULL);
358 assert( hWndTest );
359 ShowWindow( hWndTest, SW_SHOW);
360 SetWindowPos( hWndTest, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
361 SetForegroundWindow( hWndTest );
362 UpdateWindow( hWndTest);
363
364 /* flush pending messages */
365 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
366
367 SetFocus( hWndTest );
368 TestSysKeys( hWndTest );
369 DestroyWindow(hWndTest);
370 }
371
372 /* try to make sure pending X events have been processed before continuing */
373 static void empty_message_queue(void)
374 {
375 MSG msg;
376 int diff = 200;
377 int min_timeout = 50;
378 DWORD time = GetTickCount() + diff;
379
380 while (diff > 0)
381 {
382 if (MsgWaitForMultipleObjects(0, NULL, FALSE, min_timeout, QS_ALLINPUT) == WAIT_TIMEOUT) break;
383 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
384 {
385 TranslateMessage(&msg);
386 DispatchMessage(&msg);
387 }
388 diff = time - GetTickCount();
389 }
390 }
391
392 struct transition_s {
393 WORD wVk;
394 BYTE before_state;
395 BYTE optional;
396 };
397
398 typedef enum {
399 sent=0x1,
400 posted=0x2,
401 parent=0x4,
402 wparam=0x8,
403 lparam=0x10,
404 defwinproc=0x20,
405 beginpaint=0x40,
406 optional=0x80,
407 hook=0x100,
408 winevent_hook=0x200
409 } msg_flags_t;
410
411 struct message {
412 UINT message; /* the WM_* code */
413 msg_flags_t flags; /* message props */
414 WPARAM wParam; /* expected value of wParam */
415 LPARAM lParam; /* expected value of lParam */
416 };
417
418 struct sendinput_test_s {
419 WORD wVk;
420 DWORD dwFlags;
421 BOOL _todo_wine;
422 struct transition_s expected_transitions[MAXKEYEVENTS+1];
423 struct message expected_messages[MAXKEYMESSAGES+1];
424 } sendinput_test[] = {
425 /* test ALT+F */
426 /* 0 */
427 {VK_LMENU, 0, 0, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
428 {{WM_SYSKEYDOWN, hook|wparam, VK_LMENU}, {WM_SYSKEYDOWN}, {0}}},
429 {'F', 0, 0, {{'F', 0x00}, {0}},
430 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN},
431 {WM_SYSCHAR},
432 {WM_SYSCOMMAND}, {0}}},
433 {'F', KEYEVENTF_KEYUP, 0, {{'F', 0x80}, {0}},
434 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
435 {VK_LMENU, KEYEVENTF_KEYUP, 0, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
436 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
437
438 /* test CTRL+O */
439 /* 4 */
440 {VK_LCONTROL, 0, 0, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
441 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
442 {'O', 0, 0, {{'O', 0x00}, {0}},
443 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
444 {'O', KEYEVENTF_KEYUP, 0, {{'O', 0x80}, {0}},
445 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
446 {VK_LCONTROL, KEYEVENTF_KEYUP, 0, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
447 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
448
449 /* test ALT+CTRL+X */
450 /* 8 */
451 {VK_LMENU, 0, 0, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
452 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN}, {0}}},
453 {VK_LCONTROL, 0, 0, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
454 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
455 {'X', 0, 0, {{'X', 0x00}, {0}},
456 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
457 {'X', KEYEVENTF_KEYUP, 0, {{'X', 0x80}, {0}},
458 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
459 {VK_LCONTROL, KEYEVENTF_KEYUP, 0, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
460 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
461 {VK_LMENU, KEYEVENTF_KEYUP, 0, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
462 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
463
464 /* test SHIFT+A */
465 /* 14 */
466 {VK_LSHIFT, 0, 0, {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
467 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
468 {'A', 0, 0, {{'A', 0x00}, {0}},
469 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
470 {'A', KEYEVENTF_KEYUP, 0, {{'A', 0x80}, {0}},
471 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
472 {VK_LSHIFT, KEYEVENTF_KEYUP, 0, {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
473 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
474 /* test L-SHIFT & R-SHIFT: */
475 /* RSHIFT == LSHIFT */
476 /* 18 */
477 {VK_RSHIFT, 0, 0,
478 /* recent windows versions (>= w2k3) correctly report an RSHIFT transition */
479 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00, TRUE}, {VK_RSHIFT, 0x00, TRUE}, {0}},
480 {{WM_KEYDOWN, hook|wparam, VK_RSHIFT},
481 {WM_KEYDOWN}, {0}}},
482 {VK_RSHIFT, KEYEVENTF_KEYUP, 0,
483 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80, TRUE}, {VK_RSHIFT, 0x80, TRUE}, {0}},
484 {{WM_KEYUP, hook, hook|wparam, VK_RSHIFT},
485 {WM_KEYUP}, {0}}},
486
487 /* LSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
488 /* 20 */
489 {VK_LSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
490 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
491 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, LLKHF_EXTENDED},
492 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
493 {VK_LSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
494 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
495 {{WM_KEYUP, hook|wparam|lparam, VK_LSHIFT, LLKHF_UP|LLKHF_EXTENDED},
496 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
497 /* RSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
498 /* 22 */
499 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
500 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
501 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
502 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
503 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
504 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
505 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
506 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
507
508 /* Note about wparam for hook with generic key (VK_SHIFT, VK_CONTROL, VK_MENU):
509 win2k - sends to hook whatever we generated here
510 winXP+ - Attempts to convert key to L/R key but not always correct
511 */
512 /* SHIFT == LSHIFT */
513 /* 24 */
514 {VK_SHIFT, 0, 0,
515 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
516 {{WM_KEYDOWN, hook/* |wparam */|lparam, VK_SHIFT, 0},
517 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
518 {VK_SHIFT, KEYEVENTF_KEYUP, 0,
519 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
520 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_UP},
521 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
522 /* SHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
523 /* 26 */
524 {VK_SHIFT, KEYEVENTF_EXTENDEDKEY, 0,
525 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
526 {{WM_KEYDOWN, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_EXTENDED},
527 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
528 {VK_SHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
529 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
530 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_UP|LLKHF_EXTENDED},
531 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
532
533 /* test L-CONTROL & R-CONTROL: */
534 /* RCONTROL == LCONTROL */
535 /* 28 */
536 {VK_RCONTROL, 0, 0,
537 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
538 {{WM_KEYDOWN, hook|wparam, VK_RCONTROL},
539 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
540 {VK_RCONTROL, KEYEVENTF_KEYUP, 0,
541 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
542 {{WM_KEYUP, hook|wparam, VK_RCONTROL},
543 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
544 /* LCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
545 /* 30 */
546 {VK_LCONTROL, KEYEVENTF_EXTENDEDKEY, 0,
547 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
548 {{WM_KEYDOWN, hook|wparam|lparam, VK_LCONTROL, LLKHF_EXTENDED},
549 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
550 {VK_LCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
551 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
552 {{WM_KEYUP, hook|wparam|lparam, VK_LCONTROL, LLKHF_UP|LLKHF_EXTENDED},
553 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
554 /* RCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
555 /* 32 */
556 {VK_RCONTROL, KEYEVENTF_EXTENDEDKEY, 0,
557 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
558 {{WM_KEYDOWN, hook|wparam|lparam, VK_RCONTROL, LLKHF_EXTENDED},
559 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
560 {VK_RCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
561 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
562 {{WM_KEYUP, hook|wparam|lparam, VK_RCONTROL, LLKHF_UP|LLKHF_EXTENDED},
563 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
564 /* CONTROL == LCONTROL */
565 /* 34 */
566 {VK_CONTROL, 0, 0,
567 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
568 {{WM_KEYDOWN, hook/*|wparam, VK_CONTROL*/},
569 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
570 {VK_CONTROL, KEYEVENTF_KEYUP, 0,
571 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
572 {{WM_KEYUP, hook/*|wparam, VK_CONTROL*/},
573 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
574 /* CONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
575 /* 36 */
576 {VK_CONTROL, KEYEVENTF_EXTENDEDKEY, 0,
577 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
578 {{WM_KEYDOWN, hook/*|wparam*/|lparam, VK_CONTROL, LLKHF_EXTENDED},
579 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
580 {VK_CONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
581 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
582 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_CONTROL, LLKHF_UP|LLKHF_EXTENDED},
583 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
584
585 /* test L-MENU & R-MENU: */
586 /* RMENU == LMENU */
587 /* 38 */
588 {VK_RMENU, 0, 0,
589 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
590 {{WM_SYSKEYDOWN, hook|wparam|optional, VK_LCONTROL},
591 {WM_SYSKEYDOWN, hook|wparam, VK_RMENU},
592 {WM_KEYDOWN, wparam|lparam|optional, VK_CONTROL, 0},
593 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
594 {VK_RMENU, KEYEVENTF_KEYUP, 1,
595 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
596 {{WM_KEYUP, hook|wparam|optional, VK_LCONTROL},
597 {WM_KEYUP, hook|wparam, VK_RMENU},
598 {WM_SYSKEYUP, wparam|lparam|optional, VK_CONTROL, KF_UP},
599 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
600 {WM_SYSCOMMAND, optional}, {0}}},
601 /* LMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
602 /* 40 */
603 {VK_LMENU, KEYEVENTF_EXTENDEDKEY, 0,
604 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {0}},
605 {{WM_SYSKEYDOWN, hook|wparam|lparam, VK_LMENU, LLKHF_EXTENDED},
606 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
607 {VK_LMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
608 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {0}},
609 {{WM_KEYUP, hook|wparam|lparam, VK_LMENU, LLKHF_UP|LLKHF_EXTENDED},
610 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
611 {WM_SYSCOMMAND}, {0}}},
612 /* RMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
613 /* 42 */
614 {VK_RMENU, KEYEVENTF_EXTENDEDKEY, 0,
615 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
616 {{WM_SYSKEYDOWN, hook|wparam|lparam|optional, VK_LCONTROL, 0},
617 {WM_SYSKEYDOWN, hook|wparam|lparam, VK_RMENU, LLKHF_EXTENDED},
618 {WM_KEYDOWN, wparam|lparam|optional, VK_CONTROL, 0},
619 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
620 {VK_RMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
621 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
622 {{WM_KEYUP, hook|wparam|lparam|optional, VK_LCONTROL, LLKHF_UP},
623 {WM_KEYUP, hook|wparam|lparam, VK_RMENU, LLKHF_UP|LLKHF_EXTENDED},
624 {WM_SYSKEYUP, wparam|lparam|optional, VK_CONTROL, KF_UP},
625 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
626 {WM_SYSCOMMAND, optional}, {0}}},
627 /* MENU == LMENU */
628 /* 44 */
629 {VK_MENU, 0, 0,
630 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
631 {{WM_SYSKEYDOWN, hook/*|wparam, VK_MENU*/},
632 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
633 {VK_MENU, KEYEVENTF_KEYUP, 1,
634 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
635 {{WM_KEYUP, hook/*|wparam, VK_MENU*/},
636 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
637 {WM_SYSCOMMAND}, {0}}},
638 /* MENU | KEYEVENTF_EXTENDEDKEY == RMENU */
639 /* 46 */
640 {VK_MENU, KEYEVENTF_EXTENDEDKEY, 0,
641 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
642 {{WM_SYSKEYDOWN, hook|wparam|lparam|optional, VK_CONTROL, 0},
643 {WM_SYSKEYDOWN, hook/*|wparam*/|lparam, VK_MENU, LLKHF_EXTENDED},
644 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
645 {VK_MENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
646 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
647 {{WM_KEYUP, hook|wparam|lparam|optional, VK_CONTROL, LLKHF_UP},
648 {WM_KEYUP, hook/*|wparam*/|lparam, VK_MENU, LLKHF_UP|LLKHF_EXTENDED},
649 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
650 {WM_SYSCOMMAND}, {0}}},
651
652 /* test LSHIFT & RSHIFT */
653 /* 48 */
654 {VK_LSHIFT, 0, 0,
655 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
656 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, 0},
657 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
658 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
659 {{VK_RSHIFT, 0x00}, {0}},
660 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
661 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
662 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
663 {{VK_RSHIFT, 0x80}, {0}},
664 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
665 {WM_KEYUP, optional}, {0}}},
666 {VK_LSHIFT, KEYEVENTF_KEYUP, 0,
667 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
668 {{WM_KEYUP, hook|wparam, VK_LSHIFT},
669 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
670
671 {0, 0, 0, {{0}}, {{0}}} /* end */
672 };
673
674 static struct message sent_messages[MAXKEYMESSAGES];
675 static UINT sent_messages_cnt;
676
677 /* Verify that only specified key state transitions occur */
678 static void compare_and_check(int id, BYTE *ks1, BYTE *ks2, struct sendinput_test_s *test)
679 {
680 int i, failcount = 0;
681 struct transition_s *t = test->expected_transitions;
682 UINT actual_cnt = 0;
683 const struct message *expected = test->expected_messages;
684
685 while (t->wVk) {
686 int matched = ((ks1[t->wVk]&0x80) == (t->before_state&0x80)
687 && (ks2[t->wVk]&0x80) == (~t->before_state&0x80));
688
689 if (!matched && !t->optional && test->_todo_wine)
690 {
691 failcount++;
692 todo_wine {
693 ok(matched, "%2d (%x/%x): %02x from %02x -> %02x "
694 "instead of %02x -> %02x\n", id, test->wVk, test->dwFlags,
695 t->wVk, ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
696 ~t->before_state&0x80);
697 }
698 } else {
699 ok(matched || t->optional, "%2d (%x/%x): %02x from %02x -> %02x "
700 "instead of %02x -> %02x\n", id, test->wVk, test->dwFlags,
701 t->wVk, ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
702 ~t->before_state&0x80);
703 }
704 ks2[t->wVk] = ks1[t->wVk]; /* clear the match */
705 t++;
706 }
707 for (i = 0; i < 256; i++)
708 if (ks2[i] != ks1[i] && test->_todo_wine)
709 {
710 failcount++;
711 todo_wine
712 ok(FALSE, "%2d (%x/%x): %02x from %02x -> %02x unexpected\n",
713 id, test->wVk, test->dwFlags, i, ks1[i], ks2[i]);
714 }
715 else
716 ok(ks2[i] == ks1[i], "%2d (%x/%x): %02x from %02x -> %02x unexpected\n",
717 id, test->wVk, test->dwFlags, i, ks1[i], ks2[i]);
718
719 while (expected->message && actual_cnt < sent_messages_cnt)
720 {
721 const struct message *actual = &sent_messages[actual_cnt];
722
723 if (expected->message == actual->message)
724 {
725 if (expected->flags & wparam)
726 {
727 if ((expected->flags & optional) && (expected->wParam != actual->wParam))
728 {
729 expected++;
730 continue;
731 }
732 if (expected->wParam != actual->wParam && test->_todo_wine)
733 {
734 failcount++;
735 todo_wine
736 ok(FALSE, "%2d (%x/%x): in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
737 id, test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
738 }
739 else
740 ok(expected->wParam == actual->wParam,
741 "%2d (%x/%x): in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
742 id, test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
743 }
744 if (expected->flags & lparam)
745 {
746 if (expected->lParam != actual->lParam && test->_todo_wine)
747 {
748 failcount++;
749 todo_wine
750 ok(FALSE, "%2d (%x/%x): in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
751 id, test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
752 }
753 else
754 ok(expected->lParam == actual->lParam,
755 "%2d (%x/%x): in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
756 id, test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
757 }
758 ok((expected->flags & hook) == (actual->flags & hook),
759 "%2d (%x/%x): the msg 0x%04x should have been sent by a hook\n",
760 id, test->wVk, test->dwFlags, expected->message);
761
762 }
763 else if (expected->flags & optional)
764 {
765 expected++;
766 continue;
767 }
768 /* NT4 doesn't send SYSKEYDOWN/UP to hooks, only KEYDOWN/UP */
769 else if ((expected->flags & hook) &&
770 (expected->message == WM_SYSKEYDOWN || expected->message == WM_SYSKEYUP) &&
771 (actual->message == expected->message - 4))
772 {
773 ok((expected->flags & hook) == (actual->flags & hook),
774 "%2d (%x/%x): the msg 0x%04x should have been sent by a hook\n",
775 id, test->wVk, test->dwFlags, expected->message);
776 }
777 /* For VK_RMENU, at least localized Win2k/XP sends KEYDOWN/UP
778 * instead of SYSKEYDOWN/UP to the WNDPROC */
779 else if (test->wVk == VK_RMENU && !(expected->flags & hook) &&
780 (expected->message == WM_SYSKEYDOWN || expected->message == WM_SYSKEYUP) &&
781 (actual->message == expected->message - 4))
782 {
783 ok(expected->wParam == actual->wParam && expected->lParam == actual->lParam,
784 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
785 id, test->wVk, test->dwFlags, expected->message, actual->message);
786 }
787 else if (test->_todo_wine)
788 {
789 failcount++;
790 todo_wine
791 ok(FALSE,
792 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
793 id, test->wVk, test->dwFlags, expected->message, actual->message);
794 }
795 else
796 ok(FALSE,
797 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
798 id, test->wVk, test->dwFlags, expected->message, actual->message);
799
800 actual_cnt++;
801 expected++;
802 }
803 /* skip all optional trailing messages */
804 while (expected->message && (expected->flags & optional))
805 expected++;
806
807
808 if (expected->message || actual_cnt < sent_messages_cnt)
809 {
810 if (test->_todo_wine)
811 {
812 failcount++;
813 todo_wine
814 ok(FALSE, "%2d (%x/%x): the msg sequence is not complete: expected %04x - actual %04x\n",
815 id, test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
816 }
817 else
818 ok(FALSE, "%2d (%x/%x): the msg sequence is not complete: expected %04x - actual %04x\n",
819 id, test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
820 }
821
822 if( test->_todo_wine && !failcount) /* succeeded yet marked todo */
823 todo_wine
824 ok(TRUE, "%2d (%x/%x): marked \"todo_wine\" but succeeds\n", id, test->wVk, test->dwFlags);
825
826 sent_messages_cnt = 0;
827 }
828
829 /* WndProc2 checks that we get at least the messages specified */
830 static LRESULT CALLBACK WndProc2(HWND hWnd, UINT Msg, WPARAM wParam,
831 LPARAM lParam)
832 {
833 if (winetest_debug > 1) trace("MSG: %8x W:%8lx L:%8lx\n", Msg, wParam, lParam);
834
835 if (Msg != WM_PAINT &&
836 Msg != WM_NCPAINT &&
837 Msg != WM_SYNCPAINT &&
838 Msg != WM_ERASEBKGND &&
839 Msg != WM_NCHITTEST &&
840 Msg != WM_GETTEXT &&
841 Msg != WM_GETICON &&
842 Msg != WM_IME_SELECT &&
843 Msg != WM_DEVICECHANGE &&
844 Msg != WM_TIMECHANGE)
845 {
846 ok(sent_messages_cnt < MAXKEYMESSAGES, "Too many messages\n");
847 if (sent_messages_cnt < MAXKEYMESSAGES)
848 {
849 sent_messages[sent_messages_cnt].message = Msg;
850 sent_messages[sent_messages_cnt].flags = 0;
851 sent_messages[sent_messages_cnt].wParam = wParam;
852 sent_messages[sent_messages_cnt++].lParam = HIWORD(lParam) & (KF_UP|KF_EXTENDED);
853 }
854 }
855 return DefWindowProc(hWnd, Msg, wParam, lParam);
856 }
857
858 static LRESULT CALLBACK hook_proc(int code, WPARAM wparam, LPARAM lparam)
859 {
860 KBDLLHOOKSTRUCT *hook_info = (KBDLLHOOKSTRUCT *)lparam;
861
862 if (code == HC_ACTION)
863 {
864 ok(sent_messages_cnt < MAXKEYMESSAGES, "Too many messages\n");
865 if (sent_messages_cnt < MAXKEYMESSAGES)
866 {
867 sent_messages[sent_messages_cnt].message = wparam;
868 sent_messages[sent_messages_cnt].flags = hook;
869 sent_messages[sent_messages_cnt].wParam = hook_info->vkCode;
870 sent_messages[sent_messages_cnt++].lParam = hook_info->flags & (LLKHF_UP|LLKHF_EXTENDED);
871 }
872
873 if(0) /* For some reason not stable on Wine */
874 {
875 if (wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN)
876 ok(!(GetAsyncKeyState(hook_info->vkCode) & 0x8000), "key %x should be up\n", hook_info->vkCode);
877 else if (wparam == WM_KEYUP || wparam == WM_SYSKEYUP)
878 ok(GetAsyncKeyState(hook_info->vkCode) & 0x8000, "key %x should be down\n", hook_info->vkCode);
879 }
880
881 if (winetest_debug > 1)
882 trace("Hook: w=%lx vk:%8x sc:%8x fl:%8x %lx\n", wparam,
883 hook_info->vkCode, hook_info->scanCode, hook_info->flags, hook_info->dwExtraInfo);
884 }
885 return CallNextHookEx( 0, code, wparam, lparam );
886 }
887 static void test_Input_blackbox(void)
888 {
889 TEST_INPUT i;
890 int ii;
891 BYTE ks1[256], ks2[256];
892 LONG_PTR prevWndProc;
893 HWND window;
894 HHOOK hook;
895
896 if (GetKeyboardLayout(0) != (HKL)(ULONG_PTR)0x04090409)
897 {
898 skip("Skipping Input_blackbox test on non-US keyboard\n");
899 return;
900 }
901 window = CreateWindow("Static", NULL, WS_POPUP|WS_HSCROLL|WS_VSCROLL
902 |WS_VISIBLE, 0, 0, 200, 60, NULL, NULL,
903 NULL, NULL);
904 ok(window != NULL, "error: %d\n", (int) GetLastError());
905 SetWindowPos( window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
906 SetForegroundWindow( window );
907
908 if (!(hook = SetWindowsHookExA(WH_KEYBOARD_LL, hook_proc, GetModuleHandleA( NULL ), 0)))
909 {
910 DestroyWindow(window);
911 win_skip("WH_KEYBOARD_LL is not supported\n");
912 return;
913 }
914
915 /* must process all initial messages, otherwise X11DRV_KeymapNotify unsets
916 * key state set by SendInput(). */
917 empty_message_queue();
918
919 prevWndProc = SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR) WndProc2);
920 ok(prevWndProc != 0 || (prevWndProc == 0 && GetLastError() == 0),
921 "error: %d\n", (int) GetLastError());
922
923 i.type = INPUT_KEYBOARD;
924 i.u.ki.time = 0;
925 i.u.ki.dwExtraInfo = 0;
926
927 for (ii = 0; ii < sizeof(sendinput_test)/sizeof(struct sendinput_test_s)-1;
928 ii++) {
929 GetKeyboardState(ks1);
930 i.u.ki.wScan = ii+1 /* useful for debugging */;
931 i.u.ki.dwFlags = sendinput_test[ii].dwFlags;
932 i.u.ki.wVk = sendinput_test[ii].wVk;
933 pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
934 empty_message_queue();
935 GetKeyboardState(ks2);
936 if (!ii && sent_messages_cnt <= 1 && !memcmp( ks1, ks2, sizeof(ks1) ))
937 {
938 win_skip( "window doesn't receive the queued input\n" );
939 /* release the key */
940 i.u.ki.dwFlags |= KEYEVENTF_KEYUP;
941 pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
942 break;
943 }
944 compare_and_check(ii, ks1, ks2, &sendinput_test[ii]);
945 }
946
947 empty_message_queue();
948 DestroyWindow(window);
949 UnhookWindowsHookEx(hook);
950 }
951
952 static void reset_key_status(void)
953 {
954 key_status.last_key_down = -1;
955 key_status.last_key_up = -1;
956 key_status.last_syskey_down = -1;
957 key_status.last_syskey_up = -1;
958 key_status.last_char = -1;
959 key_status.last_syschar = -1;
960 key_status.last_hook_down = -1;
961 key_status.last_hook_up = -1;
962 key_status.last_hook_syskey_down = -1;
963 key_status.last_hook_syskey_up = -1;
964 key_status.expect_alt = FALSE;
965 key_status.sendinput_broken = FALSE;
966 }
967
968 static void test_unicode_keys(HWND hwnd, HHOOK hook)
969 {
970 TEST_INPUT inputs[2];
971 MSG msg;
972
973 /* init input data that never changes */
974 inputs[1].type = inputs[0].type = INPUT_KEYBOARD;
975 inputs[1].u.ki.dwExtraInfo = inputs[0].u.ki.dwExtraInfo = 0;
976 inputs[1].u.ki.time = inputs[0].u.ki.time = 0;
977
978 /* pressing & releasing a single unicode character */
979 inputs[0].u.ki.wVk = 0;
980 inputs[0].u.ki.wScan = 0x3c0;
981 inputs[0].u.ki.dwFlags = KEYEVENTF_UNICODE;
982
983 reset_key_status();
984 pSendInput(1, (INPUT*)inputs, sizeof(INPUT));
985 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
986 if(msg.message == WM_KEYDOWN && msg.wParam == VK_PACKET){
987 TranslateMessage(&msg);
988 }
989 DispatchMessageW(&msg);
990 }
991 if(!key_status.sendinput_broken){
992 ok(key_status.last_key_down == VK_PACKET,
993 "Last keydown msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_key_down);
994 ok(key_status.last_char == 0x3c0,
995 "Last char msg wparam should have been 0x3c0 (was: 0x%x)\n", key_status.last_char);
996 if(hook)
997 ok(key_status.last_hook_down == 0x3c0,
998 "Last hookdown msg should have been 0x3c0, was: 0x%x\n", key_status.last_hook_down);
999 }
1000
1001 inputs[1].u.ki.wVk = 0;
1002 inputs[1].u.ki.wScan = 0x3c0;
1003 inputs[1].u.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
1004
1005 reset_key_status();
1006 pSendInput(1, (INPUT*)(inputs+1), sizeof(INPUT));
1007 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
1008 if(msg.message == WM_KEYDOWN && msg.wParam == VK_PACKET){
1009 TranslateMessage(&msg);
1010 }
1011 DispatchMessageW(&msg);
1012 }
1013 if(!key_status.sendinput_broken){
1014 ok(key_status.last_key_up == VK_PACKET,
1015 "Last keyup msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_key_up);
1016 if(hook)
1017 ok(key_status.last_hook_up == 0x3c0,
1018 "Last hookup msg should have been 0x3c0, was: 0x%x\n", key_status.last_hook_up);
1019 }
1020
1021 /* holding alt, pressing & releasing a unicode character, releasing alt */
1022 inputs[0].u.ki.wVk = VK_LMENU;
1023 inputs[0].u.ki.wScan = 0;
1024 inputs[0].u.ki.dwFlags = 0;
1025
1026 inputs[1].u.ki.wVk = 0;
1027 inputs[1].u.ki.wScan = 0x3041;
1028 inputs[1].u.ki.dwFlags = KEYEVENTF_UNICODE;
1029
1030 reset_key_status();
1031 key_status.expect_alt = TRUE;
1032 pSendInput(2, (INPUT*)inputs, sizeof(INPUT));
1033 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
1034 if(msg.message == WM_SYSKEYDOWN && msg.wParam == VK_PACKET){
1035 TranslateMessage(&msg);
1036 }
1037 DispatchMessageW(&msg);
1038 }
1039 if(!key_status.sendinput_broken){
1040 ok(key_status.last_syskey_down == VK_PACKET,
1041 "Last syskeydown msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_syskey_down);
1042 ok(key_status.last_syschar == 0x3041,
1043 "Last syschar msg should have been 0x3041 (was: 0x%x)\n", key_status.last_syschar);
1044 if(hook)
1045 ok(key_status.last_hook_syskey_down == 0x3041,
1046 "Last hooksysdown msg should have been 0x3041, was: 0x%x\n", key_status.last_hook_syskey_down);
1047 }
1048
1049 inputs[1].u.ki.wVk = 0;
1050 inputs[1].u.ki.wScan = 0x3041;
1051 inputs[1].u.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
1052
1053 inputs[0].u.ki.wVk = VK_LMENU;
1054 inputs[0].u.ki.wScan = 0;
1055 inputs[0].u.ki.dwFlags = KEYEVENTF_KEYUP;
1056
1057 reset_key_status();
1058 key_status.expect_alt = TRUE;
1059 pSendInput(2, (INPUT*)inputs, sizeof(INPUT));
1060 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
1061 if(msg.message == WM_SYSKEYDOWN && msg.wParam == VK_PACKET){
1062 TranslateMessage(&msg);
1063 }
1064 DispatchMessageW(&msg);
1065 }
1066 if(!key_status.sendinput_broken){
1067 ok(key_status.last_key_up == VK_PACKET,
1068 "Last keyup msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_key_up);
1069 if(hook)
1070 ok(key_status.last_hook_up == 0x3041,
1071 "Last hook up msg should have been 0x3041, was: 0x%x\n", key_status.last_hook_up);
1072 }
1073 }
1074
1075 static LRESULT CALLBACK unicode_wnd_proc( HWND hWnd, UINT msg, WPARAM wParam,
1076 LPARAM lParam )
1077 {
1078 switch(msg){
1079 case WM_KEYDOWN:
1080 key_status.last_key_down = wParam;
1081 break;
1082 case WM_SYSKEYDOWN:
1083 key_status.last_syskey_down = wParam;
1084 break;
1085 case WM_KEYUP:
1086 key_status.last_key_up = wParam;
1087 break;
1088 case WM_SYSKEYUP:
1089 key_status.last_syskey_up = wParam;
1090 break;
1091 case WM_CHAR:
1092 key_status.last_char = wParam;
1093 break;
1094 case WM_SYSCHAR:
1095 key_status.last_syschar = wParam;
1096 break;
1097 }
1098 return DefWindowProcW(hWnd, msg, wParam, lParam);
1099 }
1100
1101 static LRESULT CALLBACK llkbd_unicode_hook(int nCode, WPARAM wParam, LPARAM lParam)
1102 {
1103 if(nCode == HC_ACTION){
1104 LPKBDLLHOOKSTRUCT info = (LPKBDLLHOOKSTRUCT)lParam;
1105 if(!info->vkCode){
1106 key_status.sendinput_broken = TRUE;
1107 win_skip("SendInput doesn't support unicode on this platform\n");
1108 }else{
1109 if(key_status.expect_alt){
1110 ok(info->vkCode == VK_LMENU, "vkCode should have been VK_LMENU[0x%04x], was: 0x%x\n", VK_LMENU, info->vkCode);
1111 key_status.expect_alt = FALSE;
1112 }else
1113 ok(info->vkCode == VK_PACKET, "vkCode should have been VK_PACKET[0x%04x], was: 0x%x\n", VK_PACKET, info->vkCode);
1114 }
1115 switch(wParam){
1116 case WM_KEYDOWN:
1117 key_status.last_hook_down = info->scanCode;
1118 break;
1119 case WM_KEYUP:
1120 key_status.last_hook_up = info->scanCode;
1121 break;
1122 case WM_SYSKEYDOWN:
1123 key_status.last_hook_syskey_down = info->scanCode;
1124 break;
1125 case WM_SYSKEYUP:
1126 key_status.last_hook_syskey_up = info->scanCode;
1127 break;
1128 }
1129 }
1130 return CallNextHookEx(NULL, nCode, wParam, lParam);
1131 }
1132
1133 static void test_Input_unicode(void)
1134 {
1135 WCHAR classNameW[] = {'I','n','p','u','t','U','n','i','c','o','d','e',
1136 'K','e','y','T','e','s','t','C','l','a','s','s',0};
1137 WCHAR windowNameW[] = {'I','n','p','u','t','U','n','i','c','o','d','e',
1138 'K','e','y','T','e','s','t',0};
1139 MSG msg;
1140 WNDCLASSW wclass;
1141 HANDLE hInstance = GetModuleHandleW(NULL);
1142 HHOOK hook;
1143
1144 wclass.lpszClassName = classNameW;
1145 wclass.style = CS_HREDRAW | CS_VREDRAW;
1146 wclass.lpfnWndProc = unicode_wnd_proc;
1147 wclass.hInstance = hInstance;
1148 wclass.hIcon = LoadIcon(0, IDI_APPLICATION);
1149 wclass.hCursor = LoadCursor( NULL, IDC_ARROW);
1150 wclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1151 wclass.lpszMenuName = 0;
1152 wclass.cbClsExtra = 0;
1153 wclass.cbWndExtra = 0;
1154 if(!RegisterClassW(&wclass)){
1155 win_skip("Unicode functions not supported\n");
1156 return;
1157 }
1158 /* create the test window that will receive the keystrokes */
1159 hWndTest = CreateWindowW(wclass.lpszClassName, windowNameW,
1160 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
1161 NULL, NULL, hInstance, NULL);
1162
1163 assert(hWndTest);
1164 assert(IsWindowUnicode(hWndTest));
1165
1166 hook = SetWindowsHookExW(WH_KEYBOARD_LL, llkbd_unicode_hook, GetModuleHandleW(NULL), 0);
1167 if(!hook)
1168 win_skip("unable to set WH_KEYBOARD_LL hook\n");
1169
1170 ShowWindow(hWndTest, SW_SHOW);
1171 SetWindowPos(hWndTest, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1172 SetForegroundWindow(hWndTest);
1173 UpdateWindow(hWndTest);
1174
1175 /* flush pending messages */
1176 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageW(&msg);
1177
1178 SetFocus(hWndTest);
1179
1180 test_unicode_keys(hWndTest, hook);
1181
1182 if(hook)
1183 UnhookWindowsHookEx(hook);
1184 DestroyWindow(hWndTest);
1185 }
1186
1187 static void test_keynames(void)
1188 {
1189 int i, len;
1190 char buff[256];
1191
1192 for (i = 0; i < 512; i++)
1193 {
1194 strcpy(buff, "----");
1195 len = GetKeyNameTextA(i << 16, buff, sizeof(buff));
1196 ok(len || !buff[0], "%d: Buffer is not zeroed\n", i);
1197 }
1198 }
1199
1200 static POINT pt_old, pt_new;
1201 static BOOL clipped;
1202 #define STEP 3
1203
1204 static LRESULT CALLBACK hook_proc1( int code, WPARAM wparam, LPARAM lparam )
1205 {
1206 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
1207 POINT pt, pt1;
1208
1209 if (code == HC_ACTION)
1210 {
1211 /* This is our new cursor position */
1212 pt_new = hook->pt;
1213 /* Should return previous position */
1214 GetCursorPos(&pt);
1215 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
1216
1217 /* Should set new position until hook chain is finished. */
1218 pt.x = pt_old.x + STEP;
1219 pt.y = pt_old.y + STEP;
1220 SetCursorPos(pt.x, pt.y);
1221 GetCursorPos(&pt1);
1222 if (clipped)
1223 ok(pt1.x == pt_old.x && pt1.y == pt_old.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
1224 else
1225 ok(pt1.x == pt.x && pt1.y == pt.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
1226 }
1227 return CallNextHookEx( 0, code, wparam, lparam );
1228 }
1229
1230 static LRESULT CALLBACK hook_proc2( int code, WPARAM wparam, LPARAM lparam )
1231 {
1232 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
1233 POINT pt;
1234
1235 if (code == HC_ACTION)
1236 {
1237 ok(hook->pt.x == pt_new.x && hook->pt.y == pt_new.y,
1238 "Wrong hook coords: (%d %d) != (%d,%d)\n", hook->pt.x, hook->pt.y, pt_new.x, pt_new.y);
1239
1240 /* Should match position set above */
1241 GetCursorPos(&pt);
1242 if (clipped)
1243 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
1244 else
1245 ok(pt.x == pt_old.x +STEP && pt.y == pt_old.y +STEP, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
1246 }
1247 return CallNextHookEx( 0, code, wparam, lparam );
1248 }
1249
1250 static void test_mouse_ll_hook(void)
1251 {
1252 HWND hwnd;
1253 HHOOK hook1, hook2;
1254 POINT pt_org, pt;
1255 RECT rc;
1256
1257 GetCursorPos(&pt_org);
1258 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
1259 10, 10, 200, 200, NULL, NULL, NULL, NULL);
1260 SetCursorPos(100, 100);
1261
1262 if (!(hook2 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc2, GetModuleHandleA(0), 0)))
1263 {
1264 win_skip( "cannot set MOUSE_LL hook\n" );
1265 goto done;
1266 }
1267 hook1 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc1, GetModuleHandleA(0), 0);
1268
1269 GetCursorPos(&pt_old);
1270 mouse_event(MOUSEEVENTF_MOVE, -STEP, 0, 0, 0);
1271 GetCursorPos(&pt_old);
1272 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
1273 mouse_event(MOUSEEVENTF_MOVE, +STEP, 0, 0, 0);
1274 GetCursorPos(&pt_old);
1275 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
1276 mouse_event(MOUSEEVENTF_MOVE, 0, -STEP, 0, 0);
1277 GetCursorPos(&pt_old);
1278 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
1279 mouse_event(MOUSEEVENTF_MOVE, 0, +STEP, 0, 0);
1280 GetCursorPos(&pt_old);
1281 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
1282
1283 SetRect(&rc, 50, 50, 151, 151);
1284 ClipCursor(&rc);
1285 clipped = TRUE;
1286
1287 SetCursorPos(40, 40);
1288 GetCursorPos(&pt_old);
1289 ok(pt_old.x == 50 && pt_old.y == 50, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1290 SetCursorPos(160, 160);
1291 GetCursorPos(&pt_old);
1292 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1293 mouse_event(MOUSEEVENTF_MOVE, +STEP, +STEP, 0, 0);
1294 GetCursorPos(&pt_old);
1295 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1296
1297 clipped = FALSE;
1298 pt_new.x = pt_new.y = 150;
1299 ClipCursor(NULL);
1300 UnhookWindowsHookEx(hook1);
1301
1302 /* Now check that mouse buttons do not change mouse position
1303 if we don't have MOUSEEVENTF_MOVE flag specified. */
1304
1305 /* We reusing the same hook callback, so make it happy */
1306 pt_old.x = pt_new.x - STEP;
1307 pt_old.y = pt_new.y - STEP;
1308 mouse_event(MOUSEEVENTF_LEFTUP, 123, 456, 0, 0);
1309 GetCursorPos(&pt);
1310 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1311 mouse_event(MOUSEEVENTF_RIGHTUP, 456, 123, 0, 0);
1312 GetCursorPos(&pt);
1313 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1314
1315 mouse_event(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 123, 456, 0, 0);
1316 GetCursorPos(&pt);
1317 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1318 mouse_event(MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE, 456, 123, 0, 0);
1319 GetCursorPos(&pt);
1320 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1321
1322 UnhookWindowsHookEx(hook2);
1323 done:
1324 DestroyWindow(hwnd);
1325 SetCursorPos(pt_org.x, pt_org.y);
1326 }
1327
1328 static void test_GetMouseMovePointsEx(void)
1329 {
1330 #define BUFLIM 64
1331 #define MYERROR 0xdeadbeef
1332 int count, retval;
1333 MOUSEMOVEPOINT in;
1334 MOUSEMOVEPOINT out[200];
1335 POINT point;
1336
1337 /* Get a valid content for the input struct */
1338 if(!GetCursorPos(&point)) {
1339 skip("GetCursorPos() failed with error %u\n", GetLastError());
1340 return;
1341 }
1342 memset(&in, 0, sizeof(MOUSEMOVEPOINT));
1343 in.x = point.x;
1344 in.y = point.y;
1345
1346 /* test first parameter
1347 * everything different than sizeof(MOUSEMOVEPOINT)
1348 * is expected to fail with ERROR_INVALID_PARAMETER
1349 */
1350 SetLastError(MYERROR);
1351 retval = pGetMouseMovePointsEx(0, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1352 if (retval == ERROR_INVALID_PARAMETER)
1353 {
1354 win_skip( "GetMouseMovePointsEx broken on WinME\n" );
1355 return;
1356 }
1357 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1358 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1359 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1360
1361 SetLastError(MYERROR);
1362 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1363 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1364 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1365 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1366
1367 SetLastError(MYERROR);
1368 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)+1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1369 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1370 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1371 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1372
1373 /* test second and third parameter
1374 */
1375 SetLastError(MYERROR);
1376 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1377 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1378 ok(GetLastError() == ERROR_NOACCESS || GetLastError() == MYERROR,
1379 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1380
1381 SetLastError(MYERROR);
1382 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1383 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1384 ok(ERROR_NOACCESS == GetLastError(),
1385 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1386
1387 SetLastError(MYERROR);
1388 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1389 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1390 ok(ERROR_NOACCESS == GetLastError(),
1391 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1392
1393 SetLastError(MYERROR);
1394 count = 0;
1395 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, count, GMMP_USE_DISPLAY_POINTS);
1396 if (retval == -1)
1397 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1398 else
1399 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1400
1401 /* test fourth parameter
1402 * a value higher than 64 is expected to fail with ERROR_INVALID_PARAMETER
1403 */
1404 SetLastError(MYERROR);
1405 count = -1;
1406 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1407 ok(retval == count, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1408 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1409 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1410
1411 SetLastError(MYERROR);
1412 count = 0;
1413 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1414 if (retval == -1)
1415 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1416 else
1417 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1418
1419 SetLastError(MYERROR);
1420 count = BUFLIM;
1421 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1422 if (retval == -1)
1423 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1424 else
1425 ok((0 <= retval) && (retval <= count), "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1426
1427 SetLastError(MYERROR);
1428 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1429 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1430 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1431 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1432
1433 /* it was not possible to force an error with the fifth parameter on win2k */
1434
1435 /* test combinations of wrong parameters to see which error wins */
1436 SetLastError(MYERROR);
1437 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1438 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1439 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1440 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1441
1442 SetLastError(MYERROR);
1443 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1444 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1445 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1446 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1447
1448 SetLastError(MYERROR);
1449 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1450 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1451 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1452 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1453
1454 SetLastError(MYERROR);
1455 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1456 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1457 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1458 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1459
1460 #undef BUFLIM
1461 #undef MYERROR
1462 }
1463
1464 static void test_key_map(void)
1465 {
1466 HKL kl = GetKeyboardLayout(0);
1467 UINT kL, kR, s, sL;
1468 int i;
1469 static const UINT numpad_collisions[][2] = {
1470 { VK_NUMPAD0, VK_INSERT },
1471 { VK_NUMPAD1, VK_END },
1472 { VK_NUMPAD2, VK_DOWN },
1473 { VK_NUMPAD3, VK_NEXT },
1474 { VK_NUMPAD4, VK_LEFT },
1475 { VK_NUMPAD6, VK_RIGHT },
1476 { VK_NUMPAD7, VK_HOME },
1477 { VK_NUMPAD8, VK_UP },
1478 { VK_NUMPAD9, VK_PRIOR },
1479 };
1480
1481 s = MapVirtualKeyEx(VK_SHIFT, MAPVK_VK_TO_VSC, kl);
1482 ok(s != 0, "MapVirtualKeyEx(VK_SHIFT) should return non-zero\n");
1483 sL = MapVirtualKeyEx(VK_LSHIFT, MAPVK_VK_TO_VSC, kl);
1484 ok(s == sL || broken(sL == 0), /* win9x */
1485 "%x != %x\n", s, sL);
1486
1487 kL = MapVirtualKeyEx(0x2a, MAPVK_VSC_TO_VK, kl);
1488 ok(kL == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kL);
1489 kR = MapVirtualKeyEx(0x36, MAPVK_VSC_TO_VK, kl);
1490 ok(kR == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kR);
1491
1492 kL = MapVirtualKeyEx(0x2a, MAPVK_VSC_TO_VK_EX, kl);
1493 ok(kL == VK_LSHIFT || broken(kL == 0), /* win9x */
1494 "Scan code -> vKey = %x (not VK_LSHIFT)\n", kL);
1495 kR = MapVirtualKeyEx(0x36, MAPVK_VSC_TO_VK_EX, kl);
1496 ok(kR == VK_RSHIFT || broken(kR == 0), /* win9x */
1497 "Scan code -> vKey = %x (not VK_RSHIFT)\n", kR);
1498
1499 /* test that MAPVK_VSC_TO_VK prefers the non-numpad vkey if there's ambiguity */
1500 for (i = 0; i < sizeof(numpad_collisions)/sizeof(numpad_collisions[0]); i++)
1501 {
1502 UINT numpad_scan = MapVirtualKeyEx(numpad_collisions[i][0], MAPVK_VK_TO_VSC, kl);
1503 UINT other_scan = MapVirtualKeyEx(numpad_collisions[i][1], MAPVK_VK_TO_VSC, kl);
1504
1505 /* do they really collide for this layout? */
1506 if (numpad_scan && other_scan == numpad_scan)
1507 {
1508 UINT vkey = MapVirtualKeyEx(numpad_scan, MAPVK_VSC_TO_VK, kl);
1509 ok(vkey != numpad_collisions[i][0],
1510 "Got numpad vKey %x for scan code %x when there was another choice\n",
1511 vkey, numpad_scan);
1512 }
1513 }
1514 }
1515
1516 static void test_ToUnicode(void)
1517 {
1518 WCHAR wStr[2];
1519 BYTE state[256];
1520 const BYTE SC_RETURN = 0x1c, SC_TAB = 0x0f;
1521 const BYTE HIGHEST_BIT = 0x80;
1522 int i, ret;
1523 for(i=0; i<256; i++)
1524 state[i]=0;
1525
1526 SetLastError(0xdeadbeef);
1527 ret = ToUnicode(VK_RETURN, SC_RETURN, state, wStr, 2, 0);
1528 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1529 {
1530 win_skip("ToUnicode is not implemented\n");
1531 return;
1532 }
1533
1534 ok(ret == 1, "ToUnicode for Return key didn't return 1 (was %i)\n", ret);
1535 if(ret == 1)
1536 ok(wStr[0]=='\r', "ToUnicode for CTRL + Return was %i (expected 13)\n", wStr[0]);
1537 state[VK_CONTROL] |= HIGHEST_BIT;
1538 state[VK_LCONTROL] |= HIGHEST_BIT;
1539
1540 ret = ToUnicode(VK_TAB, SC_TAB, state, wStr, 2, 0);
1541 ok(ret == 0, "ToUnicode for CTRL + Tab didn't return 0 (was %i)\n", ret);
1542
1543 ret = ToUnicode(VK_RETURN, SC_RETURN, state, wStr, 2, 0);
1544 ok(ret == 1, "ToUnicode for CTRL + Return didn't return 1 (was %i)\n", ret);
1545 if(ret == 1)
1546 ok(wStr[0]=='\n', "ToUnicode for CTRL + Return was %i (expected 10)\n", wStr[0]);
1547
1548 state[VK_SHIFT] |= HIGHEST_BIT;
1549 state[VK_LSHIFT] |= HIGHEST_BIT;
1550 ret = ToUnicode(VK_TAB, SC_TAB, state, wStr, 2, 0);
1551 ok(ret == 0, "ToUnicode for CTRL + SHIFT + Tab didn't return 0 (was %i)\n", ret);
1552 ret = ToUnicode(VK_RETURN, SC_RETURN, state, wStr, 2, 0);
1553 todo_wine ok(ret == 0, "ToUnicode for CTRL + SHIFT + Return didn't return 0 (was %i)\n", ret);
1554 }
1555
1556 static void test_get_async_key_state(void)
1557 {
1558 /* input value sanity checks */
1559 ok(0 == GetAsyncKeyState(1000000), "GetAsyncKeyState did not return 0\n");
1560 ok(0 == GetAsyncKeyState(-1000000), "GetAsyncKeyState did not return 0\n");
1561 }
1562
1563 static void test_keyboard_layout_name(void)
1564 {
1565 BOOL ret;
1566 char klid[KL_NAMELENGTH];
1567
1568 if (GetKeyboardLayout(0) != (HKL)(ULONG_PTR)0x04090409) return;
1569
1570 klid[0] = 0;
1571 ret = GetKeyboardLayoutNameA(klid);
1572 ok(ret, "GetKeyboardLayoutNameA failed %u\n", GetLastError());
1573 ok(!strcmp(klid, "00000409"), "expected 00000409, got %s\n", klid);
1574 }
1575
1576 START_TEST(input)
1577 {
1578 init_function_pointers();
1579
1580 if (pSendInput)
1581 {
1582 test_Input_blackbox();
1583 test_Input_whitebox();
1584 test_Input_unicode();
1585 }
1586 else win_skip("SendInput is not available\n");
1587
1588 test_keynames();
1589 test_mouse_ll_hook();
1590 test_key_map();
1591 test_ToUnicode();
1592 test_get_async_key_state();
1593 test_keyboard_layout_name();
1594
1595 if(pGetMouseMovePointsEx)
1596 test_GetMouseMovePointsEx();
1597 else
1598 win_skip("GetMouseMovePointsEx is not available\n");
1599 }