Created
April 5, 2015 14:54
-
-
Save mrowa44/66eb609ff4daf342cb57 to your computer and use it in GitHub Desktop.
Non-stopping input (even special characters, arrows etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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