User input - get keystrokes (arrow keys + shift)

Hello.

I am trying to find out how to do a specific action / call a method if a
user presses the SHIFT key and an arrow key. (Two keys pressed together)

I use this for a very simple program, a pseudo shell. Like, “cd
/home/marcelle” allows the user to enter the /home/marcelle directory
via Dir.chdir.

Now, if SHIFT + left arrow key is pressed, I want to be able to go back
one directory, i.e. to “/home”. (To simplify, the SHIFT key could be
omitted and just the arrow key used)

My problem is that I do not know how I can intercept or even act on this
at all. It seems so far that mostly, via $stdin.gets, you must wait for
ENTER to be pressed. Logically, if a user presses shift + left arrow
key, there won’t be any enter here as part of the input.

How can I get the information that the arrow key was pressed?

Is there a way in Ruby to do a specific action when:

  • any single key is input
  • any combination is input (i.e. shift + another character)

On Sun, May 22, 2011 at 8:27 AM, Marc H. [email protected]
wrote:

Hello.

I am trying to find out how to do a specific action / call a method if a
user presses the SHIFT key and an arrow key. (Two keys pressed together)

Look at the curses library.

http://www.ruby-doc.org/stdlib/libdoc/curses/rdoc/index.html

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs