Pressing two keys/ buttons to fire a function

I’ve been wondering for a while now(note that I haven’t tried) how does one fire a function only if two buttons are pressed at the same time. I’m looking more for like an example(client sided only is fine, I’ll implement it into my frame work)

12 Likes

Something like this could work.

Basically you just check UserInputService and listen for whatever 2 keys you want, and if they’re pressed…Then you do stuff.

Hope this helps.

Edit: Refer to @suremark 's reply, or better yet @qqtt991, he shows some nice code.

Also I learned something new today.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(InputObject, Processed)
    if InputObject.KeyCode == Enum.KeyCode.E  and InputObject.KeyCode == Enum.KeyCode.Q then
        print("You pressed two keys at once")
        -- Do stuff here
    end
end)
3 Likes

Unfortunately that wouldn’t work. A single InputObject can’t have two equivalent KeyCodes—it will only have one unique KeyCode.

To get around this, you could store the state of those keys in outside variables (for example, as Boolean values stored in variables named Q_Down and E_Down). Then, each time the player presses one of those keys, update the variables and check if both are down.

Ah. Yeah my code was untested. Should’ve stated that. :confused:

When one of the two keys are pressed, you should check if the other key is also being pressed at that time. Note that you should check input.Keycode at this time, and just not check IsKeyDown on both keys, because otherwise whenever InputBegan fires, if both keys are held down, it would run the code again despite the user not re-pressing the key combination.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if (input.KeyCode == Enum.KeyCode.E and UIS:IsKeyDown(Enum.KeyCode.R)) or (input.KeyCode == Enum.KeyCode.R and UIS:IsKeyDown(Enum.KeyCode.E)) then
		print("Wow")
	end
end)
87 Likes

thank you, here is a solution for a reward.

4 Likes

I’m just realizing this because I forgot myself but there needs to be and not gameProcessedEvent as well so it doesn’t work while a player is chatting.

Just add that in?

Obviously lol.

1 Like

Whouldnt that make it so if you hold ‘E’ and you press ‘R’ it whould still function? Cause i wanted my script to detect if they press it at the actual same time (like they have to time it) so cant i just replace ‘IsKeyDown’ with the normal one? Or whould it not work??

IDK. Add wait() after press first button into it?
this should do the trick

no you can just press both at the same time and then roblox will detect it

but well is more simple to use keyisdown with keycode instead

You could add a line of code using the tick() to detect if both have been pressed in a certain time

4 Likes

Gonna post what worked for me in determining if 1 of the two buttons were pressed or if two of the buttons were pressed:

local UIS = game:GetService("UserInputService")
local aKey = false
local dKey = false
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputState == Enum.UserInputState.Begin then
		if (input.KeyCode == Enum.KeyCode.A) or UIS:IsKeyDown(Enum.KeyCode.A)  then
			aKey = true
			task.wait()
			if (input.KeyCode == Enum.KeyCode.D) or UIS:IsKeyDown(Enum.KeyCode.D)  then
				dKey = true
			end
		elseif (input.KeyCode == Enum.KeyCode.D) or UIS:IsKeyDown(Enum.KeyCode.D)  then
			dKey = true
			task.wait()
			if (input.KeyCode == Enum.KeyCode.A) or UIS:IsKeyDown(Enum.KeyCode.A) then
				aKey = true
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEven)
	if input.UserInputState == Enum.UserInputState.End then
		if aKey == true and dKey == true then
			print ("Both")
			aKey = false dKey = false
		elseif aKey == true and dKey == false then
			print ("A")
			aKey = false dKey = false
		elseif aKey == false and dKey == true then
			print ("D")
			aKey = false dKey = false
		end
	end
end)

This has already been stated above, ignore the first post, I wasn’t looking at UIS:IsKeyDown().