Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
5

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

Asked by 9 years ago

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

Thanks!

0
DANG!!!! AWESOME IT WORKS!!!! Noob_10365 0 — 5y

Locked by WideSteal321, aquathorn321, and royaltoe

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

4 answers

Log in to vote
11
Answered by 9 years ago

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!

0
Thanks so much! I wish I could upvote more than once! DrCylonide 158 — 9y
0
it didnt work, maybe i did something wrong... CommanderCaubunsia 126 — 5y
0
Keyboard input not work. it work in local script only OnaKat 444 — 5y
0
This has also helped me, thank you! GodlyEricGamer1800 -100 — 5y
0
how do you mkae it so it presses down SHIFT Vortex_Vasne 89 — 4y
Ad
Log in to vote
0
Answered by 9 years ago

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."

1
Tab your script correctly. EzraNehemiah_TF2 3552 — 9y
2
Um. Sorry, it's just my scripting style. detaching 8 — 9y
Log in to vote
0
Answered by
yoshiegg6 176
9 years ago

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.

Log in to vote
-1
Answered by
iaz3 190
9 years ago

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

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. EzraNehemiah_TF2 3552 — 9y
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. EzraNehemiah_TF2 3552 — 9y
0
This also looks like you took it straight out of the wiki. EzraNehemiah_TF2 3552 — 9y
0
"NOTE: it appears KeyDown is deprecated in favor of ContextActionService Also i'm a little rusty on my Lua" iaz3 190 — 9y
View all comments (2 more)
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 iaz3 190 — 9y
0
getting user input is fairly above beginner iaz3 190 — 9y