Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Track Multiple Key Presses?

Asked by 3 years ago

Is it possible to track multiple key presses with one script? So if I hold Q and E it prints "E"? Here is my current code:

function onKeyPress(inputObject, gameProcessedEvent)
    if not gameProcessedEvent then
        if inputObject.KeyCode == Enum.KeyCode.Q and inputObject.KeyCode == Enum.KeyCode.E  then
            print("E")
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Any help would be helpful. Also the "and" operation won't work. I already tried.

1 answer

Log in to vote
1
Answered by 3 years ago
local RunService = game:GetSerivce("RunService")
local UserInputService = game:GetService("UserInputService")

local KEY_1 = Enum.KeyCode.Q
local KEY_2 = Enum.KeyCode.E
local PRINT_MESSAGE = "HELL YES!"

local function onStepped()
    local keys = UserInputService:GetKeysPressed()

    if keys[KEY_1] and keys[KEY_2] then
        print(PRINT_MESSAGE)
    end
end

RunService.Stepped:Connect(onStepped)

Here is one method. This method might be cleaner if you have a larger scale game or are doing a lot of this stuff. The other method is to have runservice connection / loop check keys from a table that is edited by inputbegan and inputended. what you would do is add and remove "current keys" when these are fired, and then check in the runservice connection / loop. By the way, InputBegan can only have 1 keycode per time fired, meaning each time a key is pressed it is fired individually, so your method wouldn't work.

0
you can use UserInputService:IsKeyDown(Enum.KeyCode.E) after checking if InputBegan keycode is E imKirda 4491 — 3y
Ad

Answer this question