mac-event-monitor 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -29,6 +29,52 @@ You need to enable "Access to assistive devices" in the Universal Access prefere
29
29
  end
30
30
  monitor.run
31
31
 
32
+ ### Record/Play Events
33
+
34
+ ```
35
+ ruby recorder.rb | ruby player.rb
36
+ ```
37
+
38
+ recorder.rb
39
+
40
+ ```ruby
41
+ require 'mac-event-monitor'
42
+ require 'json'
43
+
44
+ e = Mac::EventMonitor::Monitor.new
45
+ events = []
46
+ e.add_listener {|e|
47
+ events << e
48
+ }
49
+ e.run(3)
50
+
51
+ puts events.to_json
52
+ ```
53
+
54
+ player.rb
55
+
56
+ ```ruby
57
+ require 'mac-event-monitor'
58
+ require 'mac-robot'
59
+ require 'json'
60
+
61
+ events = JSON.parse(ARGF.read)
62
+ robot = Mac::Robot.new
63
+
64
+ return if events.size == 0
65
+
66
+ events.each_with_index do |event, index|
67
+ case event.type
68
+ when :mouse_move
69
+ robot.mouse_move(event.location.x, event.location.y)
70
+ end
71
+
72
+ if n = events[index + 1]
73
+ sleep n.time - event.time
74
+ end
75
+ end
76
+ ```
77
+
32
78
  ## Contributing
33
79
 
34
80
  1. Fork it
@@ -83,7 +83,12 @@ static VALUE cMonitor_run_app(int argc, VALUE *argv, VALUE self)
83
83
 
84
84
  eventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:mask
85
85
  handler:^(NSEvent *incomingEvent) {
86
- rb_funcall(self, rb_intern("receive_event"), 2, rb_str_new2([[incomingEvent description] UTF8String]), rb_float_new(getScreenHeight()));
86
+ rb_funcall(self,
87
+ rb_intern("receive_event"),
88
+ 3,
89
+ rb_str_new2([[incomingEvent description] UTF8String]),
90
+ rb_float_new([incomingEvent timestamp]),
91
+ rb_float_new(getScreenHeight()));
87
92
  }];
88
93
 
89
94
  if(stopAfter != Qnil) {
@@ -6,7 +6,7 @@ module Mac
6
6
  attr_reader :type, :time, :location
7
7
 
8
8
  class << self
9
- def create_from_description(description, screen_height)
9
+ def create_from_description(description, time, screen_height)
10
10
  _, *atts_as_string = description.split(/ +/)
11
11
 
12
12
  attrs = (atts_as_string.join(' ') + ' ').scan(/([^=]+)=([^=]+) (?=\w?)/).inject({}) do |result, pair|
@@ -18,8 +18,6 @@ module Mac
18
18
  location = parse_location(attrs[:loc])
19
19
  location.y = screen_height - location.y
20
20
 
21
- time = attrs[:time].to_f
22
-
23
21
  case attrs[:type]
24
22
  when 'LMouseUp'
25
23
  MouseEvent.new(:mouse_up, time, location, :left)
@@ -19,8 +19,8 @@ module Mac
19
19
  run_app(stop_after)
20
20
  end
21
21
 
22
- def receive_event(str, screen_height)
23
- event = Event.create_from_description(str, screen_height)
22
+ def receive_event(str, time, screen_height)
23
+ event = Event.create_from_description(str, time, screen_height)
24
24
 
25
25
  (@listeners[event.type] || []).each do |block|
26
26
  block.call(event)
@@ -1,5 +1,5 @@
1
1
  module Mac
2
2
  module EventMonitor
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -7,18 +7,18 @@ include Mac::EventMonitor
7
7
  describe Event do
8
8
  describe '.create_from_description' do
9
9
  it 'should create event from description' do
10
- event = Event.create_from_description('NSEvent: type=LMouseDown loc=(618,524) time=1136789.4 flags=0x100 win=0x0 winNum=17310 ctxt=0x0 evNum=14883 click=1 buttonNumber=0 pressure=1', 768)
10
+ event = Event.create_from_description('NSEvent: type=LMouseDown loc=(618,524) time=1136789.4 flags=0x100 win=0x0 winNum=17310 ctxt=0x0 evNum=14883 click=1 buttonNumber=0 pressure=1', 1136789.5, 768)
11
11
 
12
12
  event.should be_an_instance_of(MouseEvent)
13
13
  event.type.should equal(:mouse_down)
14
14
  event.button.should equal(:left)
15
15
  event.location.x.should eql(618.0)
16
16
  event.location.y.should eql(244.0)
17
- event.time.should eql(1136789.4)
17
+ event.time.should eql(1136789.5)
18
18
  end
19
19
 
20
20
  it 'should create event from description with space' do
21
- event = Event.create_from_description('NSEvent: type=KeyDown loc=(184,646) time=68067.5 flags=0x20104 win=0x0 winNum=0 ctxt=0x0 chars=" " unmodchars=" " repeat=0 keyCode=49', 768)
21
+ event = Event.create_from_description('NSEvent: type=KeyDown loc=(184,646) time=68067.5 flags=0x20104 win=0x0 winNum=0 ctxt=0x0 chars=" " unmodchars=" " repeat=0 keyCode=49', 68067.6, 768)
22
22
 
23
23
  event.should be_an_instance_of(KeyboardEvent)
24
24
  event.type.should equal(:key_down)
@@ -26,13 +26,13 @@ describe Event do
26
26
  event.shift_key?.should be_true
27
27
  event.location.x.should eql(184.0)
28
28
  event.location.y.should eql(122.0)
29
- event.time.should eql(68067.5)
29
+ event.time.should eql(68067.6)
30
30
  end
31
31
  end
32
32
 
33
33
  describe '#to_json' do
34
34
  it 'serializes mouse event to json' do
35
- event = Event.create_from_description('NSEvent: type=LMouseDown loc=(618,524) time=1136789.4 flags=0x100 win=0x0 winNum=17310 ctxt=0x0 evNum=14883 click=1 buttonNumber=0 pressure=1', 768)
35
+ event = Event.create_from_description('NSEvent: type=LMouseDown loc=(618,524) time=1136789.4 flags=0x100 win=0x0 winNum=17310 ctxt=0x0 evNum=14883 click=1 buttonNumber=0 pressure=1', 1136789.44, 768)
36
36
  event = JSON.parse(event.to_json)
37
37
 
38
38
  event.should be_an_instance_of(MouseEvent)
@@ -40,11 +40,11 @@ describe Event do
40
40
  event.button.should equal(:left)
41
41
  event.location.x.should eql(618.0)
42
42
  event.location.y.should eql(244.0)
43
- event.time.should eql(1136789.4)
43
+ event.time.should eql(1136789.44)
44
44
  end
45
45
 
46
46
  it 'serializes mouse event to json' do
47
- event = Event.create_from_description('NSEvent: type=KeyDown loc=(184,646) time=68067.5 flags=0x20104 win=0x0 winNum=0 ctxt=0x0 chars=" " unmodchars=" " repeat=0 keyCode=49', 768)
47
+ event = Event.create_from_description('NSEvent: type=KeyDown loc=(184,646) time=68067.5 flags=0x20104 win=0x0 winNum=0 ctxt=0x0 chars=" " unmodchars=" " repeat=0 keyCode=49', 68067.55, 768)
48
48
  event = JSON.parse(event.to_json)
49
49
 
50
50
  event.should be_an_instance_of(KeyboardEvent)
@@ -53,7 +53,7 @@ describe Event do
53
53
  event.shift_key?.should be_true
54
54
  event.location.x.should eql(184.0)
55
55
  event.location.y.should eql(122.0)
56
- event.time.should eql(68067.5)
56
+ event.time.should eql(68067.55)
57
57
  end
58
58
  end
59
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mac-event-monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -135,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  segments:
137
137
  - 0
138
- hash: 420514610567595612
138
+ hash: 1801418130216692130
139
139
  required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  none: false
141
141
  requirements:
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  version: '0'
145
145
  segments:
146
146
  - 0
147
- hash: 420514610567595612
147
+ hash: 1801418130216692130
148
148
  requirements: []
149
149
  rubyforge_project:
150
150
  rubygems_version: 1.8.24