class Presentify::Keyboard
Public Class Methods
new(&block)
click to toggle source
# File lib/presentify/keyboard.rb, line 3 def initialize(&block) @listeners = [] block.call(self) Thread.new { watch_keyboard }.join end
Public Instance Methods
on(key, &block)
click to toggle source
# File lib/presentify/keyboard.rb, line 9 def on(key, &block) @listeners << [key, block] end
trigger(key)
click to toggle source
# File lib/presentify/keyboard.rb, line 13 def trigger(key) @listeners.each do |entry| if entry[0] == key entry[1].call end end end
Private Instance Methods
read_key()
click to toggle source
# File lib/presentify/keyboard.rb, line 37 def read_key old_state = `stty -g` `stty raw -echo` c = STDIN.getc.chr if c == "\e" # reading escape sequences extra_thread = Thread.new do c += STDIN.getc.chr + STDIN.getc.chr end extra_thread.join(0.001) extra_thread.kill end c ensure `stty #{old_state}` end
watch_keyboard()
click to toggle source
# File lib/presentify/keyboard.rb, line 23 def watch_keyboard while true case ch = read_key when "\e[A" then trigger :up when "\e[B" then trigger :down when "\e[C" then trigger :right when "\e[D" then trigger :left when "\r", "\n" then trigger :enter when "\e", "\u0003" then system("clear") && exit(0) # else puts "You pressed: #{ch.inspect}\n" end end end