Authors
Publication
Pub Details
Date
Pages
Most programs require user interaction through the keyboard, and use the INPUT or INKEY$ functions to do this job. This article will discuss some alternative ways to input through the keyboard.
Method #1
Hardware generated interrupts are used in the 2068 to update the TV frames counter and to scan the keyboard for pressed keys. If а pressed key is found, the character code associated with it is determined and stored in system variable LAST-K. If you POKE a zero in 23560, and then immediately PEEK the same location, the PEEK will return the code for a key pressed between the POKE and PEEK, provided that а scan has occurred in this time interval. То insure a scan, place a USR 737 after the POKE. This method is roughly like an INKEY$ function which returns a code rather than a string variable.
Method #2
1f you are willing to use a small amount of machine code, you can directly call the ROM routine which examines the keyboard. This is K-SCAN, located at 688d. (In the Spectrum, this same routine is at O28E). To use K-SCAN, you need to know the position code system used in the T/S, and you need to be able to get at the D and E registers, which is where the position codes are located when а return is made from K-SCAN to the calling routine. If no key is pressed, D end E hold 255; опе key results in 255 in D and the position code in E; two keys results in position codes in both D апd E. The position code is а value from 0 to 39, calculated as follows: (47-row #)-(8*column #). Here, a “row” means 5 keys іп a half-row, such as A S D F G. Rows are numbered 1 to 8, starting with the lower left row and going up and then down. A “column” consists of 8 keys, such as column 2: Z X W 2 9 O L Break/Space. There are 5 columns, numbered 1 thru 5, starting with the outer keys. (Note that there are two redundant keys which are ignored; these are the space-bar and the right side cap shift; these are keys added by Timex which perform no new function but make the keyboard a bit more like а typewriter). Unlike method #1 or INKEY$, method #2 allows you to handle two keys pressed at the same time.
Method #3
This method uses the IN function. For example, the BASIC statement LET A = IN 65278 will scan the 1st row (bottom left, 5 keys) and assign to A a value of 31 if no keys are pressed. (Note: Some published programs using IN are for the Spectrum version 2, whose base value is 255, not 51.) If the keys are pressed the value returned is the base value (31) minus the column value of any key pressed. Column values are 1, 2, 4, 8, and 16 for columns 1, 2, 3, 4, and 5 respectively. The number following IN must meet certain criteria. When expressed as a two byte binary number, the least significant (“low”) byte must be the port number of the keyboard (i.e. 254 decimal). The most significant (“high”) byte must have а “0” in the bit position corresponding to the row to be scanned. In the above example, 65278 in binary has as its high byte 11111110; since the zero is in the 1st bit position, the 1st row will be active when this statement is executed. Rather than get involved in decimal-binary conversions, you can also use a statement like: LET А = IN (256*BIN 11111110 + 254) to do the same thing. Note too, that you can put a zero in any position, or in any number of positions, in the binary number and simultaneously scan any combination of of rows with a single statement. (But, if you scan two rows at once, you cannot tell which row of the two a pressed key is in.) The BASIC equivalent of K-SCAN can be produced, of course, using eight IN statements. But unlike K-SCAN, you can detect the pressing of more than two keys.