]> git.saurik.com Git - wxWidgets.git/blob - src/osx/uiaction_osx.cpp
fixing some warnings, adding proper OnChar support for special keys
[wxWidgets.git] / src / osx / uiaction_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/uiaction_osx.cpp
3 // Purpose: wxUIActionSimulator implementation
4 // Author: Kevin Ollivier
5 // Modified by:
6 // Created: 2010-03-06
7 // RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Kevin Ollivier
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/defs.h"
13
14 #if wxUSE_UIACTIONSIMULATOR
15
16 #include "wx/uiaction.h"
17
18 #include <ApplicationServices/ApplicationServices.h>
19
20 CGEventTapLocation tap = kCGSessionEventTap;
21
22 CGEventType CGEventTypeForMouseButton(int button, bool isDown)
23 {
24 switch (button)
25 {
26 case wxMOUSE_BTN_LEFT:
27 if (isDown)
28 return kCGEventLeftMouseDown;
29 else
30 return kCGEventLeftMouseUp;
31 case wxMOUSE_BTN_RIGHT:
32 if (isDown)
33 return kCGEventRightMouseDown;
34 else
35 return kCGEventRightMouseUp;
36
37 // Apparently all other buttons use the constant OtherMouseDown
38
39 default:
40 if (isDown)
41 return kCGEventOtherMouseDown;
42 else
43 return kCGEventOtherMouseUp;
44 }
45 }
46
47 void SendCharCode(CGCharCode keycode, bool isDown)
48 {
49 CGEventRef event = CGEventCreateKeyboardEvent(NULL, keycode, isDown);
50 if (event)
51 {
52 CGEventPost(kCGHIDEventTap, event);
53 }
54 CFRelease(event);
55 }
56
57 bool wxUIActionSimulator::MouseDown(int button)
58 {
59 CGPoint pos;
60 int x, y;
61 wxGetMousePosition(&x, &y);
62 pos.x = x;
63 pos.y = y;
64 CGEventType type = CGEventTypeForMouseButton(button, true);
65 CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, button);
66 CGEventSetType(event, type);
67
68 if (event)
69 {
70 CGEventPost(tap, event);
71 }
72 CFRelease(event);
73 return true;
74 }
75
76 bool wxUIActionSimulator::MouseMove(long x, long y)
77 {
78 CGPoint pos;
79 pos.x = x;
80 pos.y = y;
81 CGEventType type = kCGEventMouseMoved;
82 CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, kCGMouseButtonLeft);
83 CGEventSetType(event, type);
84
85 if (event)
86 {
87 CGEventPost(tap, event);
88 }
89 CFRelease(event);
90
91 return true;
92 }
93
94 bool wxUIActionSimulator::MouseUp(int button)
95 {
96 CGPoint pos;
97 int x, y;
98 wxGetMousePosition(&x, &y);
99 pos.x = x;
100 pos.y = y;
101 CGEventType type = CGEventTypeForMouseButton(button, false);
102 CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, button);
103 CGEventSetType(event, type);
104
105 if (event)
106 {
107 CGEventPost(tap, event);
108 }
109 CFRelease(event);
110
111 return true;
112 }
113
114 bool wxUIActionSimulator::Key(int keycode, bool isDown, bool shiftDown, bool cmdDown, bool altDown)
115 {
116 if (shiftDown)
117 SendCharCode((CGCharCode)56, true);
118 if (altDown)
119 SendCharCode((CGCharCode)58, true);
120 if (cmdDown)
121 SendCharCode((CGCharCode)55, true);
122
123 SendCharCode((CGCharCode)keycode, isDown);
124
125 if (shiftDown)
126 SendCharCode((CGCharCode)56, false);
127 if (altDown)
128 SendCharCode((CGCharCode)58, false);
129 if (cmdDown)
130 SendCharCode((CGCharCode)55, false);
131
132 return true;
133 }
134
135 #endif // wxUSE_UIACTIONSIMULATOR
136