Example

Getting Raw Keyboard Characters in the Console

Author: Carl Sassenrath
Return to REBOL Cookbook

If you write REBOL console scripts that need to respond to single keystrokes without requiring the RETURN key, here is a good trick to know.

To make the console respond to single keys, simply change its port to BINARY mode. Here's how:


    set-modes system/ports/input [binary: true]

Now, each time a key is pressed, it will be returned to your script. This works because the console's input port is already in DIRECT mode and setting it to BINARY removes the check for the NEWLINE (LF) character.

Here is an example script:


    set-modes system/ports/input [binary: true]

    until [
      char: input
      print ["char:" char mold to-char char]
      char = 13 ; return
    ]

If you need to return the console to normal text line mode operation (which you will need to do if you want to return to the REBOL command line prompt), just use this:


    set-modes system/ports/input [binary: false]

2006 REBOL Technologies REBOL.com REBOL.net