Palm OS Programming With NSBasic (cont.)
Pen Events
Our application will be used to draw on the Palm screen. As a result, we'll need some way to capture information from
the Pen such as its coordinates and up/down events. NSBasic has built-in support for this functionality.
In the Project Explorer, you should find something that looks like Figure 5. When you right-click on the highlighted line
"NSDraw/NSDraw(Before,Events,)", you will be presented with a popup menu option. From this menu, select "Show Pen/Keyboard Event
Codes". A code window will be displayed that will look very similar to previous code windows.
Figure 5: Using The Project Explorer
Listing 4 contains code that needs to be entered into this code window. We'll look at this code in detail in the next section.
Listing 4
sub Screen1004_events()
dim res as integer
dim curX as integer
dim curY as integer
global saveX as integer
global saveY as integer
dim upOrDown as integer
dim rectWidth as integer
dim rectHeight as integer
dim circWidth as integer
dim circHeight as integer
dim circRadius as integer
if FirstTime = 1 then
FirstTime = 0
DrawLine 0,140,159,140
DrawLine 0,141,159,141
exit sub
endif
res=getEventType()
if res=1 then
exit sub 'ignore keyboard chars
endif
if res=2 then 'pen down
getPen(saveX,saveY, upOrDown) 'get pen coords
drawBitmap 1008,saveX,saveY
exit sub
endif
if res=3 then 'penup
getPen( curX, curY,upOrDown)
drawBitmap 1009,saveX,saveY 'erase pointer
if saveY<15 or curY<15 then exit sub
if saveY>140 or curY>140 then exit sub
if (curX-saveX)=0 then exit sub
if (curY-saveY)=0 then exit sub
If (chkRectangle.status=1) then
if saveX < curX then
rectWidth=(curX-saveX)
else
rectWidth=(curX-saveX)
endif
if saveY < curY then
rectHeight=(curY-saveY)
else
rectHeight=(curY-saveY)
endif
DrawRectangle saveX,saveY,rectWidth,rectHeight,0
exit sub
endif
'Circle
If (chkCircle.status=1) then
if saveX Abs(circWidth) then
circHeight=circWidth
circRadius=circHeight/2
else
circHeight=circWidth
circRadius=circWidth/2
endif
DrawRectangle saveX,saveY,circWidth,circHeight,circRadius
exit sub
endif
DrawLine saveX,saveY,curX,curY
endif
end sub
The beginning of the code in Listing 4 declares the variables that we want to use. The following table lists these variables,
and some general information about them.
| Variable Name | Information |
| Res |
Result of GetEventType Function |
| CurX,CurY |
The current X,Y values of the pen |
| SaveX,SaveY |
The previous X,Y values of the pen |
| UpOrDown |
Storing if the pen is up or down |
| RectWidth,RecHeight |
Width and Height of our rectangle. This will be a calculated value based on Current and Saved variables. |
| CircWidth,CircHeight,CircRadius |
Width,Height and Radius of our circle. NSBasic does not include support for drawing a circle. Instead, we'll calculate the width/height of a rectangle, set them equal so we have a circle (not an oval) and then set the radius of the rectangle to ½ of the width/height. |
The next step is to determine if this is the first time the application has been executed. If so, we need to draw
2 lines near the bottom of the display from 0,140 to 159,140 and 0,141 to 159,141. The NSBasic DrawLine command does this
for us. These lines give the appearance of a drawing canvas.
s
In order for these events to be started, the pen or a key would need to be pressed. Because our application is only concerned
with the pen events, we need to determine the last event type, which triggered the special event processing. The GetEventType
Function is used for this purpose by simply storing the results of this function in the res variable. We can then check this
value to see what type of event occurred, skipping any key related activities and focusing on pen up/down events.
Next: Final Steps
|