PEN

n = PEN (value)

Returns pen, mouse or tap data depending on value.

Value Desription
0 True (non zero) if there is a new pen or mouse event
1 x position of last pen down, tap, mouse button down
2 y position of last pen down, tap, mouse button down
3 True if pen is down, mouse left button is pressed or tap
4 Pen, tap: last/current x position, MOUSE: the current x position only if the left mouse button is pressed
5 Pen, tap: last/current y position, MOUSE: the current x position only if the left mouse button is pressed
10 current mouse x pos
11 current mouse y pos
12 true if the left mouse button is pressed
13 true if the right mouse button is pressed
14 true if the middle mouse button is pressed

PEN must be enabled prior to use of this function with Pen ON|OFF

Most important values are 3, 4 and 5. With PEN(3) you will know, if the left mouse button was pressed or the screen was tapped. With PEN(4) and PEN(5) you will get the coordinates of that click or tap. See example 2 and 3.

Example 1: Overview

Print "  Move and click the Pen or Mouse [press Esc to stop]..."
Print
Pen On
Print Using " ####x, ####y";              ' Format for x,y position
While Inkey <> Chr(27) Do                 ' Press Esc key to exit loop
  Locate 3, 0
  print "Pen|Mouse new event:  "; Pen(0)
  print
  print "Pen last down|Mouse down:";: ? Usg; Pen(1), Pen(2)
  print
  print "Pen down|Mouse Left down:  "; Pen(3)
  print
  print "Pen last/current|Mouse Left down:";: ? Usg; Pen(4), Pen(5)
  print
  print "Mouse current:";: ? Usg; Pen(10), Pen(11)
  print
  print "Mouse Left, Middle, Right down: ", Pen(12), Pen(14), Pen(13);
Wend

Example 2: Get position of tap or mouse click

pen on

print "Press left mouse button or tab screen. Press q to quit."

while(1)          
    if(pen(3)) then
        PosX = pen(4)
        PosY = pen(5)
        locate 1,0: print "Click/tap at position: " + PosX + " , " + PosY
    endif
    
    k = inkey()
    if(len(k) == 1 AND k == "q") then end
    
    showpage
    delay(20)
wend

DELAY 20 ms saves CPU and battery in a loop.

Example 3: Press a button

pen on

print "Press left mouse button or tab screen. Press q to quit."

' Define button
button.x = 100
button.y = 100
button.w = 100
button.h = 100

while(1)

    ' Draw button in dark yellow
    rect button.x, button.y STEP button.w, button.h color 6 filled
          
    if(pen(3)) then
        PosX = pen(4)
        PosY = pen(5)
        
        ' Check if the click or tap is inside the button area
        if( (PosX > button.x) and (PosX < button.x + button.w) and (PosY > button.y) and (PosY < button.y + button.h) )
            ' Draw button in red
            rect button.x, button.y STEP button.w, button.h color 12 filled 
        endif
        
    endif
    
    k = inkey()
    if(len(k) == 1 AND k == "q") then end
    
    showpage
    delay(20)
wend
Graphics
If there is insufficient information on this page and you wish learn more about PEN, please send an email to smallbasic@gmail.com. You can help to improve information about PEN by submitting a pull request, click View Source for details. Note, an offline language reference text file is also available - see the Download section.