There are some useful
keyboard functions in
user32.dll that can be used when managing keyboard input. Here's some code for some of the most useful ones:
1. Obtain the keyboard layout:
load dll "user32.dll", 1
layout = call dll(1, "GetKeyboardLayout", 0)
primary = layout && 0x01FF
sub = (layout && 0xFE01) >> 10
print "Primary Language ID: 0x" + hex$(primary)
print "SubLanguage ID : 0x" + hex$(sub)
wait key
A list of
primary language and
sublanguage IDs can be found
here. Note that there are probably several layouts which may be the same or have only minor differences.
2. Get a key's name based on its scancode:
load dll "user32.dll", 1
name$ = space$(32) ` Precreate buffer to store key name
do
cls
scan = scancode()
lParam = scan << 16
result = call dll(1, "GetKeyNameTextA", lParam, name$, 32)
print name$
loop
However it returns the name in the currently selected language, and while it returns the name for most keys it does not seem to work for certain keys such as up/down/left/right/home/etc.
3. Determine the scancode of a key based on its character value (most useful):
load dll "user32.dll", 1
key = asc("~")
virtualKey = call dll(1, "VkKeyScanA", key) && 0xFF
scan = call dll(1, "MapVirtualKeyA", virtualKey, 0)
print "Scancode for key '" + chr$(key) + "' is " + str$(scan)
wait key