Class: Konamio::Sequence::Listener
- Includes:
- KeyMap
- Defined in:
- lib/konamio/sequence/listener.rb
Instance Method Summary collapse
- #execute! ⇒ Object
-
#initialize(options) ⇒ Listener
constructor
A new instance of Listener.
- #listen ⇒ Object
Methods included from KeyMap
Constructor Details
#initialize(options) ⇒ Listener
Returns a new instance of Listener.
6 7 8 9 |
# File 'lib/konamio/sequence/listener.rb', line 6 def initialize() = { input: $stdin, debounce: 0.0001 }.merge() (:sequence, ) end |
Instance Method Details
#execute! ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/konamio/sequence/listener.rb', line 11 def execute! case listen when true return Konamio::Result.new(success: true, data: { sequence: @sequence[1..-1]}) when false return Konamio::Result.new(success: false, data: { sequence: @sequence }) when :negative return Konamio::Result.new(success: false, data: { sequence: :negative }) end end |
#listen ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/konamio/sequence/listener.rb', line 22 def listen input = @input.getch if(input == "\e") extra_thread = Thread.new{ input << @input.getch input << @input.getch } # wait just long enough for special keys to get swallowed extra_thread.join(@debounce) # kill thread so not-so-long special keys don't wait on getc extra_thread.kill end case input when sequence_for(@sequence[0]) true when "\e" :negative else false end end |