title: How to capture a key press in Ruby?
body: <p>In Ruby, I need a simple thread that would run some code every time a key in pressed. Is there a way to do that?</p>
  
  <p>I need to be able to capture the <kbd>Page Up</kbd> and <kbd>Page Down</kbd></p>
  
  <p>Here is what I tried:</p>
  
  <pre><code>#!/usr/bin/env ruby
  
  Thread.new do
    while c = STDIN.getc
      puts c.chr
    end
  end
  
  loop do
    puts Time.new
    sleep 0.7
  end
  </code></pre>
  
  <p>This almost works. There is only 1 issue, one needs to hit return after every key stroke. I guess this is because of buffered IO.</p>