Returning a code with keypress information

keycode()

Return a code with keypress information


SYNTAX

keycode(mode)

There are three different modes available, 0, 1 and 2.

keycode(0) awaits a single keypress, attempts to recognise the key, and then returns a code according to the following rules:

  • If the key pressed corresponds to one of the “Sent by” sequences defined in the terminal parameter file, keycode(0) returns a negative number. This number is the complement of the terminal parameter file entry number corresponding to the key pressed. For example, pressing F3 would return -3, as F3 is entry number 3 in the “Sent by Sequences” section of the terminal parameter file. BACKSPACE is entry 33, so pressing this key causes -33 to be returned. This method can be used to identify special keys in a fully portable manner.

  • If the key pressed was not recognised as a “Sent by” sequence from the parameter file, then the ASCII code for the character is returned as a positive number. For example, pressing ‘a’ causes 97 to be returned.

keycode(1) returns immediately with a positive integer indicating the key that was pressed to exit the last dialog or input command.

  • The value returned is the entry number in the terminal parameter file corresponding to the recognised key. Again, this mechanism allows for full portability between terminal types.

  • If there was no previous dialog or input command, keycode(1) returns -1.

keycode(2) returns 0 if there is no keyboard input or 1 if a key has been pressed. This means that the program can run a command or series of commands while checking for an input from the user.

The keycode function returns an integer in the i2 range and therefore should not be stored in an i1 field.


NOTES

  • It is common practice to negate the value returned from keycode(1). Then, if the key entered was a sequence recognised by the terminal parameter file, the value becomes the same as that returned by keycode(0) for the same key. A standard set of manifest constants is defined in $SCULPTOR/include/keycode.h.

  • If !compat keycode =2 is declared, the values returned are Sculptor 2 compatible.

  • The inchar(0) function is identical to keycode(0). However, with any argument other than zero, inchar() takes input from a channel.


EXAMPLES

  1. Wait for any input, using keycode (2) to test:

    !temp KeyWait,,i2
    
         while (TRUE) {
              tmp.KeyWait = keycode(2)
              if (tmp.KeyWait <> 0) then break
              caption "Waiting for input"
              sleep 2
         }
    
  2. Check keypress against defined manifest constants:

    !include <keycode.h>
    
    !temp KeyPress,,i2
    
         tmp.KeyPress = keycode(0)
         switch (tmp.KeyPress * -1) {
              case = KEY_ENTER:
                   caption "ENTER key pressed"
                   break
    
              case = KEY_F1:
                   caption "F1 key pressed"
                   break
         }
    

RELATED TOPICS

Terminal parameter files

The include file <keycode.h>

inchar()