mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
feat(imports/addKeybind): support secondary keybind (#450)
This commit is contained in:
@@ -19,39 +19,33 @@ if cache.game == 'redm' then return end
|
|||||||
|
|
||||||
local keybinds = {}
|
local keybinds = {}
|
||||||
|
|
||||||
local function disableKeybind(self, toggle)
|
|
||||||
keybinds[self.name].disabled = toggle
|
|
||||||
end
|
|
||||||
|
|
||||||
local IsPauseMenuActive = IsPauseMenuActive
|
local IsPauseMenuActive = IsPauseMenuActive
|
||||||
local GetControlInstructionalButton = GetControlInstructionalButton
|
local GetControlInstructionalButton = GetControlInstructionalButton
|
||||||
|
|
||||||
local keybind_mt = {
|
local keybind_mt = {
|
||||||
disabled = false,
|
disabled = false,
|
||||||
|
defaultKey = '',
|
||||||
|
defaultMapper = 'keyboard',
|
||||||
}
|
}
|
||||||
|
|
||||||
function keybind_mt:__index(index)
|
function keybind_mt:__index(index)
|
||||||
local value = keybind_mt[index]
|
return index == 'currentKey' and self:getCurrentKey() or keybind_mt[index]
|
||||||
|
|
||||||
if value then
|
|
||||||
return value
|
|
||||||
end
|
|
||||||
|
|
||||||
if index == 'currentKey' then
|
|
||||||
return self:getCurrentKey()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function keybind_mt:getCurrentKey()
|
function keybind_mt:getCurrentKey()
|
||||||
return GetControlInstructionalButton(0, self.hash, true):sub(3)
|
return GetControlInstructionalButton(0, self.hash, true):sub(3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function keybind_mt:disable(toggle)
|
||||||
|
self.disabled = toggle
|
||||||
|
end
|
||||||
|
|
||||||
---@param data KeybindProps
|
---@param data KeybindProps
|
||||||
---@return CKeybind
|
---@return CKeybind
|
||||||
function lib.addKeybind(data)
|
function lib.addKeybind(data)
|
||||||
---@cast data CKeybind
|
---@cast data CKeybind
|
||||||
if not data.defaultKey then data.defaultKey = '' end
|
data.hash = joaat('+' .. data.name) | 0x80000000
|
||||||
if not data.defaultMapper then data.defaultMapper = 'keyboard' end
|
keybinds[data.name] = setmetatable(data, keybind_mt)
|
||||||
|
|
||||||
RegisterCommand('+' .. data.name, function()
|
RegisterCommand('+' .. data.name, function()
|
||||||
if not data.onPressed or data.disabled or IsPauseMenuActive() then return end
|
if not data.onPressed or data.disabled or IsPauseMenuActive() then return end
|
||||||
@@ -65,16 +59,15 @@ function lib.addKeybind(data)
|
|||||||
|
|
||||||
RegisterKeyMapping('+' .. data.name, data.description, data.defaultMapper, data.defaultKey)
|
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()
|
SetTimeout(500, function()
|
||||||
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
|
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
|
||||||
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
|
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
data.hash = joaat('+' .. data.name) | 0x80000000
|
|
||||||
data.disabled = data.disabled
|
|
||||||
data.disable = disableKeybind
|
|
||||||
keybinds[data.name] = setmetatable(data, keybind_mt)
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user