Keyboard Layout Functions

Identify the active keyboard layout and resolve a key name to the codes behind it.

These functions are exported by the KS module:

#Import "Ks" { GetKeyboardLayout, GetKeyInfo }

GetKeyboardLayout

Layout := GetKeyboardLayout()

Returns Keysharp's identifier for the current keyboard layout, as a readable platform-native string. Returns "unknown" if the platform reports no layout.

Note: The string is platform-specific. Treat it as an opaque value to compare or to pass to GetKeyInfo, not as a value to parse.

GetKeyInfo

Info := GetKeyInfo(KeyName , Layout)

Parameters

KeyName

Type: String

A key name from the key list, or a single character.

Layout

Type: String

If omitted, the current layout is used. Otherwise, specify a layout string obtained from GetKeyboardLayout to resolve the key as that layout would.

Return Value

Type: Object or Integer

Returns 0 if KeyName cannot be resolved. Otherwise returns an object with the following properties:

PropertyTypeValue
VKIntegerThe virtual key code.
SCIntegerThe scan code.
NameStringThe canonical key name.
ModifiersIntegerThe modifiers needed to produce the key, as a bit mask.
PrefixStringThose modifiers as hotkey prefix symbols, for example +^.

Examples

Reports the codes behind a character.

#Import "Ks" { GetKeyInfo }

Info := GetKeyInfo("!")
if Info
    MsgBox "VK " Info.VK ", SC " Info.SC ", typed as " Info.Prefix Info.Name

Tests whether a key name is valid, since an unresolved name returns 0.

#Import "Ks" { GetKeyInfo }

for Name in ["Esc", "NotAKey"]
    MsgBox Name ": " (GetKeyInfo(Name) ? "resolved" : "unknown")

List of Keys, GetKeyName, GetKeyVK, GetKeySC, GetKeyState