Liberty BASIC Help Online

Reading Mouse Events and Keystrokes
 
A graphics window or graphicbox is able to read mouse and keyboard input.  Here is the world's smallest painting program!
 
  open "Paint something!" for graphics as #w
  print #w, "when leftButtonMove [paint]"
  print #w, "when characterInput [letter]"
  print #w, "down ; size 3"
  wait
[paint]
  print #w, "set "; MouseX; " "; MouseY
  wait
[letter]
  print #w, "\"; Inkey$
  wait
 
The mouse location within the graphics area can be retrieved from the MouseX and MouseY variables.  The Inkey$ variable holds the character of the key pressed.  In order to capture keyboard input the graphics device must have focus.  Sometimes it is necessary to force the input focus using the setfocus command:
 
  print #w.g, "setfocus"
 
Mouse actions example with subroutine event handler
Here's a really simple illustrative example of a paint program.  When the leftButtonDown event happens the draw subroutine gets called, and the graphicbox handle and mouse x and y values get passed in.
    'a simple drawing program
 
    open "drawing example" for graphics_nsb as #draw
    #draw "vertscrollbar on 0 "; DisplayHeight
    #draw "horizscrollbar on 0 "; DisplayWidth
    #draw "down"
    #draw "size 2"
    #draw "when leftButtonMove draw"
    wait
 
sub draw handle$, x, y
    #handle$, "set "; x; " "; y
end sub
 
Mouse actions example with branch label event handler
Here's a really simple illustrative example of a paint program.  When the leftButtonDown event happens the program branches to the [draw] routine. Mouse coordinates are contained in MouseX and MouseY.
 
'a simple drawing program
 
    open "drawing example" for graphics_nsb as #draw
    #draw "vertscrollbar on 0 "; DisplayHeight
    #draw "horizscrollbar on 0 "; DisplayWidth
    #draw "down"
    #draw "size 2"
    #draw "when leftButtonMove [draw]"
    wait
 
[draw]
    #draw, "set "; MouseX; " "; MouseY
    wait
 
Keyboard input example with subroutine event handler
Here's a simple program that monitors user keypresses.  When the characterInput event happens the keyCheck subroutine gets called, and the graphicbox handle and Inkey$ values get passed in.
  'a simple keycheck program
 
    open "Press some keys!" for graphics_nsb as #draw
    #draw "setfocus;place 10 20"
    #draw "when characterInput keyCheck"
    wait
 
sub keyCheck handle$, key$
    #handle$, "\";key$
    end sub
 
Keyboard input example with branch label event handler
Here's a simple program that monitors user keypresses.  When the characterInput event happens the program branches to the [keyCheck] routine, and Inkey$ contains the information about the key(s) pressed by the user.
'a simple keycheck program
 
    open "Press some keys!" for graph