Skip to navigation

Revs on the BBC Micro

Keyboard: GetTextInput

Name: GetTextInput [Show more] Type: Subroutine Category: Keyboard Summary: Fetch a string from the keyboard, padded with spaces if required
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * GetDriverName calls GetTextInput * GetNumberInput calls GetTextInput

This routine fetches a string of characters from the keyboard and stores the result in memory. The string is entered by pressing RETURN, at which point the string in memory is padded with spaces so that it meets the required length. The DELETE key is supported, leading spaces are ignored, and the ESCAPE key is trapped and has no effect.
Arguments: (Y A) The address where the string should be stored X The length of string that we require
Returns: Y The number of characters entered, before any padding is applied
.GetTextInput STA P \ Set (Q P) = (Y A) STY Q STX W \ Store the required length of input in W LDA #2 \ Call OSBYTE with A = 2 and X = 0 to select the LDX #0 \ keyboard as the input stream and disable RS423 JSR OSBYTE LDA #21 \ Call OSBYTE with A = 21 and X = 0 to flush the LDX #0 \ keyboard buffer JSR OSBYTE .text1 LDY #0 \ Set Y = 0 to use as a counter for the number of \ characters entered .text2 JSR OSRDCH \ Call OSRDCH to read a character from the currently \ selected input stream (i.e. the keyboard) into A BCS text7 \ If the call to OSRDCH set the C flag then there was an \ error (probably caused by pressing ESCAPE), so jump to \ text7 to process this CMP #13 \ If RETURN was pressed, jump to text9 BEQ text9 CMP #' ' \ If a control character was entered (i.e. with an ASCII BCC text2 \ code less than that of " "), jump back to text2 to \ ignore it and wait for another key press BNE text3 \ If a key other than SPACE was pressed, jump to text3 \ skip the next two instructions CPY #0 \ SPACE was pressed, so if no other characters have been BEQ text2 \ (i.e. Y = 0), jump back to text2 to ignore it .text3 CMP #127 \ If the character entered has an ASCII value < 127, BCC text4 \ jump to text4 to process it as a valid character BNE text2 \ If the character entered has an ASCII value > 127, \ jump back to text2 to ignore it \ If we get here then the DELETE key was pressed, which \ has an ASCII value of 127 DEY \ Decrement the number of characters entered in Y, to \ process the deletion BPL text6 \ If Y is still positive, jump to text6 to print the \ delete character, which will delete the last character \ entered on-screen BMI text1 \ Y is negative, so we just deleted past the start of \ the entered string, so jump to text1 to set Y to 0 and \ start again (this BMI is effectively a JMP as we just \ passed through a BPL) .text4 \ If we get here then a valid character was entered CPY W \ If the number of characters entered in Y is not yet BNE text5 \ the required number in W, jump to text5 to store the \ new character LDA #7 \ Otherwise set A = 7 (the ASCII code for a beep) and BNE text6 \ jump to text6 to skip storing the new character and \ make a beep, as we already have enough characters .text5 \ If we get here then we have successfully fetched a new \ character, so now we store it STA (P),Y \ Store the character entered in the Y-th byte of (Q P) INY \ Increment the character counter in Y .text6 JSR OSWRCH \ Print the character in A, which will either be the new \ character, or a beep, or a delete JMP text2 \ Jump up to text2 to fetch the next character .text7 \ If we get here then ESCAPE was pressed TYA \ Store the character count in Y on the stack PHA LDA #126 \ Call OSBYTE with A = 126 to acknowledge the ESCAPE JSR OSBYTE \ condition PLA \ Retrieve the character count from the stack into Y TAY JMP text2 \ Jump up to text2 to fetch the next character .text8 INY \ We get here from below after appending a space to the \ stored string, so we increment Y and repeat the \ padding process until the string is full .text9 \ If we get here then RETURN was pressed CPY W \ If the number of characters entered in Y is not yet BNE text10 \ the required number in W, jump to text10 to pad out \ the string with spaces RTS \ Otherwise the string is the correct size, so we now \ return from the subroutine .text10 LDA #' ' \ Append a space to the end of the stored string STA (P),Y BNE text8 \ Jump back to text8 to keep padding the string with \ spaces (this BNE is effectively a JMP as A is never \ zero)