How would I do UserInputService Keycode Combos

I am attempting to hide a gui when a player presses LeftControl + Z

However, I cannot figure out how I can execute this as I only know how to make one keycode work, and I cannot bind two keys to work together,

I have tried to look on the developer forum for this, but the method the other person uses seems a little complicated and would be weird to work with if I just tried to use Z by itself.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameprocess)
      if not gameprocess then
            if input.Keycode == Enum.Keycode.LeftControl and input.Keycode == Enum.Keycode.Z then
                  script.Parent.Visible = not script.Parent.Visible
            end
      end
end)

However this doesn’t work!

1 Like

You can use IsKeyDown, but you’ll have to do some extra work to make it work well (involving multiple key events).

In this case, I would have the event be for the Z key, then check if control is down as people normally press control first.

1 Like
local UserInputService = game:GetService("UserInputService")

local actualInputs, last, event = {}, {}, Instance.new("BoolValue")

function ischanged()
      if not(last == actualInputs) then
            last = actualInputs;
            event.Value = not(event.Value); -- changes
      end
end

function getkeycombo(...)
      local items = {...}
      for i=1,#items do
            if not(table.find(actualInputs,items[i])) then
                  return false
            end
      end
      return true
end

UserInputService.InputBegan:Connect(function(input, gameprocess)
      if not gameprocess then
            if not table.find(actualInputs,input.Keycode) then
                  table.insert(actualInputs,input.Keycode)
            end
      end
end)

UserInputService.InputEnded:Connect(function(input, gameprocess)
      if not gameprocess then
            if table.find(actualInputs,input.Keycode) then
                  table.remove(actualInputs,table.find(actualInputs,input.Keycode))
            end
      end
end)

coroutine.resume(coroutine.create(function()
      while task.wait() do
            ischanged()
      end
end))

event.Changed:Connect(function()
      local bool = getkeycombo(Enum.Keycode.LeftControl,Enum.Keycode.Z)
      print(bool)
end)

1 Like

I wouldn’t recommend using a loop for input. All of this can be detected via events alone.

1 Like

Ok, thanks so much. Although I think I have another method using variables. :+1:

I know you’ve already got it solved, but I just wrote this up:

local userInputService = game:GetService('UserInputService')

function inputBeganMulti(...)
	local input = {...}
	local events = {}
	local keys = {}
	local output = {}
	local execute
	function output:Connect(func)
		execute = func
	end
	function output:Disconnect()
		for _,event in pairs(events) do
			event:Disconnect()
		end
	end
	function output:Wait()
		while #keys ~= #input do
			task.wait()
		end
		output:Disconnect()
	end
	for _,key in pairs(input) do
		local event = userInputService.InputBegan:Connect(function(k,process)
			if not process and k.KeyCode == key then
				table.insert(keys,key)
				if #keys == #input then
					if execute then
						execute()
					end
				end
			end
		end)
		table.insert(events,event)
		event = userInputService.InputEnded:Connect(function(k,process)
			if not process and k.KeyCode == key then
				local n = table.find(keys,key)
				if n then
					table.remove(keys,n)
				end
			end
		end)
		table.insert(events,event)
	end
	return output
end

--Example:
inputBeganMulti(Enum.KeyCode.LeftControl,Enum.KeyCode.Z):Connect(function()
	print('hi')
end)

Supports Connect, Wait, and Disconnect. Should basically work the same as normal events.

There’s a much simpler solution for this which involves the use of GetKeysPressed(), it returns an array of ‘InputObjects’ that can be queried, as in the following.

local Game = game
local UserInputService = Game:GetService("UserInputService")

local function OnInputBegan(InputObject, WasProcessed)
	if WasProcessed then return end
	local Keys = {"LeftControl", "Z"} --Length of two.
	if not table.find(Keys, InputObject.KeyCode.Name) then return end --Preliminary check.
	local InputObjects = UserInputService:GetKeysPressed() --Get keys pressed.
	for _, InputObject in ipairs(InputObjects) do
		table.remove(Keys, table.find(Keys, InputObject.KeyCode.Name)) --Remove key from 'Keys' table if it is currently being pressed.
	end
	if #Keys > 0 then return end --Return out of the function if a key is not being pressed.
	print("Success!")
end

UserInputService.InputBegan:Connect(OnInputBegan)
2 Likes