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)
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
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)
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)