gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
local eWasDown = false local qWasDown = false function checkKeyState() if…
- User
local eWasDown = false local qWasDown = false
function checkKeyState()
if isKeyDown(0x12) then -- VK_MENU 0x12 ALT key if isKeyDown(0x51) and not qWasDown then -- 0x51 Q key print('q action') qWasDown = true elseif qWasDown and not isKeyDown(0x51) then qWasDown = false end if isKeyDown(0x45) and not eWasDown then -- 0x51 Q key print('e action') eWasDown = true elseif eWasDown and not isKeyDown(0x45) then eWasDown = false end endend
упрости этот код
- Assistant
Certainly! Here's a simplified version of the code without changing the logic:
local wasKeyDown = { e = false, q = false } function checkKeyState() if isKeyDown(0x12) then -- VK_MENU 0x12 ALT key for key, code in pairs({q = 0x51, e = 0x45}) do if isKeyDown(code) and not wasKeyDown[key] then print(key .. ' action') wasKeyDown[key] = true elseif wasKeyDown[key] and not isKeyDown(code) then wasKeyDown[key] = false end end end endChanges Made:
- Used a table
wasKeyDownto store the states of keyseandq. - Utilized a loop to reduce code repetition, iterating over the keys and their respective codes.
- This simplifies the code and makes it more maintainable, especially if you plan to add more key checks in the future.
- Used a table
- User
без цикла фор
- Assistant
Certainly! Here's a simplified version of the code without using a
forloop, while maintaining the original functionality:local eWasDown = false local qWasDown = false function checkKeyState() if isKeyDown(0x12) then -- VK_MENU 0x12 ALT key -- Check Q key if isKeyDown(0x51) then -- 0x51 Q key if not qWasDown then print('q action') qWasDown = true end else qWasDown = false end -- Check E key if isKeyDown(0x45) then -- 0x45 E key if not eWasDown then print('e action') eWasDown = true end else eWasDown = false end end endExplanation:
- The logic remains the same, but I've rearranged the conditional checks slightly for clarity.
- The key state checks are still performed individually, with improved structure for readability.