Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Track Multiple Key Presses?

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

✓ Accepted answer
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.

1 comment

0

you can use UserInputService:IsKeyDown(Enum.KeyCode.E) after checking if InputBegan keycode is E

This discussion is preserved as it was. New answers, comments, and votes are closed.