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

What events can I use for a key press down and up?

Asked by 7 years ago

Kind of like this...

mouse.Button1Down:Connect(function()

end)

...but for a key?

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Hey alonzo12345,

The most common event that was used a lot was the mouse's KeyDown event for detecting when a key is pressed however, that event is not deprecated and there's an even better method of detecting keys. Let me introduce you to UserInputService. This is a service that you can use for input, as the name might suggest. It's I believe about the most efficient and easiest way of getting input from the keyboard at the moment. This Service is very easy to use so, just bare with me here. Here is the wiki page for it and all of it's methods/events/properties. Below is a personal example with some brief explanations of it:

local uis = game:GetService("UserInputService") -- Declares a variable for the UserInputService.

uis.InputBegan:Connect(function(obj, gp) -- Anonymous function with a parameter of obj and gp and the event 'InputBegan' is triggered when any key on the keyboard is pressed.
    if obj.KeyCode = Enum.KeyCode.Q and not gp then -- obj is the key that is pressed and gp is a boolean value to check if it's meant to be pressed or not. gp is basically used to see if the user was typing in chat while he/she pressed the key or if the key was intentionally pressed without typing in chat. The if statement will not let the rest of the code run if 'Q' was pressed while chatting in this scenario.
        print("Q was pressed.") -- Simply prints 'Q was pressed.' when it is pressed.
end) -- end for the anonymous function. Make sure that parenthesis at the end is there because, that's how you end anonymous functions.

For Key Up, you can use the InputEnded event of UserInputService. The parameters are the same and I am pretty sure it works the same way as the one above.

Well, I hope I some-what helped you out with your issue and the methods I used above will be linked below.

Here is the link for the :GetService() page.

Here is the link for the Anonymous functions page.

Here is the link for normal functions.

Here is the link for UserInputService being used.

~~ KingLoneCat

0
Uinps is bad use mouse.KeyDown:connect(function(key) if key=="q" then --dostuff end end) uinps will also activate when typing so you need more denounces for it also you can use KeyUp instead of KeyDown coolallball 0 — 7y
Ad

Answer this question