Skip to content

Instantly share code, notes, and snippets.

@mrowa44
Created April 5, 2015 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrowa44/66eb609ff4daf342cb57 to your computer and use it in GitHub Desktop.
Save mrowa44/66eb609ff4daf342cb57 to your computer and use it in GitHub Desktop.
Non-stopping input (even special characters, arrows etc.)
require 'io/wait'
def read_char
system "stty raw -echo"
if $stdin.ready?
char = $stdin.getc.chr
if char == "\e"
extra_thread = Thread.new do
char += $stdin.getc.chr
char += $stdin.getc.chr
end
extra_thread.join 0.0001
extra_thread.kill
end
end
char
end
# Example usage
def show_single_key
char = read_char
# Read char only if not nil
# To make it non-stopping simply allow nil
if char
if char == "\u0003"
system "stty -raw echo"
puts "CHAR: #{char.inspect}"
Process.exit! true
else
system "stty -raw"
puts "CHAR: #{char.inspect}"
end
end
end
loop do
show_single_key
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment