mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
Some people accidentally, mistakenly, and without any malicious intent forget to attribute code from this resource to its source. This change will hopefully assist people, so that they do not make that mistake again.
92 lines
2.6 KiB
Lua
92 lines
2.6 KiB
Lua
--[[
|
|
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>
|
|
]]
|
|
|
|
if cache.game == 'redm' then return end
|
|
|
|
---@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 [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
|
|
|
|
---@param data KeybindProps
|
|
---@return CKeybind
|
|
function lib.addKeybind(data)
|
|
---@cast data CKeybind
|
|
data.hash = joaat('+' .. data.name) | 0x80000000
|
|
keybinds[data.name] = setmetatable(data, keybind_mt)
|
|
|
|
RegisterCommand('+' .. data.name, function()
|
|
if data.disabled or IsPauseMenuActive() then return end
|
|
data.isPressed = true
|
|
if data.onPressed then data:onPressed() end
|
|
end)
|
|
|
|
RegisterCommand('-' .. data.name, function()
|
|
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)
|
|
|
|
if data.secondaryKey then
|
|
RegisterKeyMapping('~!+' .. data.name, data.description, data.secondaryMapper or data.defaultMapper, data.secondaryKey)
|
|
end
|
|
|
|
SetTimeout(500, function()
|
|
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
|
|
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
|
|
end)
|
|
|
|
return data
|
|
end
|
|
|
|
return lib.addKeybind
|