Sha256: e4581bef7b2fbd7d11dc899da377294e6e9ddb78ebf344816225fdafb288c9f6

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

#
# ↓ almost copy-pasted from a gist
#


require 'io/console'
 

module DialogTui
module UserAction

module_function

# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
  STDIN.echo = false
  STDIN.raw!
 
  input = STDIN.getc.chr
  if input == "\e" then
    input << STDIN.read_nonblock(3) rescue nil
    input << STDIN.read_nonblock(2) rescue nil
  end
ensure
  STDIN.echo = true
  STDIN.cooked!
 
  return input
end
 
# oringal case statement from:
# http://www.alecjacobson.com/weblog/?p=75
def show_single_key
  c = read_char
 
  case c
  when " "
    puts "SPACE"
  when "\t"
    puts "TAB"
  when "\r"
    puts "RETURN"
  when "\n"
    puts "LINE FEED"
  when "\e"
    puts "ESCAPE"
  when "\e[A"
    puts "UP ARROW"
  when "\e[B"
    puts "DOWN ARROW"
  when "\e[C"
    puts "RIGHT ARROW"
  when "\e[D"
    puts "LEFT ARROW"
  when "\177"
    puts "BACKSPACE"
  when "\004"
    puts "DELETE"
  when "\e[3~"
    puts "ALTERNATE DELETE"
  when "\u0003"
    puts "CONTROL-C"
    exit 0
  when /^.$/
    puts "SINGLE CHAR HIT: #{c.inspect}"
  else
    puts "SOMETHING ELSE: #{c.inspect}"
  end
end
 
end
end

if __FILE__ == $0
  DialogTui::UserAction.show_single_key while true
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dialog_tui-0.1.3 lib/dialog_tui/user_action/read_char.rb
dialog_tui-0.1.2 lib/dialog_tui/user_action/read_char.rb
dialog_tui-0.1.1 lib/dialog_tui/user_action/read_char.rb
dialog_tui-0.1.0 lib/dialog_tui/user_action/read_char.rb