Creating a multi-key keybind

Hello!

I’m currently working on revamping my ban system/administrator panel. To open it, I want to have a keybind that requires 2 or 3 keys to be pressed. I tried looking up keybinds for lua, and I only found single-key keybinds, not multi-key, which I already know how to do. Where do I start with this? The closest thing I’ve found was this, however I don’t know how to make it apply to my specific scenario.

2 Likes

You would really just keep track of the keys which are being held down.

Example:

local UIS = game:GetService("UserInputService")
local AltDown = false

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end -- if you're typing in a e.g. a textbox, the next lines will be ignored

	if input.KeyCode == Enum.KeyCode.LeftAlt then
		AltDown = true
	elseif input.KeyCode == Enum.KeyCode.D then
		if not AltDown then return end -- if alt is not being held down, then don't continue
		MyFunction() -- fire the function which does what you want it to do
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		AltDown = false
	end
end)

Extra: a tip I can give you for KeyCodes is to type “Enum.KeyCode.” then a bunch of KeyCodes will appear in a list and you can then use the arrows to scroll through them to find the KeyCode that you want if you don’t remember it.

6 Likes

You Could have an If statement ReQuire Both Keys to be true:

local UIS = game:GetService("UserInputService")

local Key1 = --the first key--
local Key2 = --the sceond key--

if Key1 == true and Key2 == true then
     ---Do Script or function
else
     Key1 = false
     Key2 = false
end


UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.One then             ------------"1" button
		Key1 = True
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Two then            -------------"2" button
		Key2 = True
	end
end)

1 Like

You could use UserInputService:IsKeyDown to check whether the keys are down
Example(using Alt + M):

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)

if (input.KeyCode == Enum.KeyCode.M and UIS:IsKeyDown(Enum.KeyCode.Alt)) or (input.KeyCode == Enum.KeyCode.Alt and UIS:IsKeyDown(Enum.KeyCode.M)) then

-- run function

end

end)

It is a little clunky but it works.

However it is sometimes recommended to use ContextActionService.

6 Likes