Getting Started
Introduction
Sample Programs
QBasic IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Web Sites
More Tutorials
Vendors
Books
Magazines
NewsLetters
NewsGroups
Forums
User Groups
Talk Shows
Blogs

GBIC >> QBasic >> Tutorial >> Events

QBasic Information Center Tutorials - Events
These tutorials were written to help you get a quick, but thorough, understanding of QBasic - the scope of the language as well as it's specific capabilities.

Events
QBasic includes a fairly wide range of features which allow it respond to activity (events) of devices attached to a computer, via the following statements. Programmers may write code which is called in response to these events.

    • Joystick
     on strig, strig, stick 
    • Keyboard
     on key, key 
    • Light Pen
     on pen, pen 
    • Serial Ports
     on com, com
    • Speaker
     on play, play, beep, sound 
    • Timers
     on timer, timer
    • Control Strings    
     ioctl, ioctl$ 

Status Information
In addition to trapping device events, QBasic can also query some devices for status information, or control the devices, using the following statements.

    Joystickstick(n%) - returns coordinates of a joystick
    strig(n%) - returns status of a joystick trigger
    Light Pen     pen(n%) - returns status of the light pen
    Speakerplay(n) - returns number of notes in the background music queue
    beep - generates a beep sound from the speaker
    sound - generates a customized sound from the speaker

Additional details on these status/control commands are provided in the reference section below. QBasic's ability to query the keyboard is covered in a different tutorial section.

Control Strings
QBasic can also send control string to device software drivers using the following commands.

  • ioctl - sends a control string to a device driver
  • ioctl$ - returns status information from devices

These commands apply to devices opened using the QBasic Open statement, where the assigned filenumber is used to identify the device to which a control string is to be sent.

Event Trapping Functions Reference
Here's a quick reference of the available event trapping functions, in alphabetical order.

  • ioctl - sends control string to a device driver
        ioctl(#1, var$)   # var$ is sent to device #1
        
    The device number is that assigned using the Open statement.

  • ioctl$ - returns status information from a device driver
        result$ = ioctl$(#1)   # requests status from device #1
        
    The device number is that assigned using the Open statement.

  • on com - enable/disable/suspend event trapping on a comm port
        on com(n%) gosub line  # trigger an event when a key is pressed
                # n% values are 1 or 2, corresponding to serial ports
    
        com(n%) on      # enables event trapping for serial port n%
        com(n%) off     # disables event trapping for serial port n%
        com(n%) stop    # suspends event trapping for serial port n%
                          (events are saved and processed later
                          when event trapping is re-enabled)
        

  • on key - enable/disable/suspend event trapping of keystrokes
        on key(n%) gosub line  # trigger an event when a key is pressed
                n%        Key
                ------    --------------------------------------------
                0         All keys listed here (KEY(0) ON, KEY(0) OFF,
                          and KEY(0) STOP only).
                1-10      Function keys F1-F10.
                11        Up Arrow key.
                12        Left Arrow key.
                13        Right Arrow key.
                14        Down Arrow key.
                15-25     User-defined keys or combination of keys
                30, 31    Function keys F11 and F12.
    
        key(n%) on       # enables event trapping for key n%
        key(n%) off      # disables event trapping for key n%
        key(n%) stop     # suspends event trapping for key n% 
                           (events are saved and processed later
                           when event trapping is re-enabled)
        

    See QBasic Help for more information on creating user-defined key events.

  • on pen - enable/disable/suspend light pen event trapping
        on pen(limit%)  gosub line    # capture light-pen events
              # limit% (1-32) - on play branches to subroutine when
                                there are fewer than limit% notes in
                                the music buffer
     
        pen on                # enable light-pen event trapping
        pen off               # disable light-pen event trapping
        pen stop              # suspend light-pen event trapping.
                                events are saved and processed when
                                light-pen event trapping is enabled
        

  • on play - enable/disable/suspend play event trapping
        on play gosub line    # capture play events
     
        play on                # enable play trapping
        play off               # disable play trapping
        play stop              # suspend play trapping.
                                events are saved and processed
                                when event trapping is enabled
        

  • on strig - enable/disable/suspend joystick event trapping
        on strig (n%) gosub line # capture a specific joystick event
               # n% values  0 - lower trigger, A
                            2 - lower trigger, B
                            4 - upper trigger, A
                            6 - upper trigger, B
    
        stick(n%)            # returns coordinates of a joystick
        strig(n%) on         # enable joystick event trapping
        strig(n%) off        # disable joystick event trapping
        strig(n%) stop       # suspend joystick event trapping.
                               events are saved and processed
                               when event trapping is enabled
        

  • on timer - enable/disable/suspend timer event trapping
        on timer(n%) gosub line  # trigger an event in n% seconds
               # n% is seconds until an event is triggered
               
        timer           # returns number of seconds since midnight
        timer on        # enable timer event trapping
        timer off       # disable timer event trapping
        timer stop      # suspend timer event trapping.
                          events are saved and processed
                          when event trapping is enabled
        
  • play - sends control string to a device driver
        beep         # no arguments, cannot control duration
        

  • beep - sends control string to a device driver
        beep         # no arguments, cannot control duration
        

  • sound - plays a sound through the speaker
        sound frequency%, duration%    # single frequency 37-32,767
                                       # duration is clock ticks
                                         (10.2 ticks per second)    
        

If you have any suggestions or corrections, please let me know.