mac-robot 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -13,6 +13,7 @@ A Library to Automate User Interactions
13
13
  robot.mouse_press
14
14
  robot.mouse_move(250, 250)
15
15
  robot.mouse_release
16
+ robot.scroll_wheel(:x => 10, :y => -20, :z => 5)
16
17
 
17
18
  === Automate keyboard
18
19
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
@@ -36,6 +36,27 @@ CGEventType getEventTypeFromValue(VALUE value)
36
36
  return type;
37
37
  }
38
38
 
39
+ CGScrollEventUnit getScrollEventUnitFromValue(VALUE value)
40
+ {
41
+ const char *unitStr;
42
+ CGScrollEventUnit unit;
43
+
44
+ unitStr = StringValuePtr(value);
45
+
46
+ if(strncmp(unitStr, "pix", 3) == 0) {
47
+ // accepts pixel, pixels, pix
48
+ unit = kCGScrollEventUnitPixel;
49
+ } else if(strncmp(unitStr, "line", 4) == 0) {
50
+ // accepts line, lines
51
+ unit = kCGScrollEventUnitLine;
52
+ } else {
53
+ rb_warn("Given unit `%s' is invalid. pixel or line is allowed.", unitStr);
54
+ unit = kCGScrollEventUnitPixel;
55
+ }
56
+
57
+ return unit;
58
+ }
59
+
39
60
  CGMouseButton getMouseButtonFromValue(VALUE value)
40
61
  {
41
62
  // http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html
@@ -78,6 +99,29 @@ static VALUE cEventDispatcher_dispatchMouseEvent(int argc, VALUE *argv, VALUE se
78
99
  return Qnil;
79
100
  }
80
101
 
102
+ // http://stackoverflow.com/questions/6126226/how-to-create-an-nsevent-of-type-nsscrollwheel
103
+ // wheel_cnt: 1 - Y, 2 - Y-X, 3 - Y-X-Z
104
+ static VALUE cEventDispatcher_dispatchScrollWheelEvent(int argc, VALUE *argv, VALUE self)
105
+ {
106
+ VALUE unit, wheel_cnt, scroll_y, scroll_x, scroll_z;
107
+ CGEventRef event;
108
+
109
+ rb_scan_args(argc, argv, "5", &unit, &wheel_cnt, &scroll_y, &scroll_x, &scroll_z);
110
+
111
+ event = CGEventCreateScrollWheelEvent(
112
+ NULL,
113
+ getScrollEventUnitFromValue(rb_funcall(unit, rb_intern("to_s"), 0)),
114
+ NUM2INT(wheel_cnt),
115
+ NUM2INT(scroll_y),
116
+ NUM2INT(scroll_x),
117
+ NUM2INT(scroll_z));
118
+
119
+ CGEventPost(kCGHIDEventTap, event);
120
+ CFRelease(event);
121
+
122
+ return Qnil;
123
+ }
124
+
81
125
  // http://forums.macrumors.com/showthread.php?t=780577
82
126
  static VALUE cEventDispatcher_dispatchKeyboardEvent(int argc, VALUE *argv, VALUE self)
83
127
  {
@@ -105,5 +149,6 @@ void Init_event_dispatcher(void){
105
149
  rb_cEventDispatcher = rb_define_class_under(rb_cRobot, "EventDispatcher", rb_cObject);
106
150
  rb_define_singleton_method(rb_cEventDispatcher, "new", cEventDispatcher_new, -1);
107
151
  rb_define_method(rb_cEventDispatcher, "dispatchMouseEvent", cEventDispatcher_dispatchMouseEvent, -1);
152
+ rb_define_method(rb_cEventDispatcher, "dispatchScrollWheelEvent", cEventDispatcher_dispatchScrollWheelEvent, -1);
108
153
  rb_define_method(rb_cEventDispatcher, "dispatchKeyboardEvent", cEventDispatcher_dispatchKeyboardEvent, -1);
109
154
  }
data/lib/mac-robot.rb CHANGED
@@ -46,6 +46,15 @@ module Mac
46
46
  keyboard_event(keycode, 0)
47
47
  end
48
48
 
49
+ def scroll_wheel_line(args = {})
50
+ scroll_wheel_event(:line, *scroll_count_extract(args))
51
+ end
52
+ alias scroll_wheel scroll_wheel_line
53
+
54
+ def scroll_wheel_pixel(args = {})
55
+ scroll_wheel_event(:pixel, *scroll_count_extract(args))
56
+ end
57
+
49
58
  def get_pixel_color(x, y)
50
59
  raise OutOfResolution if x < 0 || y < 0
51
60
  raise OutOfResolution if display_pixel_size.width < x || display_pixel_size.height < y
@@ -64,6 +73,32 @@ module Mac
64
73
  @dispatcher.dispatchMouseEvent(@x, @y, BUTTONS[button], type)
65
74
  end
66
75
 
76
+ def scroll_wheel_event(unit, scroll_y, scroll_x = 0, scroll_z = 0)
77
+ wheel_count = 3
78
+ @dispatcher.dispatchScrollWheelEvent(unit, wheel_count, scroll_y, scroll_x, scroll_z)
79
+ end
80
+
81
+ def scroll_count_extract(args = {})
82
+ # java.awt.Robot
83
+ # http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Robot.html#mouseWheel%28int%29
84
+ scroll_x = 0 # x > 0 => right
85
+ scroll_y = 0 # y > 0 => down
86
+ scroll_z = 0 # z > 0 => towards
87
+
88
+ args.each do |key, value|
89
+ case key
90
+ when :x
91
+ scroll_x -= value
92
+ when :y
93
+ scroll_y -= value
94
+ when :z
95
+ scroll_z -= value
96
+ end
97
+ end
98
+
99
+ return [scroll_y, scroll_x, scroll_z]
100
+ end
101
+
67
102
  def keyboard_event(keycode, keydown)
68
103
  @dispatcher.dispatchKeyboardEvent(keycode, keydown)
69
104
  end
data/mac-robot.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mac-robot"
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["youpy"]
12
- s.date = "2012-06-17"
12
+ s.date = "2012-09-18"
13
13
  s.description = "A Library to Automate User Interactions"
14
14
  s.email = "youpy@buycheapviagraonlinenow.com"
15
15
  s.extensions = ["ext/event_dispatcher/extconf.rb", "ext/util/extconf.rb"]
@@ -15,6 +15,15 @@ describe Mac::Robot do
15
15
  end
16
16
  end
17
17
 
18
+ describe 'scroll wheel event' do
19
+ it 'should simulate scroll wheel event' do
20
+ subject.scroll_wheel_line(:y => 10) # down 10 lines
21
+ subject.scroll_wheel(:y => -10) # up 10 lines
22
+ subject.scroll_wheel_pixel(:x => 10, :y => -20, :z => 5)
23
+ subject.scroll_wheel_pixel(:y => 20, :x => -10, :z => -5)
24
+ end
25
+ end
26
+
18
27
  describe 'keyboard event' do
19
28
  it 'should simulate keyboard event' do
20
29
  [0x04, 0x0e, 0x25, 0x25, 0x1f].each do |keycode|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mac-robot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-17 00:00:00.000000000 Z
12
+ date: 2012-09-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  segments:
105
105
  - 0
106
- hash: 3702793379613243156
106
+ hash: 625334772670347707
107
107
  required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  none: false
109
109
  requirements: