2+ Keys = 1 Action (UIS, CAS)

Hello I would like to know how can I do an action when I press M and P for example, or P is another action and M as well, M+P = test, M = 1 et P = print(“helloworld”) for example.

I mean, how I do concrete action by pressing several keys at the same time or by combining the keys that are pressed (so we do the action of key 1 then key 2 etc;)

You can store all keys you’re currently pressing in a table and continue from there:

local pressedKeys = {}

local function registerKey(key)
	if not table.find(pressedKeys, key) then -- if key isn't already in the table
		table.insert(pressedKeys, key)
		print(pressedKeys)
	end

end
local function removeKey(key)
	local keyIndex = table.find(pressedKeys, key)
	if keyIndex then
		table.remove(pressedKeys, keyIndex)
	end
	print(pressedKeys)
end

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.E then
			registerKey(input.KeyCode)
		elseif input.KeyCode == Enum.KeyCode.R then
			registerKey(input.KeyCode)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.E then
			registerKey(input.KeyCode)
		elseif input.KeyCode == Enum.KeyCode.R then
			registerKey(input.KeyCode)
		end
	end
end)
2 Likes

What? Wait I will show you an example:

local UIS = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(key, isTyping)
    if key.KeyCode == Enum.KeyCode.W then -- W
        print("Hi")
    else if key.KeyCode == Enum.KeyCode.A then -- A
        print("Hello")
    else if key.KeyCode == Enum.KeyCode.W and key.KeyCode == Enum.KeyCode.A then -- W + A
        print("LOL")
    end
end)

please correct me if im wrong but i dont think you can get 2 keycodes at the same time by pressing each at the same time that way, because i think the function connects for each input separately in its own scope.

the reason why Giorgi311 used a table is so that he can dynamically keep track of the keys being pressed and released within it without overwriting anything if he doesnt need to, while allowing to check for multiple keys being pressed at the same time

Use UserInputService:IsKeyDown()
Documentation

Example:

local cas = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")

cas:BindAction("Print", function()
    if uis:IsKeyDown(Enum.KeyCode.P) and uis:IsKeyDown(Enum.KeyCode.M) then --M and P are pressed
        print("test")
    elseif uis:IsKeyDown(Enum.KeyCode.P) and not uis:IsKeyDown(Enum.KeyCode.M) then --just P is pressed
        print("helloworld")
    elseif uis:IsKeyDown(Enum.KeyCode.M) and not uis:IsKeyDown(Enum.KeyCode.P) then --just M is pressed then
        print(1)
    else
        print("???")
    end
end, false, Enum.KeyCode.M, Enum.KeyCode.P)
1 Like

Ok, I’ve modified the previous code and it now detects whether I press both E and R or only E:

local pressedKeys = {}

local function registerKey(key)
	if not pressedKeys[key] then-- if key isn't already in the table
		pressedKeys[key] = true
	end
	print(pressedKeys)
end
local function removeKey(key)
	pressedKeys[key] = nil
	print(pressedKeys)

end

local function clearKeys()
	for key, v in pairs(pressedKeys) do
		pressedKeys[key] = nil
	end
end
local function action() -- all actions here
	if pressedKeys["E"] and pressedKeys["R"] then
		print("pressed both keys")
		clearKeys()
	elseif pressedKeys["E"] then
		print("pressed only 1 key")
		clearKeys()
	end
end	


local waitTime = 0.05 -- small delay before checking actions because more keys could be pressed
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.E then
			registerKey("E")
		elseif input.KeyCode == Enum.KeyCode.R then
			registerKey("R")
		end
		task.wait(waitTime)
		action()
	end
end)

UserInputService.InputEnded:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.E then
			removeKey("E")
		elseif input.KeyCode == Enum.KeyCode.R then
			removeKey("R")
		end
	end
end)
2 Likes

Yeah okay but when I do the keys at the same time or :

  • P and M after
  • M and P after

It does not work :(.

19:25:12.165 :arrow_forward: 1 (x2) - Client - LocalScript:10
19:25:19.207 helloworld - Client - LocalScript:8
19:25:20.423 1 - Client - LocalScript:10
19:25:21.107 helloworld - Client - LocalScript:8
19:25:21.590 1 - Client - LocalScript:10
19:25:21.840 helloworld - Client - LocalScript:8
19:25:22.190 1 - Client - LocalScript:10
19:25:22.390 helloworld - Client - LocalScript:8
19:25:23.606 1 - Client - LocalScript:10
19:25:23.923 helloworld - Client - LocalScript:8
19:25:24.256 1 - Client - LocalScript:10
19:25:24.673 helloworld - Client - LocalScript:8
19:25:24.941 1 - Client - LocalScript:10

1 Like

Have you tested the new code I gave you?

Yeah it works, thank you for your help :slight_smile: !

1 Like

No problem! And oops there was a slight error in the function removeKey(): I set it to false instead of nil. I’ve updated the code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.