How do I know when the player presses CTRL+INSERT_KEY_HERE

How do i know when the player presses 2 keys for example:

CTRL + YOUR_KEY_HERE like CTRL + K

i had tried this code

if input.KeyCode == Enum.KeyCode.LeftControl and input.KeyCode == Enum.KeyCode.L then
   -- it wont work though
end

it still doesn’t work and idk how

An example would help me very much

You want to use GetKeysPressed to create key combos.

Example:

local UIS = game:GetService("UserInputService")

local function keyCombo(key, GPE)
	local keysPressed = UIS:GetKeysPressed()
		
	local space, shift = false, false

	for _, key in ipairs(keysPressed) do
		if (key.KeyCode == Enum.KeyCode.Space) then
			space = true
		elseif (key.KeyCode == Enum.KeyCode.LeftShift) then
			shift = true
		end
	end

	if space and shift then
        -- Do Something
	end 
end

UIS.InputBegan:Connect(keyCombo)
3 Likes

but this code that powers everything wont work can you describe the input as both of the keys that are pressed

SKILLS_EVENT:FireServer(input.KeyCode, Mouse.Hit.Position)

I need more context in order to properly answer your question. Could you provide the scripts?

receiver:

The Server

if InputKey == Enum.KeyCode.LeftControl and InputKey == Enum.KeyCode.L then

Sender:

The Client

local function KeyComboForLightning()
	local KeysPressed = UIS:GetKeysPressed()
	
	local CTRL, L = false, false
	
	for _, Keys in pairs(KeysPressed) do
		if Keys.KeyCode == Enum.KeyCode.LeftControl then
			CTRL = true
		elseif Keys.KeyCode == Enum.KeyCode.L then
			L = true
		end
	end
	
	if CTRL and L then
		SKILLS_EVENT:FireServer(KeysPressed, Mouse.Hit.Position)
	end
end

What do you mean? Copy the code from the Connect part of the RemoteEvent.

that’s an code snippet from my server-sided code

eh? There is no Event:Connect(...) part

SKILLS_EVENT.OnServerEvent:Connect(function(Player, InputKey, MousePos)
	if Player then
		-- if statements for input key
		if InputKey == Enum.KeyCode.T then
			if Player.Character.LeftUpperArm.Transparency == 0 then
				SkillsModule.Invisibility(Player, Player.Character, 1)
			elseif Player.Character.LeftUpperArm.Transparency == 1 then
				SkillsModule.Invisibility(Player, Player.Character, 0)
			end
		elseif InputKey == Enum.KeyCode.V then
			SkillsModule.Teleport(Player, Player.Character, MousePos)
		elseif InputKey == Enum.KeyCode.Space then
			SkillsModule.Fly(Player, Player.Character)
		elseif InputKey == Enum.KeyCode.L then
			PlayerDebounces[Player.Name] = true
			
			if PlayerDebounces[Player.Name] == false then
				PlayerDebounces[Player.Name] = true
				ElementalModule.Lightning(Player, MousePos)
				wait(1)
				PlayerDebounces[Player.Name] = false
			end
		end
	end
end)

You will want to check if Left Control is down using UserInputService:IsKeyDown. By the way the server should never (in)directly handle user input, so checking for key code on the server is unnecessary.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, engine_processed)
    if engine_processed then
        return
    end

    if UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then -- the key is still down while they pressed a new key
        if input.KeyCode == Enum.KeyCode.K then
            -- remote:FireServer()
        end
    end
end)

that’s for a single key not 2 combined keys i wanted 2 keys that have a similar system to this

if input.KeyCode.LeftControl and input.KeyCode == Enum.KeyCode.K then

as of @frriend if that actually works then how would i do it as a single argument like input

Which is what mine does. UserInputService:IsKeyDown returns true if the key code passed is currently pressed. A key press would fire InputBegan so it’s a new input, and you are checking if the key is pressed.

2 Likes