File: keypress_nonblock.rb

package info (click to toggle)
ruby-tty-reader 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 772 kB
  • sloc: ruby: 1,759; sh: 4; makefile: 4
file content (19 lines) | stat: -rw-r--r-- 378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

require_relative "../lib/tty-reader"

reader = TTY::Reader.new

puts "Press a key (or Ctrl-X to exit)"

loop do
  print reader.cursor.clear_line
  print "=> "
  char = reader.read_keypress(nonblock: true)
  if char == ?\C-x
    puts "Exiting..."
    exit
  elsif char
    puts "#{char.inspect} [#{char.ord}] (hex: #{char.ord.to_s(16)})"
  end
end