mousetools 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mousetools.rb +195 -0
- metadata +62 -0
data/lib/mousetools.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ffi'
|
3
|
+
|
4
|
+
module Mouse
|
5
|
+
extend FFI::Library
|
6
|
+
ffi_lib '/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics'
|
7
|
+
|
8
|
+
class CGPoint < FFI::Struct
|
9
|
+
layout :x, :double,
|
10
|
+
:y, :double
|
11
|
+
end
|
12
|
+
|
13
|
+
# Mouse/Keyboard events
|
14
|
+
enum :CGEventType, [ :kCGEventNull, 0, # Null Event
|
15
|
+
:kCGEventLeftMouseDown, 1, # left mouse-down event
|
16
|
+
:kCGEventLeftMouseUp, 2, # left mouse-up event
|
17
|
+
:kCGEventRightMouseDown, 3, # right mouse-down event
|
18
|
+
:kCGEventRightMouseUp, 4, # right mouse-up event
|
19
|
+
:kCGEventMouseMoved, 5, # mouse-moved event
|
20
|
+
:kCGEventLeftMouseDragged, 6, # left mouse-dragged event
|
21
|
+
:kCGEventRightMouseDragged, 7, # right mouse-dragged event
|
22
|
+
:kCGEventScrollWheel, 22, # mouse scrolled event
|
23
|
+
:kCGEventOtherMouseDown, 25, # other mouse-down event
|
24
|
+
:kCGEventOtherMouseUp, 26, # other mouse-up event
|
25
|
+
:kCGEventOtherMouseDragged, 27, # other mouse-dragged event
|
26
|
+
:kCGEventKeyDown, 10, # key-down event
|
27
|
+
:kCGEventKeyUp, 11 ] # key-up down
|
28
|
+
|
29
|
+
# Mouse buttons
|
30
|
+
enum :CGMouseButton, [ :kCGMouseButtonLeft, 0,
|
31
|
+
:kCGMouseButtonRight,
|
32
|
+
:kCGMouseButtonCenter ]
|
33
|
+
|
34
|
+
# Virtual Key Codes
|
35
|
+
enum :CGKeyCode, [ :return, 0x24,
|
36
|
+
:tab, 0x30,
|
37
|
+
:space, 0x31,
|
38
|
+
:delete, 0x33,
|
39
|
+
:escape, 0x35,
|
40
|
+
:command, 0x37,
|
41
|
+
:left_shift, 0x38,
|
42
|
+
:left_option, 0x3A,
|
43
|
+
:left_control, 0x3B,
|
44
|
+
:right_shift, 0x3C,
|
45
|
+
:right_option, 0x3D,
|
46
|
+
:right_control, 0x3E,
|
47
|
+
:function, 0x3F,
|
48
|
+
:home, 0x73,
|
49
|
+
:end, 0x77,
|
50
|
+
:page_up, 0x74,
|
51
|
+
:page_down, 0x79,
|
52
|
+
:forward_delete, 0x75,
|
53
|
+
:left_arrow, 0x7B,
|
54
|
+
:right_arrow, 0x7C,
|
55
|
+
:down_arrow, 0x7D,
|
56
|
+
:up_arrow, 0x7E ]
|
57
|
+
|
58
|
+
# CGEventTapLocation
|
59
|
+
enum :CGEventTapLocation, [ :kCGHIDEventTap, 0,
|
60
|
+
:kCGSessionEventTap,
|
61
|
+
:kCGAnnotatedSessionEventTap ]
|
62
|
+
|
63
|
+
# Attach to the CGEventCreateMouseEvent function
|
64
|
+
attach_function :CGEventCreateMouseEvent, [ :pointer, :CGEventType, CGPoint.by_value, :CGMouseButton ], :pointer
|
65
|
+
attach_function :CGEventCreateKeyboardEvent, [:pointer, :CGKeyCode, :bool ], :pointer
|
66
|
+
attach_function :CGEventSetType, [:pointer, :CGEventType], :void
|
67
|
+
attach_function :CGEventPost, [ :CGEventTapLocation, :pointer], :void
|
68
|
+
attach_function :CFRelease, [:pointer], :void
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
class Mousetools
|
73
|
+
|
74
|
+
@current_coordinates
|
75
|
+
attr_accessor :current_coordinates
|
76
|
+
|
77
|
+
def initialize
|
78
|
+
@current_coordinates = Hash.new
|
79
|
+
end
|
80
|
+
|
81
|
+
def left_mouse_down(x = nil, y = nil)
|
82
|
+
raise "Coordinates not set" if (x.nil? || y.nil?) && @current_coordinates.nil?
|
83
|
+
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
84
|
+
point = Mouse::CGPoint.new
|
85
|
+
point[:x] = @current_coordinates[:x]
|
86
|
+
point[:y] = @current_coordinates[:y]
|
87
|
+
# Create the event
|
88
|
+
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventLeftMouseDown, point, :kCGMouseButtonLeft)
|
89
|
+
# Post the event
|
90
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
91
|
+
# Release the event ref
|
92
|
+
Mouse::CFRelease(event)
|
93
|
+
end
|
94
|
+
|
95
|
+
def left_mouse_up(x = nil, y = nil)
|
96
|
+
raise "Coordinates not set" if (x.nil? || y.nil?) && @current_coordinates.nil?
|
97
|
+
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
98
|
+
point = Mouse::CGPoint.new
|
99
|
+
point[:x] = @current_coordinates[:x]
|
100
|
+
point[:y] = @current_coordinates[:y]
|
101
|
+
# Create the event
|
102
|
+
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventLeftMouseUp, point, :kCGMouseButtonLeft)
|
103
|
+
# Post the event
|
104
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
105
|
+
# Release the event ref
|
106
|
+
Mouse::CFRelease(event)
|
107
|
+
end
|
108
|
+
|
109
|
+
def right_mouse_down(x = nil, y = nil)
|
110
|
+
raise "Coordinates not set" if (x.nil? || y.nil?) && @current_coordinates.nil?
|
111
|
+
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
112
|
+
point = Mouse::CGPoint.new
|
113
|
+
point[:x] = @current_coordinates[:x]
|
114
|
+
point[:y] = @current_coordinates[:y]
|
115
|
+
# Create the event
|
116
|
+
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventRightMouseDown, point, :kCGMouseButtonRight)
|
117
|
+
# Post the event
|
118
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
119
|
+
# Release the event ref
|
120
|
+
Mouse::CFRelease(event)
|
121
|
+
end
|
122
|
+
|
123
|
+
def right_mouse_up(x = nil, y = nil)
|
124
|
+
raise "Coordinates not set" if (x.nil? || y.nil?) && @current_coordinates.nil?
|
125
|
+
@current_coordinates = {:x => x, :y => y} if (!x.nil? && !y.nil?)
|
126
|
+
point = Mouse::CGPoint.new
|
127
|
+
point[:x] = @current_coordinates[:x]
|
128
|
+
point[:y] = @current_coordinates[:y]
|
129
|
+
# Create the event
|
130
|
+
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventRightMouseUp, point, :kCGMouseButtonRight)
|
131
|
+
# Post the event
|
132
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
133
|
+
# Release the event ref
|
134
|
+
Mouse::CFRelease(event)
|
135
|
+
end
|
136
|
+
|
137
|
+
def move_cursor(x = nil, y = nil)
|
138
|
+
raise "Must provide new coordinates to move the cursor" if (x.nil? || y.nil?)
|
139
|
+
point = Mouse::CGPoint.new
|
140
|
+
point[:x] = x
|
141
|
+
point[:y] = y
|
142
|
+
# Create the event
|
143
|
+
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventMouseMoved, point, :kCGMouseButtonLeft)
|
144
|
+
# Post the event
|
145
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
146
|
+
@current_coordinates = {:x => x, :y => y}
|
147
|
+
# Release the event ref
|
148
|
+
Mouse::CFRelease(event)
|
149
|
+
end
|
150
|
+
|
151
|
+
def left_mouse_drag(x = nil, y = nil)
|
152
|
+
raise "Must provide new coordinates to drag the cursor" if (x.nil? || y.nil?)
|
153
|
+
point = Mouse::CGPoint.new
|
154
|
+
point[:x] = x
|
155
|
+
point[:y] = y
|
156
|
+
# Create the event
|
157
|
+
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventLeftMouseDragged, point, :kCGMouseButtonLeft)
|
158
|
+
# Post the event
|
159
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
160
|
+
@current_coordinates = {:x => x, :y => y}
|
161
|
+
# Release the event ref
|
162
|
+
Mouse::CFRelease(event)
|
163
|
+
end
|
164
|
+
|
165
|
+
def right_mouse_drag(x = nil, y = nil)
|
166
|
+
raise "Must provide new coordinates to drag the cursor" if (x.nil? || y.nil?)
|
167
|
+
point = Mouse::CGPoint.new
|
168
|
+
point[:x] = x
|
169
|
+
point[:y] = y
|
170
|
+
# Create the event
|
171
|
+
event = Mouse::CGEventCreateMouseEvent(nil, :kCGEventRightMouseDragged, point, :kCGMouseButtonRight)
|
172
|
+
# Post the event
|
173
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
174
|
+
@current_coordinates = {:x => x, :y => y}
|
175
|
+
# Release the event ref
|
176
|
+
Mouse::CFRelease(event)
|
177
|
+
end
|
178
|
+
|
179
|
+
def key_down(virtual_key = nil)
|
180
|
+
raise "Must provide a key" if virtual_key.nil?
|
181
|
+
keycode = virtual_key.intern
|
182
|
+
event = Mouse::CGEventCreateKeyboardEvent(nil, keycode, true)
|
183
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
184
|
+
Mouse::CFRelease(event)
|
185
|
+
end
|
186
|
+
|
187
|
+
def key_up(virtual_key = nil)
|
188
|
+
raise "Must provide a key" if virtual_key.nil?
|
189
|
+
keycode = virtual_key.intern
|
190
|
+
event = Mouse::CGEventCreateKeyboardEvent(nil, keycode, false)
|
191
|
+
Mouse::CGEventPost(:kCGHIDEventTap, event)
|
192
|
+
Mouse::CFRelease(event)
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mousetools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Scott Larson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-04-16 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A gem that allows for basic mouse-manipulation functionality in OS X (including left/right click, dragging, key-click, etc.)
|
22
|
+
email: scottl@mozy.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/mousetools.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://rubygems.org/gems/mousetools
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.6
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Mousetools for Mac
|
61
|
+
test_files: []
|
62
|
+
|