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

How do you detect a key being pressed?

Asked by 8 years ago

It's really dumb. But I never used it before.

I need to know how to detect a key being pressed, not just DOWN. Not like MouseButton1Down, more of a MouseButtoon1Click. Get me?

Thanx.

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

There are two ways you can go about doing this.. Either use KeyDown and KeyUp or, preferably, use the InputBegan and InputEnded events of UserInputService. The latter is preferable because you can detect any input from the keyboard while KeyDown/KeyUp is limited.


The InputBegan event returns two values: The input, and whether or not the input was a game processed event such as chatting or the i or o keys, etc.. So to prevent the event from firing while chatting you check if the second returned value is false before proceeding with code.

Credit to M39a9am3R for pointing out an error in my code

--Define UserInputService
local uis = game:GetService('UserInputService')

--InputBegan
uis.InputBegan:connect(function(input,process)
    --Make sure it's not a game process
    if not process then
        --Check key 'E'
        if input.KeyCode == Enum.KeyCode.E then
            print('E pressed')
        end
    end
end)
Ad
Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

To detect if a key was pressed, there are two ways of going about this. Either InputBegan (UserInputService) or KeyDown (GetMouse).


KeyDown

KeyDown is a basic way, yet it has become deprecated after years of use. This method also will not work with some keys such as /, i, o or other ROBLOX used keys. To begin, you will want to get the player's mouse. Keep in mind that these scripts will need to be in a local script or they will not work.

--Since we are in a localscript, we can use the LocalPlayer property or Players Service.
Mouse = game.Players.LocalPlayer:GetMouse() -- We have hold of the player, since the GetMouse function will only work on a Player object.

Now, when a key is pressed, we will want it to fire an event. We'll need to set up the KeyDown event.

Mouse = game.Players.LocalPlayer:GetMouse()

Mouse.KeyDown:connect(function(key) --We have set up the event and function, whenever the player hits a key, it will be under the value 'key'.

    if key:lower() == 'a' then --If the player hit's the key a, then the if then statement will run.
        print('Player has used the button A.')
    end

end)

You can even get the byte code of a key, say if I wanted to use the shift key I would need to find the byte code of it.

Mouse = game.Players.LocalPlayer:GetMouse()
Mouse.KeyDown:connect(function(key)
    print(key:byte()) --This will give you the byte code of any button on the keyboard.
end)

InputBegan

This is the more up-to-date version, it will be using the service UserInputService. The UserInputService service works on multiple platforms rather than be restricted to the computer, but for this example it still will. Just a warning, if you try to chat with this InputBegan method, the event will still fire but I will show a way around this.

game:GetService('UserInputService').InputBegan(function(InputObject, Processed) --InputObject is kind of a table of objects which are Delta, KeyCode, Position, UserInputState, and UserInputType.
--We will want to use KeyCode since that provides us our key.

if InputObject.KeyCode == Enum.KeyCode.A then --If the KeyCode of InputObject is A then run the statement.
    print('A was Pressed.')
end

end)

Like I said, since UserInputService will work when you are typing, there is a way around this. If you are focused on a textbox, then you can have the InputBegan function if then statement ignore the action. You would want to use the TextBoxFocused and TextBoxFocusReleased events.

textBoxIsFocused = false
game:GetService('UserInputService').InputBegan(function(InputObject, Processed)

if InputObject.KeyCode == Enum.KeyCode.A and not textBoxIsFocused then --The 'not' will negate the value of textBoxIsFocused, which means if textBoxIsFocused is true, then the if then statement will read it as false, and vice versa.
    print('A was Pressed.')
end

end)

--We will need to set up the TextBoxFocused Event.

game:GetService('UserInputService').TextBoxFocused:connect(function() --We don't care if anything goes in there, right?
textBoxIsFocused = true --So now, the if then statement should not run.
--We will want to set up a wait function on the TextBoxFocusReleased event (I learned this technique from adark I believe a while ago).

game:GetService('UserInputService').TextBoxFocusReleased:wait() --Instead of tagging on a connection line there, we just tagged on a wait function. Since the TextBox focus is being released.

textBoxIsFocused = false --Now the if then statement should run.
end

Hopefully this helped, if you have any questions post it in the comments and I will try to get back to it. If this answered your question do not forget to hit the upvote button and click accept answer.

Answer this question