Skip to content

Commit

Permalink
feat(client/keybinds): added isPressed state for keybinds (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geden420 authored Sep 16, 2024
1 parent ff4ba29 commit 7bd2244
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions imports/addKeybind/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ if cache.game == 'redm' then return end
---@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 = {}

Expand All @@ -24,6 +26,7 @@ local GetControlInstructionalButton = GetControlInstructionalButton

local keybind_mt = {
disabled = false,
isPressed = false,
defaultKey = '',
defaultMapper = 'keyboard',
}
Expand All @@ -36,6 +39,10 @@ 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
Expand All @@ -48,13 +55,15 @@ function lib.addKeybind(data)
keybinds[data.name] = setmetatable(data, keybind_mt)

RegisterCommand('+' .. data.name, function()
if not data.onPressed or data.disabled or IsPauseMenuActive() then return end
data:onPressed()
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 data.onReleased or data.disabled or IsPauseMenuActive() then return end
data:onReleased()
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)
Expand Down

0 comments on commit 7bd2244

Please sign in to comment.