Skip to content

tw.rawKeys

Side: Client

Raw keyboard input handling with QWERTY/AZERTY layout support.

ConVars

ConVarDefaultDescription
twinded_libs:keyboard_layout"qwerty"Keyboard layout ("qwerty" or "azerty")

Functions

tw.rawKeys.listen(key, callback) -> id

Registers a listener for a specific key press/release. Returns an ID that can be used to remove the listener.

ParameterTypeDescription
keystring or integerKey name (e.g. "E", "SPACE") or VK code
callbackfunctionCalled with is_pressed (boolean): true on key down, false on key up

Returns: integer — Listener ID

lua
local id = tw.rawKeys.listen("E", function(is_pressed)
    if is_pressed then
        print("E key pressed")
    else
        print("E key released")
    end
end)

tw.rawKeys.removeListener(id) -> boolean

Removes a previously registered key listener.

ParameterTypeDescription
idintegerListener ID returned by listen

Returns: booleantrue if the listener was found and removed.

lua
local removed = tw.rawKeys.removeListener(id)

tw.rawKeys.getKeyFromVK(vk_code) -> string | nil

Converts a virtual key code to a key name based on the current keyboard layout.

ParameterTypeDescription
vk_codeintegerVirtual key code

Returns: string or nil — Key name, or nil if unknown.

lua
local key_name = tw.rawKeys.getKeyFromVK(0x45) -- "E"

tw.rawKeys.getAliasFromStandardKey(key) -> string

Returns the layout-adjusted alias for a standard QWERTY key name. For example, on AZERTY layout, "Q" returns "A".

ParameterTypeDescription
keystringStandard QWERTY key name

Returns: string — Adjusted key name for the current layout.

lua
local adjusted = tw.rawKeys.getAliasFromStandardKey("Q")
-- "Q" on QWERTY, "A" on AZERTY

tw.rawKeys.getAllVK() -> table

Returns a table of all known virtual key codes and their key names.

Returns: table — Key-value table mapping VK codes to key names.

lua
local all_keys = tw.rawKeys.getAllVK()
for vk, name in pairs(all_keys) do
    print(vk, name)
end

Examples

Toggle a feature with a key

lua
local feature_active = false

tw.rawKeys.listen("F5", function(is_pressed)
    if is_pressed then
        feature_active = not feature_active
        print("Feature:", feature_active and "ON" or "OFF")
    end
end)

Temporary key listener

lua
local id = tw.rawKeys.listen("G", function(is_pressed)
    if is_pressed then
        print("G pressed!")
    end
end)

-- Later, remove the listener
tw.rawKeys.removeListener(id)

AZERTY-aware key binding

lua
-- This works correctly on both QWERTY and AZERTY layouts
local key = tw.rawKeys.getAliasFromStandardKey("E")
tw.rawKeys.listen(key, function(is_pressed)
    if is_pressed then
        -- interact
    end
end)

Notes

  • The keyboard layout is read from the twinded_libs:keyboard_layout ConVar. Players using AZERTY should set this in their server config or client settings.
  • Callbacks receive both press and release events. Check the is_pressed parameter to distinguish them.
  • Listener IDs are stable and unique. Store them if you need to remove listeners later.
  • Key names are case-insensitive strings (e.g. "E", "SPACE", "F5", "LSHIFT").

Premium RedM Scripts — Multi-Framework