Inkey()

Syntax

String$ = Inkey()
Description
Returns a two character string if a key is pressed during the call of Inkey(). It doesn't interrupt (halt) the program flow. If the result isn't empty, it contains two characters: the first one is the ASCII value of the character pressed, and the right part is it's associated number. It's useful for extended (non-ASCII) keys (for example, function keys, arrows, etc).

Parameters:
This command has no parameters.


Return value:
A string containing two characters representing the ASCII code (if possible, in the first character) and the raw keycode (in the second character).


Example:
If OpenConsole()
    PrintN("Press Escape to exit")

    Repeat
        key_pressed$ = Inkey()
        
        If key_pressed$<>""
            ; Show the key if it has a printable ASCII code
            If Asc(key_pressed$)>=32 And Asc(key_pressed$)<128
                PrintN("You pressed: "+Left(key_pressed$, 1))
                PrintN("It has a raw code of: "+Str(PeekB(@key_pressed$+1) & $FF))
            Else
                ; Either a non-printable ASCII code or an extended character
                If Asc(key_pressed$)=255 And skip_shift=0
                    PrintN("You pressed the extended (first char=255) key: "+Str(PeekB(@key_pressed$+1) & $FF))
                ElseIf Asc(key_pressed$)=224
                    skip_shift = 1
                    PrintN("You pressed a cursor key (first char=224) with raw code: "+Str(PeekB(@key_pressed$+1) & $FF))
                Else
                    ; If skip shift was 1 then we do not need to process this
                    ; because the key will be the shift key produced by the
                    ; cursor key
                    If skip_shift=1
                        skip_shift = 0
                    Else
                        PrintN("The key you pressed has raw values of: "+Str(PeekB(@key_pressed$) & $FF)+" and "+Str(PeekB(@key_pressed$+1) & $FF))
                    EndIf
                EndIf
            EndIf
        Else
            Delay(50) ; Don't eat all the CPU time, we're on a multitask OS
        EndIf
        
    ; Wait until escape is pressed
    Until Left(key_pressed$, 1) = Chr(27)

    CloseConsole()
EndIf
End


Notes:
It is not only alphanumeric keys that have an ASCII value. The escape key (27), return key (10) tab key (9) and backspace key (8) are just four examples.

If it is an extended key then the first character will have an ASCII code of 255 and the second character will contain the raw code.

The exception to this is the Insert, Home, Page up, Delete, End and Page down keys, which are between the main keyboard and the numeric keypad. The first character has an ASCII code of 224 and the second character is the raw code. This is then followed by the key codes for the shift key. The reason for this is that when the 'NumLock' is turned on you must press shift with the keys on the numeric keypad to get the functions such as Insert. In that case the first character will have an ASCII code of 255, so it seems that the separate keys for Insert, etc, use 224 so you can distinguish between them.

The ASCII codes and numeric values reported in this description may change depending on the code page you have configured at boot time for keyboard input. However, the concepts should be the same and you can use the above example to figure out the real values for your system.

Supported OS

Windows, Linux, MacOS X

<- ConsoleTitle() - Console Index - Input() ->