Class: Mac::EventMonitor::Event

Inherits:
Object
  • Object
show all
Includes:
JsonSerializable
Defined in:
lib/mac-event-monitor/event.rb

Direct Known Subclasses

KeyboardEvent, MouseEvent

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonSerializable

included, #to_json

Constructor Details

#initialize(type, time, location) ⇒ Event

Returns a new instance of Event.



54
55
56
57
58
# File 'lib/mac-event-monitor/event.rb', line 54

def initialize(type, time, location)
  @type     = type.to_sym
  @time     = time
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



6
7
8
# File 'lib/mac-event-monitor/event.rb', line 6

def location
  @location
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/mac-event-monitor/event.rb', line 6

def time
  @time
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/mac-event-monitor/event.rb', line 6

def type
  @type
end

Class Method Details

.create_from_description(description, time, screen_height) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mac-event-monitor/event.rb', line 9

def create_from_description(description, time, screen_height)
  _, *atts_as_string = description.split(/ +/)

  attrs = (atts_as_string.join(' ') + ' ').scan(/([^=]+)=([^=]+) (?=\w?)/).inject({}) do |result, pair|
    name, value = pair
    result[name.to_sym] = value
    result
  end

  location = parse_location(attrs[:loc])
  location.y = screen_height - location.y

  case attrs[:type]
  when 'LMouseUp'
    MouseEvent.new(:mouse_up,    time, location, :left)
  when 'RMouseUp'
    MouseEvent.new(:mouse_up,    time, location, :right)
  when 'OMouseUp'
    MouseEvent.new(:mouse_up,    time, location, :other)
  when 'LMouseDown'
    MouseEvent.new(:mouse_down,  time, location, :left)
  when 'RMouseDown'
    MouseEvent.new(:mouse_down,  time, location, :right)
  when 'OMouseDown'
    MouseEvent.new(:mouse_down,  time, location, :other)
  when 'MouseMoved'
    MouseEvent.new(:mouse_move,  time, location, nil)
  when 'LMouseDragged'
    MouseEvent.new(:mouse_drag,  time, location, :left)
  when 'RMouseDragged'
    MouseEvent.new(:mouse_drag,  time, location, :right)
  when 'OMouseDragged'
    MouseEvent.new(:mouse_drag,  time, location, :other)
  when 'KeyDown'
    KeyboardEvent.new(:key_down, time, location, attrs[:keyCode], attrs[:flags])
  when 'KeyUp'
    KeyboardEvent.new(:key_up,   time, location, attrs[:keyCode], attrs[:flags])
  end
end

.parse_location(location_as_str) ⇒ Object



49
50
51
# File 'lib/mac-event-monitor/event.rb', line 49

def parse_location(location_as_str)
  Location.new(*location_as_str.scan(/[\d\.]+/).map {|v| v.to_f })
end

Instance Method Details

#dataObject



60
61
62
# File 'lib/mac-event-monitor/event.rb', line 60

def data
  [type, time, location]
end