Here some code from my command line snake game (Key_Null is Simplest way to proceed (I guess) is to set up a loop, containing the default action code, and the ReadKey statement(s) placed intoĪn if KeyPressed block. In a situation where your program is running, doing some iterative stuff by default, whereas given keyboard keys pressed change this default advancement, the Until Key = Key_ESC // terminate the program if ESC has been pressed Key := ReadKey // retrieve the second character If Key = Key_NULL then begin // NULL character indicates a two-characters key Write('ASCII code of key pressed is : ') Key := ReadKey // retrieve key pressed (as character) Writeln('Hit the key to display the code of or ESC to terminate') Writeln Here is the source (use the following link toĭownload it, together with another small program, that displays the ASCII table for codes 32 to 126). The ESC key) waits for a keyboard key being pressed and for each key pressed, displays the corresponding ASCII code. Long years ago, I wrote a small program, that in a loop (terminated by So, if you want to test if the user of your program has pressed some special key, you have to know if it is a one- or two-character key and what is the ASCII codeĬorresponding to the character returned by the ReadKey function. Example:ĬTRL+SHIFT+c → 3 ALT+CTRL+SHIFT+c → 0 + 46. Only one of the control keys will be retained and this by considering the priority ALT > CTRL > SHIFT. Note that multiple control keys are not handled by ReadKey. The TAB key corresponds to ASCII code 9, the rightwards arrow to 0 + 77Ĭ → 99, C (SHIFT+c) → 67, CTRL+c → 3 (corresponding to special key BREAK), ALT+c → 0 + 46. The character NULL + some other character. For other keys, ReadKey returns 2 characters: One single character (or speaking numeric, the ASCII code corresponding to the key pressed). For the keys of printable characters and the most common special keys, it returns Most keystrokes, simple or combinations, may be retrieved correctlyīy ReadKey thanks to a special feature of this function. Not only that there is a whole set of special keys (the function keys F1 – F12 alone need 12 codes), but there isĪlso the combination of a control key with other keys, as for example CTRL+C or ALT+ENTER. Possible to describe all possible keyboard keys. It seems obvious, that with 128 ASCII codes it's not In fact, the ReadKey function is somewhat more complicated than described above. For example: Chr(27) is equivalent to #27. Note, that character constants may be writtenĪs the corresponding ASCII code, preceded by the number sign(#). Inversely, you can convert a character to the corresponding ASCII code using the function Ord. Knowing the ASCII code, you can convert it into a character, using the function Chr. The output, you get, depends on the code page used. Printable characters are in the range from 32 to 126, 127 is the ESC key, characters above 127 give no output if the system codepage is actually 65001 (UTF8), otherwise, ASCII code 0 corresponds to the NULL character, ASCII codes from 1 to 31 are control or special characters, Keys? Characters are internally stored as ASCII codes. No problem for letters, numbers and symbols, but how to deal with special and command keys, like ENTER, ESC, TAB, or the arrow The last keyboard key pressed may be retrieved by using the function ReadKey (also defined in the Crt unit). Write('Hit any key to terminate the program. Terminating a program by hitting any key In the Crt unit, thus we must specify this unit in the uses clause. ![]() If we want to terminate the program with any keystroke, we can use the Boolean function KeyPressed within a loop. Write('Hit ENTER to terminate the program. ![]() The simplest way to do so, is to tell the user to push ENTER to exit a simple Readln may be used in this case: // Terminating a program by hitting the ENTER key Thus, if you want to be sure to view the program's output, you'll have to pause the program before it reaches theĮnd of the code. On MS Windows, no idea if this applies to Linux, too). If you run a command line program by double-clicking the executable, the Command Prompt window is automatically closed when the program terminates (this is how it works ![]() How to do it in a Lazarus GUI application?Ĭatching key strokes in Free Pascal command line programs. How to proceed to get the corresponding character or code, in the case a special or control character was pressed? How to do it in a simple command line program and How the program or application notices, when a key is pressed? This tutorial is about reacting, when the user strokes a key on the keyboard. Catching keystrokes in Free Pascal programs and applications.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |