| |
|
|
October, 2007
A number of people requested info on how to poll for keyboard events whilst running under GS/OS. Since I have the manuals and the inclination to learn myself, I read up on how to do it, and then hacked together a quick example application illustrating how to do it using the Event Manager tool.
To get this example code working, you need to create a resource file,
add a Tool Startup resource, and make sure the following tools are
loaded (checked):
- Tool Locator
- Memory Manager
- Misc Tools
- Event Manager
You also have to ensure that the resource file is associated with your source file by using the "Compile--> Add Resources..." menu item. See below:
 |
Select File-->New to begin creating a new resource file. |
| | |
 |
Enter in a name for your resource file. This is usually the same name as your main source file. |
| |
|
 |
Click on the New Resource Type button and add a Tool startup resource. |
| |
|
 |
Select the resources applicable to your program that you wish to be loaded. Click OK when done. You can then close the resource file window. |
| |
|
 |
Select the source edit window that you wish to add resources to and select
Compile-->Add Resources... . |
| |
|
 |
Then select the resource file that you just created and click Add. The resource file is now "linked" to your source file, and you can startup the specified Tools using the StartupTools function. Refer to the source code below and also the Tool Startup Resource section in chapter 4 of the TML Pascal II Reference Manual. |
The "Textbook" example code follows:
|
|
PROGRAM TextKeyTest;
USES
Types,
Memory,
Locator,
MiscTool,
Events;
VAR
gMyMemoryID: Integer;
gStartStopRef: Ref;
gMainEvent: EventRecord;
keYPressed: Boolean;
CONST
kStartStopResID = 1;
BEGIN
keyPressed := FALSE;
{ Enable keyboard interrupts }
IntSource(0);
{ Start the Memory Manager }
gMyMemoryID := MMStartUp;
{ Load the Toolbox tools as specified in the resource file }
gStartStopRef := StartupTools(gMyMemoryID,RefIsResource,Ref(kStartStopResID));
if _ToolErr = noError then
BEGIN
REPEAT
IF GetNextEvent(keyDownMask,gMainEvent) THEN
BEGIN
Writeln('The following key was pressed ''', Chr(gMainEvent.message), '''');
keyPressed := TRUE;
END
ELSE
Writeln('No key has been pressed.');
UNTIL keyPressed;
Readln;
END;
ShutDownTools(RefIsHandle,gStartStopRef);
END.
|
| |
Note: If you are creating a graphic textbook application (see chapter 7 of the TML Reference Manual), you do not need to explicitly startup the Toolbox tools as listed in the above example. A handy side-effect of using the Graphics(320) or Graphics(640) command is that the Event Manager is started automatically for you. I have repeated the above example, but this time using the graphics mode which makes the code a lot simpler.
The "Graphic Textbook" example code follows:
|
PROGRAM GraphKeyTest;
USES
Types,
Events;
VAR
gMainEvent: EventRecord;
keYPressed: Boolean;
BEGIN
{ Go into graphics mode }
Graphics(640);
keyPressed := FALSE;
REPEAT
IF GetNextEvent(keyDownMask,gMainEvent) THEN
BEGIN
Writeln('The following key was pressed ''', Chr(gMainEvent.message), '''');
keyPressed := TRUE;
END
ELSE
Writeln('No key has been pressed.');
UNTIL keyPressed;
Readln;
END. |
Back to Pascal Programming Page
Copyright © 2007-2018 by Michael Stephens
|
|
|