UserInputService Input "LeftShift" and "LeftControl" always return false

Simple,

this was my example code (tried to make some simple run script)

--RUNNING SCRIPT--
-------------------------------

local RunSpeed = 18
local NormalSpeed = 12
local SlowSpeed = 10

-------------------------------

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, GameProcessedEvent)
	print(Input.KeyCode)
	--if not GameProcessedEvent then
		if Input == Enum.KeyCode.LeftShift then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = RunSpeed
		elseif Input == Enum.KeyCode.LeftControl then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = SlowSpeed
		end
	--end
end)

UIS.InputEnded:Connect(function(Input, GameProcessedEvent)
	if Input == Enum.KeyCode.LeftShift or Input == Enum.KeyCode.LeftControl then
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = NormalSpeed
	end
end)

this print(Input.KeyCode) prints “Enum.KeyCode.LeftShift”

however, the bug is that when you try to make an if statement or print this print(Input == Enum.KeyCode.LeftShift) it always returns false.

2 Likes

Try using Input.KeyCode == Enum.KeyCode.LeftShift instead. The “Input” variable passed by Input-related events is an InputObject, not a KeyCode.

4 Likes

Woops, seems like it was issue with my code.

I was misled by this Bug with Enum.KeyCode.LeftShift for UserInputService InputBegan

I’m confused.

UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		local keyPressed = input.KeyCode
		print(keyPressed)
		if keyPressed == Enum.KeyCode.LeftShift then
			print('THIS MSG NEVER PRINTS!')
		end
	end
end)

What’s wrong w/ the code?

Solved by @realhigbead - look what he wrote.

I did and I still don’t see what I’m doing wrong :confused:

its “keyPressed.KeyCode” not “keyPressed”

The “gameProcessed” variable is set to true if you’re using shift lock switch. Your code works with shift lock switch disabled.

image

2 Likes

I’m still confused
So it’s like this?

UserInputService.InputBegan:Connect(function(input, gameProcessed)
		if input.KeyCode.KeyCode== Enum.KeyCode.LeftShift then
			print('MSG!')
		end
end)

that doesn’t seem right

That makes a lot more sense.
Thank you.

Also, how would you detect someone pressing LeftShift while they have ShiftLock enabled?

you white


if input.KeyCode.KeyCode== Enum.KeyCode.LeftShift then

instead of


if input.KeyCode== Enum.KeyCode.LeftShift then

Check if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter.

Wouldn’t that falsely trigger if the player zooms in 100% into first person view?

Probably, you should test it first as I can’t right now. I will look if there is some property you can use to read the zoom distance

The Input events are still fired if you have ShiftLock enabled, but the gameProcessed will be set to true. Since your code only continues if gameProcessed is false, you get your issue.

The simplest solution is just to remove the if gameProcessed then from the beginning of your code.

1 Like

Then how do you tell if someone presses left shift while they are not typing inside a TextBox? Is there any clean way to do that?

Adding on, game processed will be true if

  • typing
  • clicked on a UI button

So i don’t think the last one will be a problem, so just check that they aren’t in a text box.

if not UserInputService:GetFocusedTextBox() then

3 Likes

Oh I think what I was trying to point out in this post was how UserInputService.InputBegan worked differently than UserInputService.InputEnded

I don’t think that post I made a few months ago was all that clear and I could see why it’s confusing. Is this worth making a new bug report about?

  • yes
  • no

0 voters

This also isn’t a bug. Shift lock switch only uses InputBegan when toggling, so it’d make sense that gameProcessed would be false for InputEnded.

(This is going a little offtopic, so you may want to create a new topic if you keep having issues with this)

1 Like