Check If a Key is Pressed in Basic4GL

How to Check if a Key is Pressed in Basic4GL

Checking whether or not a key is pressed down is the first step of programming more complicated player control using the keyboard. How you check a key will depend on whether it is a "typeable key" or if it is a special key, such as the "arrow" keys.

Checking "Typeable" Key

A typeable key is a key that you type to get a letter, number or character. For these keys, you can use KeyDown().

If KeyDown("A") Then Printr " ' A ' key down. " EndIF

Checking Special Keys

Checking special keys requires ScanKeyDown() and a virtual keycode. A virtual keycode is a built-in constant that refers to that key. They begin with vk_.

If ScanKeyDown(vk_left) Then Printr " ' Left ' key down." EndIF

Complete Source Code

TextMode(TEXT_BUFFERED)

Do 
    Cls
    If KeyDown("A") Then Printr " ' A ' key down. " EndIF
    If ScanKeyDown(vk_left) Then Printr " ' Left ' key down." EndIF

    Printr "(press escape to exit)"
    DrawText()
Loop

End

This page has been recommended for cleanup, integration, etc.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.