1 /**
2  * Windows input utility code.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.sys.windows.input;
15 version (Windows):
16 
17 import ae.sys.windows.imports;
18 mixin(importWin32!q{winbase});
19 mixin(importWin32!q{windef});
20 mixin(importWin32!q{winuser});
21 
22 /// Send WM_COPYDATA to the specified window.
23 void sendCopyData(HWND hWnd, DWORD n, in void[] buf)
24 {
25 	COPYDATASTRUCT cds;
26 	cds.dwData = n;
27 	cds.cbData = cast(uint)buf.length;
28 	cds.lpData = cast(PVOID)buf.ptr;
29 	SendMessage(hWnd, WM_COPYDATA, 0, cast(LPARAM)&cds);
30 }
31 
32 private enum MAPVK_VK_TO_VSC = 0;
33 
34 /// Simulate keyboard input.
35 void keyDown(ubyte c) { keybd_event(c, cast(ubyte)MapVirtualKey(c, MAPVK_VK_TO_VSC), 0              , 0); }
36 void keyUp  (ubyte c) { keybd_event(c, cast(ubyte)MapVirtualKey(c, MAPVK_VK_TO_VSC), KEYEVENTF_KEYUP, 0); } /// ditto
37 
38 void press(ubyte c, uint delay=0)
39 {
40 	if (c) keyDown(c);
41 	Sleep(delay);
42 	if (c) keyUp(c);
43 	Sleep(delay);
44 } /// ditto
45 
46 void keyDownOn(HWND h, ubyte c) { PostMessage(h, WM_KEYDOWN, c, MapVirtualKey(c, MAPVK_VK_TO_VSC) << 16); } /// ditto
47 void keyUpOn  (HWND h, ubyte c) { PostMessage(h, WM_KEYUP  , c, MapVirtualKey(c, MAPVK_VK_TO_VSC) << 16); } /// ditto
48 
49 void pressOn(HWND h, ubyte c, uint delay=0)
50 {
51 	if (c) keyDownOn(h, c);
52 	Sleep(delay);
53 	if (c) keyUpOn(h, c);
54 	Sleep(delay);
55 } /// ditto