Class: Dispel::Keyboard

Inherits:
Object
  • Object
show all
Defined in:
lib/dispel/keyboard.rb

Constant Summary collapse

MAX_CHAR =
255
ENTER =
13
ESCAPE =
27
IS_18 =
RUBY_VERSION =~ /^1\.8/
SEQUENCE_TIMEOUT =
0.005
NOTHING =

getch returns this as ‘nothing’ on 1.8 but nil on 1.9.2

(2**32 - 1)
A_TO_Z =
('a'..'z').to_a
DEFAULT_INPUT =
lambda { Curses.getch }

Class Method Summary collapse

Class Method Details

.input(&block) ⇒ Object



14
15
16
# File 'lib/dispel/keyboard.rb', line 14

def self.input(&block)
  @input = block
end

.output(options = {}, &block) ⇒ Object



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
48
49
# File 'lib/dispel/keyboard.rb', line 18

def self.output(options={}, &block)
  @sequence = []
  @started = Time.now.to_f
  timeout = options[:timeout]

  if timeout && SEQUENCE_TIMEOUT > timeout
    raise "Timeout must be higher then SEQUENCE_TIMEOUT (#{SEQUENCE_TIMEOUT})"
  end

  loop do
    @now = Time.now.to_f
    @elapsed = @now - @started

    key = fetch_user_input

    # finish previous sequence
    if @sequence.any? and @elapsed > SEQUENCE_TIMEOUT
      sequence_to_keys(@sequence).each(&block)
      @sequence = []
    end

    if key # start new sequence
      @started = @now
      @sequence << key
    elsif timeout and @elapsed > timeout
      @started = @now
      yield :timeout
    else
      sleep SEQUENCE_TIMEOUT # nothing happening -> sleep a bit to save cpu
    end
  end
end