When a key is pressed it is stored in the $bfec01 register. To decode the register and find the value we have to execute a not.b and ror.b #1 to expose the raw key code. After this we can then do our checks to find out what key has been pressed.
To access the register and collect the necessary data we can use the following code
move.b $bfec01,d0 ; Keypress
ror.b #1,d0 ; d0 now contains the raw key
From the above code the data register D0 will now contain the raw key code. A raw key code is a hex representation of the key that has been selected. The image below (taken from http://whdload.de/docs/en/rawkey.html) show the Amiga keyboard along with the RAW key codes for each of the keys
We can then start checking what keys are pressed and branch to our desired sub-routine.
cmp.b #$50,d0 ;Check for $50 = F1
cmp.b #$51,d0 ;Check for $51 = F2
Lets test this out with our ‘Hello World’ code allowing using to check for the escape key to quit the program by exiting the busy loop and the ‘s’ key to start another loop.
ExecBase = 4
OpenLib = -552
OpenLibVersion = 34
CloseLib = -414
PutString = -948
MOVE.B $bfec01,d0 ; Keypress
ROR.b #1,d0 ; d0 now contains the raw key
CMP.b #$45,d0 ;Check for esc
CMP.b #$21,d0 ;Check for 'S'
NoLibError: CLR.L D0
DosName: DC.B "dos.library",0
DisplayString: DC.B "Hello World!",10,0
DisplayStartString: DC.B "Start",10,0
DisplayQuitString: DC.B "Quit",10,0
Again assembling the code and jumping over to UAE to run the program from the shell gives the usual hello world output but this time instead of clicking the left mouse button hit the escape key. You will see a control character be added to the cli but the minute you delete this from the window control is given back to the cli showing that the program has indeed exited. The same if the ‘s’ key has been pressed, we will branch to the ‘start’ section and loop until the mouse button has been pressed.
We have now created a very basic title loop and main loop. In other words title page and main game page.
Leave a Reply