Q(uick)BASIC Function: PEN

Quick View

PEN

A device I/O function that reads the lightpen coordinates

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • PEN(n%)
Description/Parameter(s)
n% Specifies the information to be returned about light pen status:
n%Returns
0Whether pen was used since last call (-1 if yes, 0 if no)
1The x screen coordinate of the last pen press
2The y screen coordinate of the last pen press
3The current pen switch status (-1 if down, 0 if up)
4The x screen coordinate where the pen last left the screen
5The y screen coordinate where the pen last left the screen
6The character row of the last pen press
7The character column of the last pen press
8The character row where the pen last left the screen
9The character column where the pen last left the screen
Example
DO P = PEN(3) LOCATE 1, 1: PRINT "Pen is "; IF P THEN PRINT "down" ELSE PRINT "up " PRINT "X ="; PEN(4), " Y ="; PEN(5); " " LOOP
Syntax
  • PEN(n)
Description/Parameter(s)

The argument n indicates what value is to be returned. It is a numeric expression in the range 0-9.

Note: PEN function does not work when the mouse driver is enabled because the mouse driver uses the PEN function's BIOS calls. Use mouse function 14 to disable the driver's lightpen emulation. Mouse function 13 turns emulation back on. See your mouse manual for more information.

The following list describes the values for n and the corresponding values returned by PEN:

Argument Value Returned
0 The most recent pen use: -1 if pen was down since last poll, 0 if not
1 The x pixel coordinate where pen was last pressed
2 The y pixel coordinate where pen was last pressed
3 The current pen-switch value: -1 if down, 0 if up
4 The last known valid x pixel coordinate
5 The last known valid y pixel coordinate
6 The character row position where pen was last pressed
7 The character column position where pen was last pressed
8 The last known character row where the pen was positioned
9 The last known character column where the pen was positioned
Example

This example produces an endless loop to print the current pen-switch status (UP/DOWN). Press CTRL+BREAK to exit the loop.
Note: Do not run this example if your mouse driver is enabled.

' You will have to press CTRL+BRK to exit this loop COLOR 0,7 CLS ' Clear screen PEN ON DO P = PEN(3) LOCATE 1,1 : PRINT "PEN IS "; IF P THEN PRINT "DOWN" ELSE PRINT "UP " X = PEN(4) : Y = PEN(5) PRINT "X =" X, "Y =" Y; " " LOOP COLOR 7,0 END
Syntax
  • PEN(n%)
Description/Parameter(s)
n% An integer value between 0 and 9 that specifies what information is to be returned about the status of the lightpen.
Argument Value returned
0 Whether pen was down since last function call (-1 = yes, 0 = no).
1 The x coordinate of the last pen press.
2 The y coordinate of the last pen press.
3 The current pen switch status (-1 = down, 0 = up).
4 The x coordinate where the pen last left the screen.
5 The y coordinate where the pen last left the screen.
6 The character row of the last pen press.
7 The character column of the last pen press.
8 The character row where the pen last left the screen.
9 The character column where the pen last left the screen.
The lightpen coordinate system is identical to the current graphics screen mode, without viewport or window considerations.
The PEN function is not available in OS/2 protected mode.

Usage Note

  • The PEN function does not work when the mouse driver is enabled because the mouse driver uses the PEN function's BIOS calls. Use mouse function 14 to disable the driver's lightpen emulation. Mouse function 13 turns emulation back on. See your mouse manual for more information.
Example

This example uses the PEN statement to enable event trapping and the PEN function to display current light pen status and position. ON PEN GOSUB passes control to the PenReport subroutine when a pen event occurs.
Note: Do not run this example if your mouse driver is enabled.

CLS 'Clear screen. COLOR 0, 7 'Set black on white. CLS 'Clear screen. PEN ON 'Enable light pen. ON PEN GOSUB PenReport DO LOCATE 23, 12 PRINT "Press the Light Pen against the screen to see a report." LOCATE 24, 17 PRINT " Press the Space Bar to exit the program. "; LOOP UNTIL INKEY$ = " " PEN OFF 'Disable light pen. COLOR 7, 0 'Set back to black on white. CLS 'Clean up the screen. END PenReport: DO P = PEN(3) 'Report light pen status and get X and Y position. LOCATE 10, 27 PRINT "A Pen event has occurred." LOCATE 24, 17 PRINT "Press ANY key to exit the Light Pen report."; LOCATE 12, 30 PRINT "Light Pen is "; IF P THEN PRINT "Down" X = PEN(4): Y = PEN(5) ELSE PRINT "Up " X = 0: Y = 0 END IF 'Report the X and Y position. LOCATE 14, 22 PRINT "X Position ="; X; " " LOCATE 14, 40 PRINT "Y Position ="; Y; " " LOOP WHILE INKEY$ = "" RETURN