How do I make a keybind?

  1. What do you want to achieve?
    I want to make a system that uses keybinds like Ctrl + D.

  2. What is the issue?
    I can’t figure out how to.

  3. What solutions have you tried so far?
    There was none.

Well, let’s see. What are you trying to make Ctrl + D Do exactly?

You could just use the input service and make a if statement with and in between the two key-presses.

1 Like

You’re going to want to access the UserInputService and use a InputBegan or InputEnded signal. Then, you’re going to want to set up variables and adjust the values according to what key was pressed or released.

Begin with whatever you so chose, I would start with the signals:

Also, please read each step so you can understand the process.

Step 1
local userInputService = game:GetService("UserInputService")

--Processed will be true if the player is typing or the key is binded to something via `ContextService` so we check to make sure its false!

userInputService.InputBegan:Connect(function(key, processed)

end)

userInputService.InputEnded:Connect(function(key, processed)

end)

Then, proceed to add you’re variables and conditions, approach this as you wish (just make sure you have the corresponding variables to the keys you wish to bind together):

Step 2
local userInputService = game:GetService("UserInputService")

local keysPressed = {}

userInputService.InputBegan:Connect(function(key, processed) --Key Down
	if key.KeyCode == Enum.KeyCode.D and not processed and not keysPressed["D"] then
		keysPressed["D"] = true
	elseif key.KeyCode == Enum.KeyCode.LeftControl and not processed and not keysPressed["LeftControl"] then
		keysPressed["LeftControl"] = true
	end
end)

userInputService.InputEnded:Connect(function(key, processed) --Key Up
	if key.KeyCode == Enum.KeyCode.D and not processed then
		keysPressed["D"] = false
	elseif key.KeyCode == Enum.KeyCode.LeftControl and not processed then
		keysPressed["LeftControl"] = false
	end
end)

Finally, we need a way to check if both key variables are true! Approach this how you wish, I will do the following:

Step 3
local userInputService = game:GetService("UserInputService")

local keysPressed = {}

local function checkPressedKeys() -- This function just checks if key values are true 
	if keysPressed["D"] and keysPressed["LeftControl"] then
		warn("Both keys are down!") --Your code goes here
	end
end

userInputService.InputBegan:Connect(function(key, processed) --Key Down
	if key.KeyCode == Enum.KeyCode.D and not processed and not keysPressed["D"] then
		keysPressed["D"] = true
	elseif key.KeyCode == Enum.KeyCode.LeftControl and not processed and not keysPressed["LeftControl"] then
		keysPressed["LeftControl"] = true
	end
	checkPressedKeys() -- Call the function that checks the keys
end)

userInputService.InputEnded:Connect(function(key, processed) --Key Up
	if key.KeyCode == Enum.KeyCode.D and not processed then
		keysPressed["D"] = false
	elseif key.KeyCode == Enum.KeyCode.LeftControl and not processed then
		keysPressed["LeftControl"] = false
	end
end)

Hope this explains how to approach this!

Also, for hyper optimzation you could store the KeyCodes in their own tables and check them from there. That way you can easily store multiple shortcuts without having a huge wall of if statements.

2 Likes

I am guessing he his trying to make it duplicate something, just like how you duplicate a part in Roblox Studio.

1 Like

make a if statement of ctrl then make a if statement of d, then do ur thing