Skip to content

Commit 3a563f2

Browse files
committed
fix(keybinds): fix reset logic
1 parent 40b860f commit 3a563f2

2 files changed

Lines changed: 57 additions & 32 deletions

File tree

SSV2/includes/frontend/settings/keybinds_ui.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ local function DrawKeybind(gvarKey, isController)
8888
local resetPopup = _F("%s##%s%s", reset_label, label, currentKeyName)
8989
ImGui.SameLine()
9090
ImGui.BeginDisabled(is_default or default_key == nil)
91-
if (GUI:Button(reset_label)) then
91+
if (GUI:Button(_F("%s##%s", reset_label, label))) then
9292
ImGui.OpenPopup(resetPopup)
9393
end
9494
ImGui.EndDisabled()
@@ -108,14 +108,20 @@ local function DrawKeybind(gvarKey, isController)
108108
GUI:Tooltip(_T("SETTINGS_KEYBINDS_NO_UNBIND"))
109109
end
110110

111-
if (default_key and ImGui.DialogBox(resetPopup, _T("SETTINGS_KEYBINDS_RESET_COFNIRM"), ImGuiDialogBoxStyle.WARN)) then
111+
if (ImGui.DialogBox(resetPopup, _T("SETTINGS_KEYBINDS_RESET_COFNIRM"), ImGuiDialogBoxStyle.WARN)) then
112112
local newKey = isController and table.copy(default_key) or default_key
113113
table.set_nested_key(GVars, current_path, newKey)
114+
if (not isController and type(newKey) ~= "table") then
115+
KeyManager:UpdateKeybind(current_key, { id = newKey })
116+
end
114117
end
115118

116119
if (ImGui.DialogBox(unbindPopup, _T("SETTINGS_KEYBINDS_UNBIND_COFNIRM"), ImGuiDialogBoxStyle.WARN)) then
117120
local newKey = isController and { name = "Unbound", code = 0 } or "Unbound"
118121
table.set_nested_key(GVars, current_path, newKey)
122+
if (not isController and type(newKey) ~= "table") then
123+
KeyManager:UpdateKeybind(current_key, { id = newKey })
124+
end
119125
end
120126

121127
ImGui.SetNextWindowSize(400, 220)

SSV2/includes/services/KeyManager.lua

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414

1515
---@enum eControlType
16-
eControlType = {
16+
eControlType = {
1717
KEYBOARD = 0x0,
1818
CONTROLLER = 0x1
1919
}
2020

2121
---@enum eVirtualKeyCodes
22-
eVirtualKeyCodes = { -- https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
22+
eVirtualKeyCodes = { -- https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
2323
DIGIT_0 = 0x30,
2424
DIGIT_1 = 0x31,
2525
DIGIT_2 = 0x32,
@@ -198,19 +198,19 @@ eVirtualKeyCodes = { -- https://learn.microsoft.com/en-us/windows/win32/inputdev
198198
Z = 0x5A,
199199
}
200200

201-
local WM_KEYDOWN <const> = 0x0100
202-
local WM_KEYUP <const> = 0x0101
201+
local WM_KEYDOWN <const> = 0x0100
202+
local WM_KEYUP <const> = 0x0101
203203
local WM_LBUTTONDOWN <const> = 0x0201
204-
local WM_LBUTTONUP <const> = 0x0202
204+
local WM_LBUTTONUP <const> = 0x0202
205205
local WM_MBUTTONDOWN <const> = 0x0207
206-
local WM_MBUTTONUP <const> = 0x0208
207-
local WM_MOUSEWHEEL <const> = 0x020A
206+
local WM_MBUTTONUP <const> = 0x0208
207+
local WM_MOUSEWHEEL <const> = 0x020A
208208
local WM_RBUTTONDOWN <const> = 0x0204
209-
local WM_RBUTTONUP <const> = 0x0205
210-
local WM_SYSKEYDOWN <const> = 0x0104
211-
local WM_SYSKEYUP <const> = 0x0105
209+
local WM_RBUTTONUP <const> = 0x0205
210+
local WM_SYSKEYDOWN <const> = 0x0104
211+
local WM_SYSKEYUP <const> = 0x0105
212212
local WM_XBUTTONDOWN <const> = 0x020B
213-
local WM_XBUTTONUP <const> = 0x020C
213+
local WM_XBUTTONUP <const> = 0x020C
214214

215215
--#endregion
216216

@@ -383,40 +383,55 @@ function KeyManager:IsAnyKeyPressed()
383383
end
384384

385385
---@param keybindName string
386-
function KeyManager:IsKeybindPressed(keybindName)
386+
---@return eControlType, (integer|string)?
387+
function KeyManager:GetKeybind(keybindName)
387388
local control = self:GetCurrentControlType()
389+
local key = nil
388390
if (control == eControlType.KEYBOARD) then
389-
local kbKeyStr = GVars.keyboard_keybinds[keybindName]
390-
return kbKeyStr and KeyManager:IsKeyPressed(kbKeyStr) or false
391+
key = GVars.keyboard_keybinds[keybindName]
391392
end
392393

393394
if (control == eControlType.CONTROLLER) then
394395
local gpad_t = GVars.gamepad_keybinds[keybindName]
395-
if (type(gpad_t) ~= "table" or not gpad_t.code or gpad_t.code == 0) then
396-
return false
396+
if (type(gpad_t) ~= "table" or not gpad_t.code) then
397+
key = 0
397398
end
398399

399-
return PAD.IS_CONTROL_PRESSED(0, gpad_t.code)
400+
key = gpad_t.code
401+
end
402+
403+
return control, key
404+
end
405+
406+
---@param keybindName string
407+
---@return boolean
408+
function KeyManager:IsKeybindPressed(keybindName)
409+
local controlType, key = self:GetKeybind(keybindName)
410+
if (not key) then return false end
411+
412+
if (controlType == eControlType.KEYBOARD) then
413+
return self:IsKeyPressed(key)
414+
end
415+
416+
if (controlType == eControlType.CONTROLLER and type(key) == "number") then
417+
return key ~= 0 and PAD.IS_CONTROL_PRESSED(0, key)
400418
end
401419

402420
return false
403421
end
404422

405423
---@param keybindName string
424+
---@return boolean
406425
function KeyManager:IsKeybindJustPressed(keybindName)
407-
local control = self:GetCurrentControlType()
408-
if (control == eControlType.KEYBOARD) then
409-
local kbKeyStr = GVars.keyboard_keybinds[keybindName]
410-
return kbKeyStr and KeyManager:IsKeyJustPressed(kbKeyStr) or false
411-
end
426+
local controlType, key = self:GetKeybind(keybindName)
427+
if (not key) then return false end
412428

413-
if (control == eControlType.CONTROLLER) then
414-
local gpad_t = GVars.gamepad_keybinds[keybindName]
415-
if (type(gpad_t) ~= "table" or not gpad_t.code or gpad_t.code == 0) then
416-
return false
417-
end
429+
if (controlType == eControlType.KEYBOARD) then
430+
return self:IsKeyJustPressed(key)
431+
end
418432

419-
return PAD.IS_CONTROL_JUST_PRESSED(0, gpad_t.code)
433+
if (controlType == eControlType.CONTROLLER and type(key) == "number") then
434+
return key ~= 0 and PAD.IS_CONTROL_JUST_PRESSED(0, key)
420435
end
421436

422437
return false
@@ -489,9 +504,13 @@ function KeyManager:UpdateKeybind(oldKey, newKey)
489504
return
490505
end
491506

492-
local prev_func, on_key_down = registered.callback, registered.m_repeat_on_hold
507+
local callback, on_key_down = registered.callback, registered.m_repeat_on_hold
508+
if (type(newKey.newCallback) == "function") then
509+
callback = newKey.newCallback
510+
end
511+
493512
self:RemoveKeybind(oldKey)
494-
self:RegisterKeybind(newKey.id, prev_func, on_key_down)
513+
self:RegisterKeybind(newKey.id, callback, on_key_down)
495514
end
496515

497516
---@param key eVirtualKeyCodes | string

0 commit comments

Comments
 (0)