mac-event-monitor 0.0.3 → 0.1.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.
@@ -2,12 +2,10 @@
2
2
  #import <AppKit/AppKit.h>
3
3
 
4
4
  @interface EventMonitorAppDelegate : NSObject <NSApplicationDelegate> {
5
- id eventMonitor;
6
5
  }
7
6
 
8
7
  -(void)onTimeout:(NSTimer *) timer;
9
8
 
10
- @property (assign) VALUE rb_monitor;
11
-
12
9
  @end
13
10
 
11
+ NSEventMask mask;
@@ -1,42 +1,37 @@
1
1
  #import "event_monitor.h"
2
2
 
3
- @implementation EventMonitorAppDelegate
3
+ NSEventMask mask =
4
+ NSLeftMouseUpMask
5
+ | NSRightMouseUpMask
6
+ | NSOtherMouseUpMask
7
+ | NSLeftMouseDownMask
8
+ | NSRightMouseDownMask
9
+ | NSOtherMouseDownMask
10
+ | NSLeftMouseDraggedMask
11
+ | NSRightMouseDraggedMask
12
+ | NSOtherMouseDraggedMask
13
+ | NSMouseMovedMask
14
+ | NSKeyDownMask
15
+ | NSKeyUpMask
16
+ // | NSScrollWheelMask
17
+ // | NSTabletPointMask
18
+ // | NSTabletProximityMask
19
+ ;
4
20
 
5
- @synthesize rb_monitor;
21
+ @implementation EventMonitorAppDelegate
6
22
 
7
23
  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
8
- NSEventMask mask;
9
-
10
- mask =
11
- NSLeftMouseUpMask
12
- | NSRightMouseUpMask
13
- | NSOtherMouseUpMask
14
- | NSLeftMouseDownMask
15
- | NSRightMouseDownMask
16
- | NSOtherMouseDownMask
17
- | NSLeftMouseDraggedMask
18
- | NSRightMouseDraggedMask
19
- | NSOtherMouseDraggedMask
20
- | NSMouseMovedMask
21
- // | NSScrollWheelMask
22
- // | NSTabletPointMask
23
- // | NSTabletProximityMask
24
- // | NSKeyDownMask
25
- ;
26
-
27
- eventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:mask
28
- handler:^(NSEvent *incomingEvent) {
29
- rb_funcall(rb_monitor, rb_intern("receive_event"), 1, rb_str_new2([[incomingEvent description] UTF8String]));
30
- }];
24
+ [NSEvent addGlobalMonitorForEventsMatchingMask:mask
25
+ handler:^(NSEvent *incomingEvent) {}];
31
26
  }
32
27
 
33
28
  - (void)onTimeout:(NSTimer *)timer {
34
29
  NSEvent *event;
35
30
 
31
+ [NSEvent removeMonitor:[timer userInfo]];
36
32
  [timer release];
37
33
 
38
34
  [[NSApplication sharedApplication] stop:nil];
39
- //[NSEvent removeMonitor:eventMonitor];
40
35
 
41
36
  // http://www.cocoabuilder.com/archive/cocoa/219842-nsapp-stop.html
42
37
  event = [NSEvent otherEventWithType: NSApplicationDefined
@@ -60,20 +55,25 @@ static VALUE cMonitor_run_app(int argc, VALUE *argv, VALUE self)
60
55
  {
61
56
  EventMonitorAppDelegate *delegate;
62
57
  VALUE stopAfter;
58
+ id eventMonitor;
63
59
 
64
60
  rb_scan_args(argc, argv, "1", &stopAfter);
65
61
 
66
62
  if(!(delegate = [[NSApplication sharedApplication] delegate])) {
67
63
  delegate = [[EventMonitorAppDelegate alloc] init];
68
- delegate.rb_monitor = self;
69
64
  [NSApp setDelegate: delegate];
70
65
  }
71
66
 
67
+ eventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:mask
68
+ handler:^(NSEvent *incomingEvent) {
69
+ rb_funcall(self, rb_intern("receive_event"), 1, rb_str_new2([[incomingEvent description] UTF8String]));
70
+ }];
71
+
72
72
  if(stopAfter != Qnil) {
73
73
  [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)NUM2INT(stopAfter)
74
74
  target:delegate
75
75
  selector:@selector(onTimeout:)
76
- userInfo:nil
76
+ userInfo:eventMonitor
77
77
  repeats:NO];
78
78
  }
79
79
 
@@ -12,5 +12,11 @@ dir_config(extension_name)
12
12
 
13
13
  $LDFLAGS += ' -framework AppKit'
14
14
 
15
+ begin
16
+ MACRUBY_VERSION # Only MacRuby has this constant.
17
+ $CFLAGS += ' -fobjc-gc' # Enable MacOSX's GC for MacRuby
18
+ rescue
19
+ end
20
+
15
21
  # Do the work
16
22
  create_makefile(extension_name)
@@ -0,0 +1,48 @@
1
+ module Mac
2
+ module EventMonitor
3
+ class KeyboardEvent < Event
4
+ attr_reader :keycode, :flags
5
+
6
+ def initialize(type, keycode, flags)
7
+ super(type)
8
+
9
+ @keycode = keycode.to_i
10
+ @flags = flags.to_i(16)
11
+ end
12
+
13
+ def shift_key?
14
+ check_flag(16) || check_flag(17)
15
+ end
16
+
17
+ def ctrl_key?
18
+ check_flag(18)
19
+ end
20
+
21
+ def alt_key?
22
+ check_flag(19)
23
+ end
24
+
25
+ def command_key?
26
+ check_flag(20)
27
+ end
28
+
29
+ def num_pad_key?
30
+ check_flag(21)
31
+ end
32
+
33
+ def help_key?
34
+ check_flag(22)
35
+ end
36
+
37
+ def function_key?
38
+ check_flag(23)
39
+ end
40
+
41
+ private
42
+
43
+ def check_flag(shift)
44
+ !(flags & (1 << shift)).zero?
45
+ end
46
+ end
47
+ end
48
+ end
@@ -5,10 +5,10 @@ module Mac
5
5
 
6
6
  class << self
7
7
  def create_from_description(description)
8
- _, *atts_as_string = description.split(/\s+/)
8
+ _, *atts_as_string = description.split(/ +/)
9
9
 
10
- attrs = atts_as_string.inject({}) do |result, attr_as_string|
11
- name, value = attr_as_string.scan(/^([^=]+)=(.+)$/)[0]
10
+ attrs = (atts_as_string.join(' ') + ' ').scan(/([^=]+)=([^=]+) (?=\w?)/).inject({}) do |result, pair|
11
+ name, value = pair
12
12
  result[name.to_sym] = value
13
13
  result
14
14
  end
@@ -34,6 +34,10 @@ module Mac
34
34
  MouseEvent.new(:mouse_drag, attrs[:loc], :right)
35
35
  when 'OMouseDragged'
36
36
  MouseEvent.new(:mouse_drag, attrs[:loc], :other)
37
+ when 'KeyDown'
38
+ KeyboardEvent.new(:key_down, attrs[:keyCode], attrs[:flags])
39
+ when 'KeyUp'
40
+ KeyboardEvent.new(:key_up, attrs[:keyCode], attrs[:flags])
37
41
  end
38
42
  end
39
43
  end
@@ -1,5 +1,5 @@
1
1
  module Mac
2
2
  module EventMonitor
3
- VERSION = "0.0.3"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -9,3 +9,4 @@ require 'event_monitor'
9
9
  require 'mac-event-monitor/monitor.rb'
10
10
  require 'mac-event-monitor/event.rb'
11
11
  require 'mac-event-monitor/event/mouse_event.rb'
12
+ require 'mac-event-monitor/event/keyboard_event.rb'
@@ -12,4 +12,13 @@ describe Event do
12
12
  event.location.x.should be_an_instance_of(Float)
13
13
  event.location.y.should be_an_instance_of(Float)
14
14
  end
15
+
16
+ it 'should create event from description with space' do
17
+ 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')
18
+
19
+ event.should be_an_instance_of(KeyboardEvent)
20
+ event.type.should equal(:key_down)
21
+ event.keycode.should equal(49)
22
+ event.shift_key?.should be_true
23
+ end
15
24
  end
@@ -3,16 +3,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  include Mac::EventMonitor
4
4
 
5
5
  describe Monitor do
6
- subject do
7
- Monitor.new
6
+ before do
7
+ @monitor = Monitor.new
8
8
  end
9
9
 
10
10
  it 'should monitor mouse down events' do
11
11
  result = 0
12
12
  robot = Mac::Robot.new
13
13
 
14
- monitor = subject
15
- monitor.add_listener(:mouse_down) do |event|
14
+ @monitor.add_listener(:mouse_down) do |event|
16
15
  result += 1
17
16
  end
18
17
 
@@ -30,7 +29,37 @@ describe Monitor do
30
29
  end
31
30
 
32
31
  EM.add_periodic_timer(0.1) do
33
- monitor.run(0.1)
32
+ @monitor.run(0.1)
33
+ end
34
+ end
35
+
36
+ result.should be >= 2
37
+ end
38
+
39
+ it 'should monitor key down events' do
40
+ result = 0
41
+ robot = Mac::Robot.new
42
+
43
+ @monitor.add_listener(:key_down) do |event|
44
+ result += 1
45
+ end
46
+
47
+ result.should be_zero
48
+
49
+ EM.run do
50
+ [1, 1.5].each do |t|
51
+ EM.add_timer(t) do
52
+ robot.key_press(0x04)
53
+ #robot.key_release(0x04)
54
+ end
55
+ end
56
+
57
+ EM.add_timer(2) do
58
+ EM.stop
59
+ end
60
+
61
+ EM.add_periodic_timer(0.1) do
62
+ @monitor.run(0.1)
34
63
  end
35
64
  end
36
65
 
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.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-16 00:00:00.000000000 Z
12
+ date: 2012-07-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70325557283420 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 2.8.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70325557283420
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: mac-robot
27
- requirement: &70325557283000 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70325557283000
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: eventmachine
38
- requirement: &70325557282540 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70325557282540
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  description: A Library to Monitor User Interactions
48
63
  email:
49
64
  - youpy@buycheapviagraonlinenow.com
@@ -64,6 +79,7 @@ files:
64
79
  - ext/lib/mkmf.rb
65
80
  - lib/mac-event-monitor.rb
66
81
  - lib/mac-event-monitor/event.rb
82
+ - lib/mac-event-monitor/event/keyboard_event.rb
67
83
  - lib/mac-event-monitor/event/mouse_event.rb
68
84
  - lib/mac-event-monitor/monitor.rb
69
85
  - lib/mac-event-monitor/version.rb
@@ -85,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
101
  version: '0'
86
102
  segments:
87
103
  - 0
88
- hash: 3251763459567005321
104
+ hash: -3347223318557235811
89
105
  required_rubygems_version: !ruby/object:Gem::Requirement
90
106
  none: false
91
107
  requirements:
@@ -94,10 +110,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
110
  version: '0'
95
111
  segments:
96
112
  - 0
97
- hash: 3251763459567005321
113
+ hash: -3347223318557235811
98
114
  requirements: []
99
115
  rubyforge_project:
100
- rubygems_version: 1.8.10
116
+ rubygems_version: 1.8.24
101
117
  signing_key:
102
118
  specification_version: 3
103
119
  summary: A Library to Monitor User Interactions