2025-03-18 18:46:09 +11:00
|
|
|
--[[
|
|
|
|
|
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 (c) 2025 Linden <https://github.com/thelindat/fivem>
|
|
|
|
|
]]
|
|
|
|
|
|
2022-11-25 18:05:55 -08:00
|
|
|
if cache.game == 'redm' then return end
|
|
|
|
|
|
2022-10-16 12:49:47 +11:00
|
|
|
---@class KeybindProps
|
2022-10-14 17:08:20 -06:00
|
|
|
---@field name string
|
|
|
|
|
---@field description string
|
2023-04-17 06:11:18 +02:00
|
|
|
---@field defaultMapper? string (see: https://docs.fivem.net/docs/game-references/input-mapper-parameter-ids/)
|
2022-10-14 17:08:20 -06:00
|
|
|
---@field defaultKey? string
|
|
|
|
|
---@field disabled? boolean
|
|
|
|
|
---@field disable? fun(self: CKeybind, toggle: boolean)
|
|
|
|
|
---@field onPressed? fun(self: CKeybind)
|
|
|
|
|
---@field onReleased? fun(self: CKeybind)
|
2022-10-16 12:49:47 +11:00
|
|
|
---@field [string] any
|
|
|
|
|
|
|
|
|
|
---@class CKeybind : KeybindProps
|
|
|
|
|
---@field currentKey string
|
2022-11-24 17:31:03 +11:00
|
|
|
---@field disabled boolean
|
2024-09-16 09:45:46 +02:00
|
|
|
---@field isPressed boolean
|
2022-11-24 17:31:03 +11:00
|
|
|
---@field hash number
|
2022-11-24 17:16:18 +11:00
|
|
|
---@field getCurrentKey fun(): string
|
2024-09-16 09:45:46 +02:00
|
|
|
---@field isControlPressed fun(): boolean
|
2022-10-14 17:08:20 -06:00
|
|
|
|
|
|
|
|
local keybinds = {}
|
|
|
|
|
|
|
|
|
|
local IsPauseMenuActive = IsPauseMenuActive
|
2022-11-24 17:16:18 +11:00
|
|
|
local GetControlInstructionalButton = GetControlInstructionalButton
|
|
|
|
|
|
|
|
|
|
local keybind_mt = {
|
|
|
|
|
disabled = false,
|
2024-09-16 09:45:46 +02:00
|
|
|
isPressed = false,
|
2023-11-28 00:34:53 +01:00
|
|
|
defaultKey = '',
|
|
|
|
|
defaultMapper = 'keyboard',
|
2022-11-24 17:16:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function keybind_mt:__index(index)
|
2023-11-28 00:34:53 +01:00
|
|
|
return index == 'currentKey' and self:getCurrentKey() or keybind_mt[index]
|
2022-11-24 17:16:18 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function keybind_mt:getCurrentKey()
|
2022-11-24 17:31:03 +11:00
|
|
|
return GetControlInstructionalButton(0, self.hash, true):sub(3)
|
2022-11-24 17:16:18 +11:00
|
|
|
end
|
2022-10-14 17:08:20 -06:00
|
|
|
|
2024-09-16 09:45:46 +02:00
|
|
|
function keybind_mt:isControlPressed()
|
|
|
|
|
return self.isPressed
|
|
|
|
|
end
|
|
|
|
|
|
2023-11-28 00:34:53 +01:00
|
|
|
function keybind_mt:disable(toggle)
|
|
|
|
|
self.disabled = toggle
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-16 12:49:47 +11:00
|
|
|
---@param data KeybindProps
|
2022-10-14 17:08:20 -06:00
|
|
|
---@return CKeybind
|
|
|
|
|
function lib.addKeybind(data)
|
2023-07-31 16:29:27 +10:00
|
|
|
---@cast data CKeybind
|
2023-11-28 00:34:53 +01:00
|
|
|
data.hash = joaat('+' .. data.name) | 0x80000000
|
|
|
|
|
keybinds[data.name] = setmetatable(data, keybind_mt)
|
2022-10-14 17:08:20 -06:00
|
|
|
|
|
|
|
|
RegisterCommand('+' .. data.name, function()
|
2024-09-16 09:45:46 +02:00
|
|
|
if data.disabled or IsPauseMenuActive() then return end
|
|
|
|
|
data.isPressed = true
|
|
|
|
|
if data.onPressed then data:onPressed() end
|
2022-10-14 17:08:20 -06:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterCommand('-' .. data.name, function()
|
2024-09-16 09:45:46 +02:00
|
|
|
if data.disabled or IsPauseMenuActive() then return end
|
|
|
|
|
data.isPressed = false
|
|
|
|
|
if data.onReleased then data:onReleased() end
|
2022-10-14 17:08:20 -06:00
|
|
|
end)
|
|
|
|
|
|
2023-04-17 06:11:18 +02:00
|
|
|
RegisterKeyMapping('+' .. data.name, data.description, data.defaultMapper, data.defaultKey)
|
2022-10-14 17:08:20 -06:00
|
|
|
|
2023-11-28 00:34:53 +01:00
|
|
|
if data.secondaryKey then
|
|
|
|
|
RegisterKeyMapping('~!+' .. data.name, data.description, data.secondaryMapper or data.defaultMapper, data.secondaryKey)
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-14 17:08:20 -06:00
|
|
|
SetTimeout(500, function()
|
|
|
|
|
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
|
|
|
|
|
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
|
|
|
|
|
end)
|
|
|
|
|
|
2023-07-31 16:29:27 +10:00
|
|
|
return data
|
2022-10-14 17:08:20 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return lib.addKeybind
|