Getting Left/Right arrow key inputs

  1. A way to have something happen when the left/right arrow key is pressed

  2. The camera moves and not anything with input happens

  3. I have not found any solutions.

Here is my current Code:

local USI = game:GetService("UserInputService")

local UpPressed = false
local DownPressed = false
local LeftPressed = false

local function GetCool(Input)
	if Input.KeyCode == Enum.KeyCode.Up then
		UpPressed = true
		print("Up Pressed!")
	elseif Input.KeyCode ~= Enum.KeyCode.Down then
		UpPressed = false
		print("Up Not Pressed!")
	end
				
	if Input.KeyCode == Enum.KeyCode.Down and UpPressed == true then
		DownPressed = true
		print("Down Pressed!")
	elseif Input.KeyCode ~= Enum.KeyCode.Left then
		DownPressed = false
		print("Down Not Pressed!")
	end
				
	if Input.KeyCode == Enum.KeyCode.Left and DownPressed == true then
		LeftPressed = true
		print("Left Pressed!")
	elseif Input.KeyCode ~= Enum.KeyCode.Right then
		LeftPressed = false
		print("Right Not Pressed!")
	end
				
	if Input.KeyCode == Enum.KeyCode.Right and LeftPressed == true then
		UpPressed = false
		DownPressed = false
		LeftPressed = false
		print("Right Pressed!")
	end
end

USI.InputEnded:connect(function(Input)
	GetCool(Input)
end)

Any help will be appreciated!
This is for a secret for my upcoming game not yet released!

2 Likes

I think you’ll also need to add an InputBegan connection like you did with InputEnded to make it run when the user presses the key down, not just when they let go of it.

1 Like

You can try it

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProccessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
 if input.KeyCode == Enum.KeyCode.Up then
		--function
	end
end
end)

I have provided the script and I am intending for it to happen when the key is lifted and the Up/Down arrows work but I am only having issues with Left/Right keys

I am intending for it to happen when the key is lifted and I am only having issues with Left/Right keys the Up/Down keys work

Try this.

local USI = game:GetService("UserInputService")

local UpPressed = false
local DownPressed = false
local LeftPressed = false
local RightPressed = false

local function GetCool(Input)
if Input.UserInputType == Enum.UserInputType.Keyboard then
	if Input.KeyCode == Enum.KeyCode.Up then
		UpPressed = true
		print("Up Pressed!")
	elseif Input.KeyCode ~= Enum.KeyCode.Down then
		UpPressed = false
		print("Up Not Pressed!")
	end
				
	if Input.KeyCode == Enum.KeyCode.Down then
		DownPressed = true
		print("Down Pressed!")
	elseif Input.KeyCode ~= Enum.KeyCode.Up then
		DownPressed = false
		print("Down Not Pressed!")
	end
				
	if Input.KeyCode == Enum.KeyCode.Left then
		LeftPressed = true
		print("Left Pressed!")
	elseif Input.KeyCode ~= Enum.KeyCode.Right then
		LeftPressed = false
		print("Left Not Pressed!")
	end
	
	if Input.KeyCode == Enum.KeyCode.Right then
		RightPressed = true
		print("Right Pressed!")
	elseif Input.KeyCode ~= Enum.KeyCode.Left then
		RightPressed = false
		print("Right Not Pressed!")
	end		
end
end

USI.InputEnded:connect(function(Input)
GetCool(Input)
end)

Nope, doesn’t work.
Roblox seems to overwrite the Left/Right arrow key inputs with moving the camera.

1 Like

That must work. Cuz its working on me. Maybe its about my language.I might have misunderstood.

Your issue about “Right/Left Arrow Keys” this 2 key not printing when pressed im true?

I am making it so you have to press “Up > Down > Left > Right” then something happens if no other key has been press in between that process

And no, its not printing when the Left/Right key is lifted/pressed.
That is the only thing stopping me from making my idea possible.

(The printing is for Debugging before I do the main thing)

2 Likes

Hm. I have a new solution maybe this work…

local USI = game:GetService("UserInputService")

local UpPressed = true
local DownPressed = true
local LeftPressed = true
local RightPressed = true

local function GetCool(Input)
if Input.UserInputType == Enum.UserInputType.Keyboard then--and Input.KeyCode == Enum.KeyCode.Up or Input.KeyCode == Enum.KeyCode.Down or Input.KeyCode == Enum.KeyCode.Right or Input.KeyCode == Enum.KeyCode.Left then
	if Input.KeyCode == Enum.KeyCode.Up then
		UpPressed = true
		print("Up Pressed!")
	elseif Input.KeyCode == Enum.KeyCode.Down then
		UpPressed = false
		print("Up Not Pressed!")
	end
				
	if Input.KeyCode == Enum.KeyCode.Down then
		DownPressed = true
		print("Down Pressed!")
	elseif Input.KeyCode == Enum.KeyCode.Up then
		DownPressed = false
		print("Down Not Pressed!")
	end
				
	if Input.KeyCode == Enum.KeyCode.Left then
		LeftPressed = true
		print("Left Pressed!")
	elseif Input.KeyCode == Enum.KeyCode.Right then
		LeftPressed = false
		print("Left Not Pressed!")
	end
	
	if Input.KeyCode == Enum.KeyCode.Right then
	        RightPressed = true
		print("Right Pressed!")
	elseif Input.KeyCode == Enum.KeyCode.Left then
		RightPressed = false
		print("Right Not Pressed!")
	end
end
end

USI.InputEnded:connect(function(Input)
GetCool(Input)
end)

You could use ContextActionService and unbind the arrow keys.

local CAS = game:GetService('ContextActionService')

CAS:UnbindAction('forwardMovement') -- this is the forward arrow

Here is the source

Github link

Edit: You could also change the priority of the arrow keys using these 2 links

BindActionAtPriority

BindAction

1 Like

Thanks for these methods!
I am trying them out now.

Unless I am doing something wrong, none of these methods worked for me.
Thank you for the help anyway!

1 Like

This would not work since I am forcing the player to press “Up > Down > Left > Right” since these have no checks for if the keys are pressed in the correct order.

The Left and Right Arrow Keys are grabbed by the camera and uses UserInputService as well,
ie. They aren’t bound with ContextActionService.
But there is a flag that can prevent the camera from grabbing the left and right arrows for panning.
You’ll have to grab the activeCameraController and then set its .keyPanEnabled to false.
(Something to research, or maybe someone else will take the time to explain it more.)

3 Likes

Okay, thank you for this!
I will look into doing this method.

Thank you so much!
This ended up working!
Take a Solution!

Hey i know this topic conversation was inactive for a while but, i have the same problem and i can’t find any other topics about how to get activeCameraController, can you tell the way you made your code run successfully or link the topic you have viewed ?

2 Likes

There seems to be no documentation at all about activeCameraController and .keyPanEnabled … could you explain how you fixed this issue?? This is extremely frustrating as I can’t find any help online