Class: RubyTerminalGames::Keyboard

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



3
4
5
# File 'lib/ruby_terminal_games/keyboard.rb', line 3

def thread
  @thread
end

Instance Method Details

#capture(detect_direction: false, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_terminal_games/keyboard.rb', line 5

def capture(detect_direction: false, &block)
  Curses.noecho
  Curses.stdscr.keypad(true)
  Curses.curs_set(0)
  Curses.cbreak
  Curses.raw

  Thread.new do
    loop do
      key = Curses.getch
      command = direction(key) || key
      command = key unless detect_direction
      block.call(command)
      sleep(0.15)
    end
  end
end