Explicitly send events for modifier keys in wxUIActionSimulator.
[wxWidgets.git] / src / msw / uiaction.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/uiaction.cpp
3 // Purpose: wxUIActionSimulator implementation
4 // Author: Kevin Ollivier, Steven Lamerton, Vadim Zeitlin
5 // Modified by:
6 // Created: 2010-03-06
7 // RCS-ID: $Id$
8 // Copyright: (c) Kevin Ollivier
9 // (c) 2010 Steven Lamerton
10 // (c) 2010 Vadim Zeitlin
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #include "wx/wxprec.h"
15
16 #if wxUSE_UIACTIONSIMULATOR
17
18 #include "wx/uiaction.h"
19 #include "wx/window.h" //for wxCharCodeWXToMSW
20 #include "wx/msw/wrapwin.h"
21
22 namespace
23 {
24
25 DWORD EventTypeForMouseButton(int button, bool isDown)
26 {
27 switch (button)
28 {
29 case wxMOUSE_BTN_LEFT:
30 return isDown ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
31
32 case wxMOUSE_BTN_RIGHT:
33 return isDown ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
34
35 case wxMOUSE_BTN_MIDDLE:
36 return isDown ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
37
38 default:
39 wxFAIL_MSG("Unsupported button passed in.");
40 return (DWORD)-1;
41 }
42 }
43
44 } // anonymous namespace
45
46 bool wxUIActionSimulator::MouseDown(int button)
47 {
48 POINT p;
49 GetCursorPos(&p);
50 mouse_event(EventTypeForMouseButton(button, true), p.x, p.y, 0, 0);
51 return true;
52 }
53
54 bool wxUIActionSimulator::MouseMove(long x, long y)
55 {
56 // Because MOUSEEVENTF_ABSOLUTE takes measurements scaled between 0 & 65535
57 // we need to scale our input too
58 int displayx, displayy, scaledx, scaledy;
59 wxDisplaySize(&displayx, &displayy);
60 scaledx = ((float)x / displayx) * 65535;
61 scaledy = ((float)y / displayy) * 65535;
62 mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, scaledx, scaledy, 0, 0);
63 return true;
64 }
65
66 bool wxUIActionSimulator::MouseUp(int button)
67 {
68 POINT p;
69 GetCursorPos(&p);
70 mouse_event(EventTypeForMouseButton(button, false), p.x, p.y, 0, 0);
71 return true;
72 }
73
74 bool
75 wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown)
76 {
77 DWORD vkkeycode = wxCharCodeWXToMSW(keycode);
78 keybd_event(vkkeycode, 0, isDown ? 0 : KEYEVENTF_KEYUP, 0);
79
80 return true;
81 }
82
83 #endif // wxUSE_UIACTIONSIMULATOR