Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to detect if a player presses a key? [Answered!]

Hi. How would a script detect if a player has pressed a key on their keyboard? Is there a set function for this?

Thanks!

1 comment

0

DANG!!!! AWESOME IT WORKS!!!!

4 answers

✓ Accepted answer

Easy, first you can get a local script because of the Local Player(MUCH MUCH MUCH MUCH Easier that way, like 500x easier) then we have to get the mouse using :GetMouse() to get the players mouse. The player's mouse has an event called the KeyDown event. This event fires when a player presses a key. The parameters can represent the key you have pressed. We can find out what key we pressed by using an if statement. You can find out a key the player presses by using either string.byte or, because the key is a string, we can see what key it is that way.


Get player from a local script.

local player = game.Players.LocalPlayer --simple

Get mouse from player

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --Also pretty simple.

Use the keydown event:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function()
	print("Pressed a key.")
end)

Get the key the player has pressed:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	print("Pressed: "..key)
end)

Find out what key the player has pressed:

There are multiple ways of doing this.

1.) The string.byte way:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key:byte() == 32 then --Since you cannot type [[key == "Space"]] just use byte.
		print("Pressed space")
	end
end)

2.) Regular way

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "e" then
		print("Pressed e")
	end
end)

3.) And the Regular way plus string.lower() or string.upper()

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key:lower() == "e" or key:upper() == "E" then
		print("Pressed e or E")
	end
end)


KeyDown is not recommended to be used, use Keyboard input instead.

To use this it's easy,(Can be in normal script in workspace or something.)

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then --Also, could be written as [[inputObject.KeyCode == "R"]]
		print("R was pressed")
	end 
end)

Just use the userimputservice which also has other cool features like an event fires if someone on a touchscreen device pinches the screen it fires.



If you are using keydown still and is wanting to know how do I know that 32 is space? I got it off of this roblox wiki page. Look under the "DEC" column. http://wiki.roblox.com/index.php?title=File:Ascii_Table.png


Hope this helps!

5 comments

0

Thanks so much! I wish I could upvote more than once!

0

it didnt work, maybe i did something wrong...

0

Keyboard input not work. it work in local script only

0

This has also helped me, thank you!

0

how do you mkae it so it presses down SHIFT

Hello, like what above said, you should use the service, UserInputService instead of keyDown.

Here's an example script:

game:service'UserInputService'.InputBegan:connect(function(inputObject, gameProcessedEvent)
if (inputObject.KeyCode=='W') then
print'Hi.';
end;
end);

What this would do, exactly, is to check if you pressed the key 'W' on your keyboard, and if you did, it would print the text "Hi."

2 comments

1

Tab your script correctly.

2

Um. Sorry, it's just my scripting style.

Actually, there's a better way to do this than GetMouse because it allows you to choose when the key can fire and when it can't. It's called Context Action Service, you can learn to use it here. For example, if you were making a tool and you want to be able to press f to fly you can make that and make it so it only works when the tool is equipped. AND, you can add a button for people on mobile devices to use since they don't have keyboards.

In localscript

local plr = game.Players.LocalPlayer  -- Player where the script is running
local mouse = plr:GetMouse()  -- Their mouse

mouse.KeyDown(function(w)
	-- do stuff
end)

This should work

NOTE: it appears KeyDown is deprecated in favor of ContextActionService Also i'm a little rusty on my Lua

6 comments

0

I thumbs you down since you didn't have a very good explanation about how to KeyDown event works. BTW keydown is not recommended to be used. Use the UserInputService instead.

0

Why did someone up vote this? Has no explanation, little to no work, doesn't explain how it works, or about how LocalPlayer only works in LocalScript. This is the reason I thumbs down.

0

This also looks like you took it straight out of the wiki.

0

"NOTE: it appears KeyDown is deprecated in favor of ContextActionService Also i'm a little rusty on my Lua"

0

I havent scripted in awhile, so have not yet moved to UserInputService/ContextActionService and don't know enough about it. Keydown would suffice for now.  I answered the question, which was about getting  how to get input. I provided a working example of how to get it.  I assumed they would know what localPlayer is from context(i also specified this was in a localscript) and getting user input is

0

getting user input is fairly above beginner

This discussion is preserved as it was. New answers, comments, and votes are closed.