Class: Deskbot::Event
- Inherits:
-
Object
- Object
- Deskbot::Event
- Defined in:
- lib/deskbot/event.rb
Overview
Base class for the distinct device events yielded by Screen#listen.
Listening yields an instance of one of the concrete subclasses, each of which exposes only the fields relevant to it:
- KeyDown / KeyUp - expose
key - MouseDown / MouseUp - expose
button - MouseMove - exposes
x,yandcoords
Dispatch on the event's class rather than on a type value:
Deskbot.screen.listen do |event|
case event
when Deskbot::Event::KeyDown then puts "pressed #{event.key}"
when Deskbot::Event::KeyUp then puts "released #{event.key}"
when Deskbot::Event::MouseDown then puts "button down: #{event.button}"
when Deskbot::Event::MouseUp then puts "button up: #{event.button}"
when Deskbot::Event::MouseMove then puts event.coords.inspect
end
end
Base Event is abstract and is never instantiated directly; use
Event.build to turn a native payload into the matching subclass.
Defined Under Namespace
Classes: KeyDown, KeyUp, MouseDown, MouseMove, MouseUp
Instance Attribute Summary collapse
-
#recorded_at ⇒ Object
readonly
The time the event was recorded.
Class Method Summary collapse
-
.build(payload) ⇒ Object
Builds the concrete event described by the raw hash yielded by the native listener.
-
.recorded_at_from(payload) ⇒ Object
Builds the recorded-at
Timefor an event from its native payload.
Instance Attribute Details
#recorded_at ⇒ Object (readonly)
The time the event was recorded. Every concrete event exposes it.
29 30 31 |
# File 'lib/deskbot/event.rb', line 29 def recorded_at @recorded_at end |
Class Method Details
.build(payload) ⇒ Object
Builds the concrete event described by the raw hash yielded by the native listener.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/deskbot/event.rb', line 33 def self.build(payload) subclass = { "key_down" => Event::KeyDown, "key_up" => Event::KeyUp, "mouse_down" => Event::MouseDown, "mouse_up" => Event::MouseUp, "mouse_move" => Event::MouseMove }[payload["type"]] unless subclass raise ArgumentError, "unknown event type: #{payload["type"].inspect}" end subclass.build(payload) end |
.recorded_at_from(payload) ⇒ Object
Builds the recorded-at Time for an event from its native payload.
51 52 53 |
# File 'lib/deskbot/event.rb', line 51 def self.recorded_at_from(payload) Time.at(payload["recorded_at"]) end |