mousetools 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +3 -0
- data/lib/cgevent.rb +136 -0
- data/lib/mousetools.rb +53 -168
- metadata +5 -3
data/CHANGELOG
ADDED
data/lib/cgevent.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
module CGEvent
|
4
|
+
extend FFI::Library
|
5
|
+
ffi_lib '/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics'
|
6
|
+
|
7
|
+
class CGPoint < FFI::Struct
|
8
|
+
layout :x, :double,
|
9
|
+
:y, :double
|
10
|
+
end
|
11
|
+
|
12
|
+
# Mouse/Keyboard events
|
13
|
+
enum :CGEventType, [ :kCGEventNull, 0, # Null Event
|
14
|
+
:kCGEventLeftMouseDown, 1, # left mouse-down event
|
15
|
+
:kCGEventLeftMouseUp, 2, # left mouse-up event
|
16
|
+
:kCGEventRightMouseDown, 3, # right mouse-down event
|
17
|
+
:kCGEventRightMouseUp, 4, # right mouse-up event
|
18
|
+
:kCGEventMouseMoved, 5, # mouse-moved event
|
19
|
+
:kCGEventLeftMouseDragged, 6, # left mouse-dragged event
|
20
|
+
:kCGEventRightMouseDragged, 7, # right mouse-dragged event
|
21
|
+
:kCGEventScrollWheel, 22, # mouse scrolled event
|
22
|
+
:kCGEventOtherMouseDown, 25, # other mouse-down event
|
23
|
+
:kCGEventOtherMouseUp, 26, # other mouse-up event
|
24
|
+
:kCGEventOtherMouseDragged, 27, # other mouse-dragged event
|
25
|
+
:kCGEventKeyDown, 10, # key-down event
|
26
|
+
:kCGEventKeyUp, 11 ] # key-up down
|
27
|
+
|
28
|
+
# Mouse buttons
|
29
|
+
enum :CGMouseButton, [ :kCGMouseButtonLeft, 0,
|
30
|
+
:kCGMouseButtonRight,
|
31
|
+
:kCGMouseButtonCenter ]
|
32
|
+
|
33
|
+
# Virtual Key Codes
|
34
|
+
enum :CGKeyCode, [ :A, 0x00,
|
35
|
+
:S, 0x01,
|
36
|
+
:D, 0x02,
|
37
|
+
:F, 0x03,
|
38
|
+
:H, 0x04,
|
39
|
+
:G, 0x05,
|
40
|
+
:Z, 0x06,
|
41
|
+
:X, 0x07,
|
42
|
+
:C, 0x08,
|
43
|
+
:V, 0x09,
|
44
|
+
:B, 0x0B,
|
45
|
+
:Q, 0x0C,
|
46
|
+
:W, 0x0D,
|
47
|
+
:E, 0x0E,
|
48
|
+
:R, 0x0F,
|
49
|
+
:Y, 0x10,
|
50
|
+
:T, 0x11,
|
51
|
+
:Num1, 0x12,
|
52
|
+
:Num2, 0x13,
|
53
|
+
:Num3, 0x14,
|
54
|
+
:Num4, 0x15,
|
55
|
+
:Num6, 0x16,
|
56
|
+
:Num5, 0x17,
|
57
|
+
:Equal, 0x18,
|
58
|
+
:Num9, 0x19,
|
59
|
+
:Num7, 0x1A,
|
60
|
+
:Minus, 0x1B,
|
61
|
+
:Num8, 0x1C,
|
62
|
+
:Num0, 0x1D,
|
63
|
+
:RightBracket, 0x1E,
|
64
|
+
:O, 0x1F,
|
65
|
+
:U, 0x20,
|
66
|
+
:LeftBracket, 0x21,
|
67
|
+
:I, 0x22,
|
68
|
+
:P, 0x23,
|
69
|
+
:L, 0x25,
|
70
|
+
:J, 0x26,
|
71
|
+
:Quote, 0x27,
|
72
|
+
:K, 0x28,
|
73
|
+
:Semicolon, 0x29,
|
74
|
+
:Backslash, 0x2A,
|
75
|
+
:Comma, 0x2B,
|
76
|
+
:Slash, 0x2C,
|
77
|
+
:N, 0x2D,
|
78
|
+
:M, 0x2E,
|
79
|
+
:Period, 0x2F,
|
80
|
+
:Grave, 0x32,
|
81
|
+
:KeypadDecimal, 0x41,
|
82
|
+
:KeypadMultiply, 0x43,
|
83
|
+
:KeypadPlus, 0x45,
|
84
|
+
:KeypadClear, 0x47,
|
85
|
+
:KeypadDivide, 0x4B,
|
86
|
+
:KeypadEnter, 0x4C,
|
87
|
+
:KeypadMinus, 0x4E,
|
88
|
+
:KeypadEquals, 0x51,
|
89
|
+
:Keypad0, 0x52,
|
90
|
+
:Keypad1, 0x53,
|
91
|
+
:Keypad2, 0x54,
|
92
|
+
:Keypad3, 0x55,
|
93
|
+
:Keypad4, 0x56,
|
94
|
+
:Keypad5, 0x57,
|
95
|
+
:Keypad6, 0x58,
|
96
|
+
:Keypad7, 0x59,
|
97
|
+
:Keypad8, 0x5B,
|
98
|
+
:Keypad9, 0x5C,
|
99
|
+
:return, 0x24,
|
100
|
+
:tab, 0x30,
|
101
|
+
:space, 0x31,
|
102
|
+
:delete, 0x33,
|
103
|
+
:escape, 0x35,
|
104
|
+
:function, 0x3F,
|
105
|
+
:home, 0x73,
|
106
|
+
:end, 0x77,
|
107
|
+
:page_up, 0x74,
|
108
|
+
:page_down, 0x79,
|
109
|
+
:forward_delete, 0x75,
|
110
|
+
:left_arrow, 0x7B,
|
111
|
+
:right_arrow, 0x7C,
|
112
|
+
:down_arrow, 0x7D,
|
113
|
+
:up_arrow, 0x7E ]
|
114
|
+
|
115
|
+
enum :CGEventFlags, [ :alphashift, 0x00010000,
|
116
|
+
:shift, 0x00020000,
|
117
|
+
:control, 0x00040000,
|
118
|
+
:alt, 0x00080000,
|
119
|
+
:command, 0x00100000,
|
120
|
+
:help, 0x00400000,
|
121
|
+
:secondaryfn, 0x00800000 ]
|
122
|
+
|
123
|
+
# CGEventTapLocation
|
124
|
+
enum :CGEventTapLocation, [ :kCGHIDEventTap, 0,
|
125
|
+
:kCGSessionEventTap,
|
126
|
+
:kCGAnnotatedSessionEventTap ]
|
127
|
+
|
128
|
+
# Attach to the CGEventCreateMouseEvent function
|
129
|
+
attach_function :CGEventCreateMouseEvent, [ :pointer, :CGEventType, CGPoint.by_value, :CGMouseButton ], :pointer
|
130
|
+
attach_function :CGEventCreateKeyboardEvent, [:pointer, :CGKeyCode, :bool ], :pointer
|
131
|
+
attach_function :CGEventSetFlags, [:pointer, :CGEventFlags], :void
|
132
|
+
attach_function :CGEventSetType, [:pointer, :CGEventType], :void
|
133
|
+
attach_function :CGEventPost, [ :CGEventTapLocation, :pointer], :void
|
134
|
+
attach_function :CFRelease, [:pointer], :void
|
135
|
+
|
136
|
+
end
|
data/lib/mousetools.rb
CHANGED
@@ -1,194 +1,79 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
module Mouse
|
4
|
-
extend FFI::Library
|
5
|
-
ffi_lib '/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics'
|
6
|
-
|
7
|
-
class CGPoint < FFI::Struct
|
8
|
-
layout :x, :double,
|
9
|
-
:y, :double
|
10
|
-
end
|
11
|
-
|
12
|
-
# Mouse/Keyboard events
|
13
|
-
enum :CGEventType, [ :kCGEventNull, 0, # Null Event
|
14
|
-
:kCGEventLeftMouseDown, 1, # left mouse-down event
|
15
|
-
:kCGEventLeftMouseUp, 2, # left mouse-up event
|
16
|
-
:kCGEventRightMouseDown, 3, # right mouse-down event
|
17
|
-
:kCGEventRightMouseUp, 4, # right mouse-up event
|
18
|
-
:kCGEventMouseMoved, 5, # mouse-moved event
|
19
|
-
:kCGEventLeftMouseDragged, 6, # left mouse-dragged event
|
20
|
-
:kCGEventRightMouseDragged, 7, # right mouse-dragged event
|
21
|
-
:kCGEventScrollWheel, 22, # mouse scrolled event
|
22
|
-
:kCGEventOtherMouseDown, 25, # other mouse-down event
|
23
|
-
:kCGEventOtherMouseUp, 26, # other mouse-up event
|
24
|
-
:kCGEventOtherMouseDragged, 27, # other mouse-dragged event
|
25
|
-
:kCGEventKeyDown, 10, # key-down event
|
26
|
-
:kCGEventKeyUp, 11 ] # key-up down
|
27
|
-
|
28
|
-
# Mouse buttons
|
29
|
-
enum :CGMouseButton, [ :kCGMouseButtonLeft, 0,
|
30
|
-
:kCGMouseButtonRight,
|
31
|
-
:kCGMouseButtonCenter ]
|
32
|
-
|
33
|
-
# Virtual Key Codes
|
34
|
-
enum :CGKeyCode, [ :return, 0x24,
|
35
|
-
:tab, 0x30,
|
36
|
-
:space, 0x31,
|
37
|
-
:delete, 0x33,
|
38
|
-
:escape, 0x35,
|
39
|
-
:command, 0x37,
|
40
|
-
:left_shift, 0x38,
|
41
|
-
:left_option, 0x3A,
|
42
|
-
:left_control, 0x3B,
|
43
|
-
:right_shift, 0x3C,
|
44
|
-
:right_option, 0x3D,
|
45
|
-
:right_control, 0x3E,
|
46
|
-
:function, 0x3F,
|
47
|
-
:home, 0x73,
|
48
|
-
:end, 0x77,
|
49
|
-
:page_up, 0x74,
|
50
|
-
:page_down, 0x79,
|
51
|
-
:forward_delete, 0x75,
|
52
|
-
:left_arrow, 0x7B,
|
53
|
-
:right_arrow, 0x7C,
|
54
|
-
:down_arrow, 0x7D,
|
55
|
-
:up_arrow, 0x7E ]
|
56
|
-
|
57
|
-
# CGEventTapLocation
|
58
|
-
enum :CGEventTapLocation, [ :kCGHIDEventTap, 0,
|
59
|
-
:kCGSessionEventTap,
|
60
|
-
:kCGAnnotatedSessionEventTap ]
|
61
|
-
|
62
|
-
# Attach to the CGEventCreateMouseEvent function
|
63
|
-
attach_function :CGEventCreateMouseEvent, [ :pointer, :CGEventType, CGPoint.by_value, :CGMouseButton ], :pointer
|
64
|
-
attach_function :CGEventCreateKeyboardEvent, [:pointer, :CGKeyCode, :bool ], :pointer
|
65
|
-
attach_function :CGEventSetType, [:pointer, :CGEventType], :void
|
66
|
-
attach_function :CGEventPost, [ :CGEventTapLocation, :pointer], :void
|
67
|
-
attach_function :CFRelease, [:pointer], :void
|
68
|
-
|
69
|
-
end
|
1
|
+
require "cgevent"
|
2
|
+
include CGEvent
|
70
3
|
|
71
4
|
class Mousetools
|
72
5
|
|
73
|
-
@current_coordinates
|
74
6
|
attr_accessor :current_coordinates
|
75
7
|
|
76
|
-
def
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
def left_mouse_down(x = nil, y = nil)
|
81
|
-
raise "Coordinates not set" if (x.nil? || y.nil?) && @current_coordinates.nil?
|
82
|
-
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
83
|
-
point = Mouse::CGPoint.new
|
84
|
-
point[:x] = @current_coordinates[:x]
|
85
|
-
point[:y] = @current_coordinates[:y]
|
86
|
-
# Create the event
|
87
|
-
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventLeftMouseDown, point, :kCGMouseButtonLeft)
|
88
|
-
# Post the event
|
89
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
90
|
-
# Release the event ref
|
91
|
-
Mouse::CFRelease(event)
|
8
|
+
def left_mouse_down(x = nil, y = nil, modifier_key_1 = nil, modifier_key_2 = nil)
|
9
|
+
post_mouse_event(x, y, :kCGEventLeftMouseDown, :kCGMouseButtonLeft, modifier_key_1, modifier_key_2)
|
92
10
|
end
|
93
11
|
|
94
12
|
def left_mouse_up(x = nil, y = nil)
|
95
|
-
|
96
|
-
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
97
|
-
point = Mouse::CGPoint.new
|
98
|
-
point[:x] = @current_coordinates[:x]
|
99
|
-
point[:y] = @current_coordinates[:y]
|
100
|
-
# Create the event
|
101
|
-
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventLeftMouseUp, point, :kCGMouseButtonLeft)
|
102
|
-
# Post the event
|
103
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
104
|
-
# Release the event ref
|
105
|
-
Mouse::CFRelease(event)
|
13
|
+
post_mouse_event(x, y, :kCGEventLeftMouseUp, :kCGMouseButtonLeft)
|
106
14
|
end
|
107
15
|
|
108
|
-
def right_mouse_down(x = nil, y = nil)
|
109
|
-
|
110
|
-
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
111
|
-
point = Mouse::CGPoint.new
|
112
|
-
point[:x] = @current_coordinates[:x]
|
113
|
-
point[:y] = @current_coordinates[:y]
|
114
|
-
# Create the event
|
115
|
-
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventRightMouseDown, point, :kCGMouseButtonRight)
|
116
|
-
# Post the event
|
117
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
118
|
-
# Release the event ref
|
119
|
-
Mouse::CFRelease(event)
|
16
|
+
def right_mouse_down(x = nil, y = nil, modifier_key_1 = nil, modifier_key_2 = nil)
|
17
|
+
post_mouse_event(x, y, :kCGEventRightMouseDown, :kCGMouseButtonRight, modifier_key_1, modifier_key_2)
|
120
18
|
end
|
121
19
|
|
122
20
|
def right_mouse_up(x = nil, y = nil)
|
123
|
-
|
124
|
-
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
125
|
-
point = Mouse::CGPoint.new
|
126
|
-
point[:x] = @current_coordinates[:x]
|
127
|
-
point[:y] = @current_coordinates[:y]
|
128
|
-
# Create the event
|
129
|
-
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventRightMouseUp, point, :kCGMouseButtonRight)
|
130
|
-
# Post the event
|
131
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
132
|
-
# Release the event ref
|
133
|
-
Mouse::CFRelease(event)
|
21
|
+
post_mouse_event(x, y, :kCGEventRightMouseUp, :kCGMouseButtonRight)
|
134
22
|
end
|
135
23
|
|
136
|
-
def move_cursor(x
|
137
|
-
|
138
|
-
point = Mouse::CGPoint.new
|
139
|
-
point[:x] = x
|
140
|
-
point[:y] = y
|
141
|
-
# Create the event
|
142
|
-
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventMouseMoved, point, :kCGMouseButtonLeft)
|
143
|
-
# Post the event
|
144
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
145
|
-
@current_coordinates = {:x => x, :y => y}
|
146
|
-
# Release the event ref
|
147
|
-
Mouse::CFRelease(event)
|
24
|
+
def move_cursor(x, y)
|
25
|
+
post_mouse_event(x, y, :kCGEventMouseMoved, :kCGMouseButtonLeft)
|
148
26
|
end
|
149
27
|
|
150
|
-
def left_mouse_drag(x = nil,
|
151
|
-
|
152
|
-
point = Mouse::CGPoint.new
|
153
|
-
point[:x] = x
|
154
|
-
point[:y] = y
|
155
|
-
# Create the event
|
156
|
-
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventLeftMouseDragged, point, :kCGMouseButtonLeft)
|
157
|
-
# Post the event
|
158
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
159
|
-
@current_coordinates = {:x => x, :y => y}
|
160
|
-
# Release the event ref
|
161
|
-
Mouse::CFRelease(event)
|
28
|
+
def left_mouse_drag(x, y, modifier_key_1 = nil, modifier_key_2 = nil)
|
29
|
+
post_mouse_event(x, y, :kCGEventLeftMouseDragged, :kCGMouseButtonLeft, modifier_key_1, modifier_key_2)
|
162
30
|
end
|
163
31
|
|
164
|
-
def right_mouse_drag(x = nil,
|
165
|
-
|
166
|
-
point = Mouse::CGPoint.new
|
167
|
-
point[:x] = x
|
168
|
-
point[:y] = y
|
169
|
-
# Create the event
|
170
|
-
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventRightMouseDragged, point, :kCGMouseButtonRight)
|
171
|
-
# Post the event
|
172
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
173
|
-
@current_coordinates = {:x => x, :y => y}
|
174
|
-
# Release the event ref
|
175
|
-
Mouse::CFRelease(event)
|
32
|
+
def right_mouse_drag(x, y, modifier_key_1 = nil, modifier_key_2 = nil)
|
33
|
+
post_mouse_event(x, y, :kCGEventRightMouseDragged, :kCGMouseButtonRight, modifier_key_1, modifier_key_2)
|
176
34
|
end
|
177
35
|
|
178
|
-
def key_down(virtual_key = nil)
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
36
|
+
def key_down(virtual_key, modifier_key_1 = nil, modifier_key_2 = nil)
|
37
|
+
if (modifier_key_1.nil? && modifier_key_2.nil?)
|
38
|
+
post_keyboard_event(virtual_key, true)
|
39
|
+
elsif (!modifier_key_1.nil? && modifier_key_2.nil?)
|
40
|
+
post_keyboard_event(virtual_key, true, modifier_key_1)
|
41
|
+
else
|
42
|
+
post_keyboard_event(virtual_key, true, modifier_key_1, modifier_key_2)
|
43
|
+
end
|
184
44
|
end
|
185
45
|
|
186
|
-
def key_up(virtual_key
|
187
|
-
|
188
|
-
keycode = virtual_key.intern
|
189
|
-
event = Mouse::CGEventCreateKeyboardEvent(nil, keycode, false)
|
190
|
-
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
191
|
-
Mouse::CFRelease(event)
|
46
|
+
def key_up(virtual_key)
|
47
|
+
post_keyboard_event(virtual_key, false)
|
192
48
|
end
|
193
49
|
|
50
|
+
# Private Methods
|
51
|
+
private
|
52
|
+
def set_current_coordinates(x, y)
|
53
|
+
@current_coordinates = CGPoint.new if @current_coordinates.nil?
|
54
|
+
@current_coordinates[:x] = x
|
55
|
+
@current_coordinates[:y] = y
|
56
|
+
end
|
57
|
+
|
58
|
+
def post_mouse_event(x, y, event_type, mouse_button, modifier_key_1 = nil, modifier_key_2 = nil)
|
59
|
+
raise "Must provide inital x and y coordinates" if (x.nil? || y.nil?) && @current_coordinates.nil?
|
60
|
+
# Create the event
|
61
|
+
set_current_coordinates(x, y) unless x.nil? || y.nil?
|
62
|
+
event = CGEventCreateMouseEvent(nil, event_type, @current_coordinates, mouse_button)
|
63
|
+
CGEventSetFlags(event, modifier_key_1) if (modifier_key_1)
|
64
|
+
CGEventSetFlags(event, modifier_key_2) if (modifier_key_2)
|
65
|
+
# Post the event
|
66
|
+
CGEventPost(:kCGHIDEventTap, event)
|
67
|
+
# Release the event ref
|
68
|
+
CFRelease(event)
|
69
|
+
end
|
70
|
+
|
71
|
+
def post_keyboard_event(virtual_key, position, modifier_key_1 = nil, modifier_key_2 = nil)
|
72
|
+
event = CGEventCreateKeyboardEvent(nil, virtual_key, position)
|
73
|
+
CGEventSetFlags(event, modifier_key_1) if (modifier_key_1)
|
74
|
+
CGEventSetFlags(event, modifier_key_2) if (modifier_key_2)
|
75
|
+
CGEventPost(:kCGHIDEventTap, event)
|
76
|
+
CFRelease(event)
|
77
|
+
end
|
78
|
+
|
194
79
|
end
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: mousetools
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
6
|
- 1
|
9
|
-
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Scott Larson
|
@@ -41,6 +41,8 @@ extra_rdoc_files: []
|
|
41
41
|
|
42
42
|
files:
|
43
43
|
- lib/mousetools.rb
|
44
|
+
- lib/cgevent.rb
|
45
|
+
- CHANGELOG
|
44
46
|
has_rdoc: true
|
45
47
|
homepage: http://rubygems.org/gems/mousetools
|
46
48
|
licenses: []
|