Skip to content

Keybind (tm.keybind) ​

Context: client only.

One call registers the command, maps the key (RegisterKeyMapping) and wires press and release handlers (RegisterCommand). The handlers stay in the calling resource.

lua
tm.keybind({
    name = 'openInventory',
    description = 'Open inventory',
    defaultKey = 'TAB',
    onPressed = function(source, args, raw, keybind) end,
})

tm.keybind(options) is the same as tm.keybind.register(options) and returns a RegisteredKeybind, or nil (with a logged error) when no name was given.

Options ​

FieldAliasesDefaultDescription
namecommandrequiredCommand name. A leading + or - is ignored.
descriptionhelpthe nameLabel in the GTA key settings.
defaultMapperdefault_map'keyboard'Input device.
defaultKeydefault_key'' (unbound)Default key such as 'E', 'F5', 'LSHIFT'.
onPressedonPress, onDownnoneCalled on key down.
onReleasedonUpnoneCalled on key up. Makes the bind a +name and -name pair.
useExistingCommandfalseSkip RegisterCommand when the command is registered elsewhere (the key is still mapped).

Handlers receive what RegisterCommand gives them: (source, args, raw, options). An error inside a handler is logged and never escapes into the command system.

Behavior ​

  • Press only: one command name. With onReleased: commands +name and -name, mapped through +name.
  • The return value is { command, release?, description, defaultMapper, defaultKey } (command is '+name' for a pair).
  • Registering the same name again replaces the handlers and never registers a second command or mapping. A bind that started as press only stays press only (the release handler is ignored with a warning).
  • Players can rebind the key in their GTA settings, the default is only the starting point.

Other functions ​

FunctionDescription
tm.keybind.isRegistered(name)True when this resource has an active bind with that name.
tm.keybind.unregister(name)Stops the handlers from running. The game cannot remove a command or key mapping before the resource stops, so they stay registered. Registering the name again turns it back on.
lua
tm.keybind({
    name = 'aimMode',
    defaultKey = 'LMOUSEBUTTON',
    defaultMapper = 'MOUSE_BUTTON',
    onPressed = function() TriggerEvent('aim:start') end,
    onReleased = function() TriggerEvent('aim:stop') end,
})

Editors and the input lock ​

While an in-world editor holds tm.inputlock (zone creators, gizmos), key presses are ignored so an editor that reads a key does not also trigger the inventory or radial. Releases still run, so a key that was already down never sticks. A binding that must keep working inside an editor sets ignoreEditorLock = true.