|
13 | 13 |
|
14 | 14 |
|
15 | 15 | ---@enum eControlType |
16 | | -eControlType = { |
| 16 | +eControlType = { |
17 | 17 | KEYBOARD = 0x0, |
18 | 18 | CONTROLLER = 0x1 |
19 | 19 | } |
20 | 20 |
|
21 | 21 | ---@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 |
23 | 23 | DIGIT_0 = 0x30, |
24 | 24 | DIGIT_1 = 0x31, |
25 | 25 | DIGIT_2 = 0x32, |
@@ -198,19 +198,19 @@ eVirtualKeyCodes = { -- https://learn.microsoft.com/en-us/windows/win32/inputdev |
198 | 198 | Z = 0x5A, |
199 | 199 | } |
200 | 200 |
|
201 | | -local WM_KEYDOWN <const> = 0x0100 |
202 | | -local WM_KEYUP <const> = 0x0101 |
| 201 | +local WM_KEYDOWN <const> = 0x0100 |
| 202 | +local WM_KEYUP <const> = 0x0101 |
203 | 203 | local WM_LBUTTONDOWN <const> = 0x0201 |
204 | | -local WM_LBUTTONUP <const> = 0x0202 |
| 204 | +local WM_LBUTTONUP <const> = 0x0202 |
205 | 205 | 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 |
208 | 208 | 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 |
212 | 212 | local WM_XBUTTONDOWN <const> = 0x020B |
213 | | -local WM_XBUTTONUP <const> = 0x020C |
| 213 | +local WM_XBUTTONUP <const> = 0x020C |
214 | 214 |
|
215 | 215 | --#endregion |
216 | 216 |
|
@@ -383,40 +383,55 @@ function KeyManager:IsAnyKeyPressed() |
383 | 383 | end |
384 | 384 |
|
385 | 385 | ---@param keybindName string |
386 | | -function KeyManager:IsKeybindPressed(keybindName) |
| 386 | +---@return eControlType, (integer|string)? |
| 387 | +function KeyManager:GetKeybind(keybindName) |
387 | 388 | local control = self:GetCurrentControlType() |
| 389 | + local key = nil |
388 | 390 | 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] |
391 | 392 | end |
392 | 393 |
|
393 | 394 | if (control == eControlType.CONTROLLER) then |
394 | 395 | 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 |
397 | 398 | end |
398 | 399 |
|
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) |
400 | 418 | end |
401 | 419 |
|
402 | 420 | return false |
403 | 421 | end |
404 | 422 |
|
405 | 423 | ---@param keybindName string |
| 424 | +---@return boolean |
406 | 425 | 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 |
412 | 428 |
|
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 |
418 | 432 |
|
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) |
420 | 435 | end |
421 | 436 |
|
422 | 437 | return false |
@@ -489,9 +504,13 @@ function KeyManager:UpdateKeybind(oldKey, newKey) |
489 | 504 | return |
490 | 505 | end |
491 | 506 |
|
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 | + |
493 | 512 | self:RemoveKeybind(oldKey) |
494 | | - self:RegisterKeybind(newKey.id, prev_func, on_key_down) |
| 513 | + self:RegisterKeybind(newKey.id, callback, on_key_down) |
495 | 514 | end |
496 | 515 |
|
497 | 516 | ---@param key eVirtualKeyCodes | string |
|
0 commit comments