INKEY

INKEY

Returns the last key-code in keyboard buffer, or an empty string if there are no keys. Special key-codes like the function-keys are returned as 2-byte string.

Example 1

WHILE(1)
  k = INKEY
  IF LEN(k)
    IF LEN(k)=2
      ? "H/W #"+ASC(RIGHT(k,1))
    ELSE
      ? k; " "; ASC(k)
    FI
  FI
  SHOWPAGE
WEND

Example 2

The following program will return the keycode and a string for every pressed key. The string can be used in an if-statement to querry if the key was pressed. See example 3 how to use the string.

while(1)
  k = INKEY

  if len(k)
    if len(k) == 2        
      print "2-Byte key code   : " + asc(mid(k,1,1)) + "  " + asc(mid(k,2,1));
      print " -> s = chr(";asc(mid(k,1,1));") + chr(";asc(mid(k,2,1));")" 
    else
      if(asc(k) > 37)
        print "Printable key code: "; asc(k); " -> s = \"";k;"\"" 
      else
        print "1-Byte key code   : "; asc(k);
        print " -> s = chr(";asc(k);")"
      endif
    endif
  endif
  
  showpage
wend

Example 3: Querry if a key was pressed

See example 2 to get the keycodes for the keys.

const KeyUp = chr(27) + chr(9)
const KeyDown = chr(27) + chr(10)
const KeySpace = " "
const KeyEsc = chr(27)
const KeyReturn = chr(13)

while(1)
  k = INKEY

  if len(k)
    select case k
        case KeyUp:     print "Up"
        case KeyDown:   print "Down"
        case KeySpace:  print "Space"
        case KeyEsc:    print "Esc"
        case KeyReturn: print "Return"
        case "a":       print "a"
        case "A":       print "A"
        case else:      print "other key"
    end select
  endif
  
  showpage
wend

Example 4: Input form

' Key values:
Const K_BKSP   = Chr(0x08)  ' BackSpace
Const K_TAB    = Chr(0x09)
Const K_ENTER  = Chr(0x0D)
Const K_ESC    = Chr(0x1B)  ' Escape
Const K_DELETE = Chr(0x7F)
Const K_PGUP   = K_ESC + Chr(0x01)  ' Page Up
Const K_PGDN   = K_ESC + Chr(0x02)  ' Page Down
Const K_LEFT   = K_ESC + Chr(0x04)  ' Arrow Left
Const K_RIGHT  = K_ESC + Chr(0x05)  ' Arrow Right
Const K_UP     = K_ESC + Chr(0x09)  ' Arrow Up
Const K_DOWN   = K_ESC + Chr(0x0A)  ' Arrow Down
Const K_INSERT = K_ESC + Chr(0x10)
Const K_HOME   = K_ESC + Chr(0x11)
Const K_END    = K_ESC + Chr(0x12)

' Edit string:
Def edit_char(txt, pos, char, ins_mode) = Replace(txt, pos, char, ins_mode = 0)
Def delete_char(txt, pos) = Replace(txt, pos, "", 1)

' Initialize three demo fields:
f_lbl = ["First Name:", "Last Name: ", "Your Hobby:"] ' field label
f_row = [3, 5, 7]     ' label row
f_col = [1, 1, 1]     ' label column
f_len = [10, 10, 20]  ' variable length
f_var = ["", "", ""]  ' variable value

' Index of fields:
Const MIN_IX = Lbound(f_lbl)
Const MAX_IX = Ubound(f_lbl)
ix = MIN_IX   ' Start on first field index
ins_mode = 1  ' Start in insert character mode

' Main demo loop:
While True