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