Merge pull request #1752 from SNAILX808/esx_lib

Refactor keybind registration to use xLib.addKeybind
This commit is contained in:
Kremu
2026-01-14 16:24:14 +01:00
committed by GitHub
4 changed files with 105 additions and 18 deletions
+11
View File
@@ -1,5 +1,16 @@
--All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
function ESX.RegisterInput(command_name, label, input_group, key, on_press, on_release)
return xLib.addKeybind({
name = command_name,
description = label,
defaultMapper = input_group,
defaultKey = key,
onPressed = on_press,
onReleased = on_release
})
end
ESX.Game.GetShapeTestResultSync = xLib.Raycast.GetShapeTestResult
ESX.Game.RaycastScreen = xLib.Raycast.FromScreen
ESX.Game.StartRaycasting = xLib.Raycast.Start
-16
View File
@@ -245,22 +245,6 @@ function ESX.RefreshContext(...)
return IsResourceFound('esx_context') and exports['esx_context']:Refresh(...)
end
---@param command_name string The command name
---@param label string The label to show
---@param input_group string The input group
---@param key string The key to bind
---@param on_press function The function to call on press
---@param on_release? function The function to call on release
function ESX.RegisterInput(command_name, label, input_group, key, on_press, on_release)
local command = on_release and '+' .. command_name or command_name
RegisterCommand(command, on_press, false)
Core.Input[command_name] = ESX.HashString(command)
if on_release then
RegisterCommand('-' .. command_name, on_release, false)
end
RegisterKeyMapping(command, label or '', input_group or 'keyboard', key or '')
end
---@param menuType string
---@param open function The function to call on open
---@param close function The function to call on close
+2 -2
View File
@@ -6,6 +6,7 @@ lua54 'yes'
version '1.13.3'
shared_scripts {
'@esx_lib/imports.lua',
'locale.lua',
'shared/config/main.lua',
@@ -44,6 +45,7 @@ server_scripts {
client_scripts {
'client/main.lua',
'client/functions.lua',
'client/compat.lua',
'client/modules/wrapper.lua',
'client/modules/callback.lua',
'client/modules/adjustments.lua',
@@ -57,8 +59,6 @@ client_scripts {
'client/modules/interactions.lua',
'client/modules/scaleform.lua',
'client/modules/streaming.lua',
'shared/compat.lua'
}
ui_page {
@@ -0,0 +1,92 @@
--[[
https://github.com/overextended/ox_lib
This file is licensed under LGPL-3.0 or higher <https://www.gnu.org/licenses/lgpl-3.0.en.html>
Copyright © 2025 Linden <https://github.com/thelindat>
]]
---@class KeybindProps
---@field name string
---@field description string
---@field defaultMapper? string (see: https://docs.fivem.net/docs/game-references/input-mapper-parameter-ids/)
---@field defaultKey? string
---@field disabled? boolean
---@field disable? fun(self: CKeybind, toggle: boolean)
---@field onPressed? fun(self: CKeybind)
---@field onReleased? fun(self: CKeybind)
---@field remove? fun(self: CKeybind)
---@field [string] any
---@class CKeybind : KeybindProps
---@field currentKey string
---@field disabled boolean
---@field isPressed boolean
---@field hash number
---@field getCurrentKey fun(): string
---@field isControlPressed fun(): boolean
local keybinds = {}
local IsPauseMenuActive = IsPauseMenuActive
local GetControlInstructionalButton = GetControlInstructionalButton
local keybind_mt = {
disabled = false,
isPressed = false,
defaultKey = '',
defaultMapper = 'keyboard',
}
function keybind_mt:__index(index)
return index == 'currentKey' and self:getCurrentKey() or keybind_mt[index]
end
function keybind_mt:getCurrentKey()
return GetControlInstructionalButton(0, self.hash, true):sub(3)
end
function keybind_mt:isControlPressed()
return self.isPressed
end
function keybind_mt:disable(toggle)
self.disabled = toggle
end
function keybind_mt:remove()
if not keybinds[self.name] then return end
keybinds[self.name] = nil
end
---@param data KeybindProps
---@return CKeybind
function xLib.addKeybind(data)
data.hash = joaat("+" .. data.name) | 0x80000000
keybinds[data.name] = setmetatable(data, keybind_mt)
RegisterCommand('+' .. data.name, function()
if not keybinds[data.name] then return end
if data.disabled or IsPauseMenuActive() then return end
data.isPressed = true
if data.onPressed then data:onPressed() end
end)
RegisterCommand('-' .. data.name, function()
if not keybinds[data.name] then return end
if data.disabled or IsPauseMenuActive() then return end
data.isPressed = false
if data.onReleased then data:onReleased() end
end)
RegisterKeyMapping('+' .. data.name, data.description, data.defaultMapper, data.defaultKey)
SetTimeout(500, function()
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
end)
return data
end
return xLib.addKeybind