Liberty BASIC Help Online

Using virtual key constants with Inkey$
Keyboard input can only be trapped in graphics windows or graphicboxes.  When a key is pressed, the information is stored in the variable Inkey$.  To check for keypresses, send the command "when characterInput [branchLabel]" to the graphics window or graphicbox and evaluate Inkey$ at the designated branch label.
 
Special keys like Alt, Ctrl, Shift, the Arrow keys, etc. are not coded like the letters, numbers and other symbols.  They have special values, and are preceded by a value of 32 (or less) when they are trapped by Inkey$.  Windows has values defined for these special keys, which are expressed in virtual key constants.  You can use these constants (and other special Windows constants) in your Liberty BASIC programs.  See also:  Graphics Commands Inkey$, Reading Mouse and Keyboard Input, and Using Inkey$.  (See Inkey$ for a discussion of the meaning of the first character of Inkey$ when it is longer than one character.)
 
Key Up and Key Down
Special keys trigger a new value for Inkey$ when they are pressed and again when they are released. 
 
Virtual Keys
A virtual key is the key that is actually pressed on the keyboard.  The VK value for a letter, say 'a' is the same for lower case 'a' and upper case 'A' because it refers to the key pressed on the keyboard, not to the ASCII value of the input.  Most keys have a graphical representation.  Pressing the 'a' key in a text window causes the letter 'a' to be displayed in the window.  There are some keys that do not have a graphical representation.  It is necessary to use Virtual Key Codes to discover which of these keys has been pressed.  They include the arrow keys, the F-keys, Shift, Ctrl, Alt, Del, etc.
 
Here is a program that gives a quick example:
 
    'Inkey$ example, part two
    ctrl$ = chr$(_VK_CONTROL)
    print "Keys pressed:"
    open "Inkey$ example" for graphics as #graph
    print #graph, "when characterInput [keyPressed]"
    print #graph, "trapclose [quit]"
 
[loopHere]
    'make sure #graph has input focus
    print #graph, "setfocus"
    'scan for events
    scan
    goto [loopHere]
 
[keyPressed]
    key$ = left$(Inkey$, 2)
    if len(key$) < 2 then
        print "pressed: "; key$
      else
        if right$(key$, 1) = ctrl$ then
            print "CTRL was pressed"
          else
            print "Unhandled special key"
      end if
    end if
    goto [loopHere]
 
[quit]
 
    print "Quitting"
    close #graph
    end
 
Some other virtual key code constants:
 
  F1 through F16 _VK_F1 through _VK_F16
  0 through 9 on regular keyboard _VK_0 through _VK_9
  0 through 0 on number pad _VK_NUMPAD0 through _VK_NUMPAD9
  a through z _VK_A through _VK_Z
  Alt_VK_MENU
  Shift_VK_SHIFT
  Home_VK_HOME
  End_VK_END
  Insert_VK_INSERT
  Delete_VK_DELETE
  NumLock_VK_NUMLOCK
  Arrow Up_VK_UP
  Arrow Down_VK_DOWN
  Arrow Left_VK_LEFT
  Arrow Right_VK_RIGHT


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/