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

Keyboard input detection for arrow keys?

Asked by 6 years ago

When detecting keyboard inputs i usually use scripts similar to the small example below:

function PressQ(key)
    if key == "q"
        then...
end



mouse.KeyDown:connect(PressQ)

but I'm wondering what I should replace the "q" on the second line with to detect the down arrow being pressed? I've only ever used letters...

Thank you for your time! :)

1 answer

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

KeyDown is deprecated and will no longer work, we now have to use userinputservice.

(Userinputservice is client only!)

local UserInputService = game:GetService('UserInputService')

function onInputBegan(input,gameProcessed)
    if input.KeyCode == Enum.KeyCode.Up then
        -- Up arrow 
    end
end

UserInputService.InputBegan:connect(onInputBegan)

You can see I use Enum for the keycode, you can find the keycodes here

But these are the ones you need

Enum.KeyCode.Up --> Up
Enum.KeyCode.Down --> Down
Enum.KeyCode.Left --> Left
Enum.KeyCode.Right --> Right

To do the normal stuff (letters) you can simply do this

Enum.KeyCode.A --> A
Enum.KeyCode.B --> B
Enum.KeyCode.C --> C

There are more things you can do with it, but these are the things you need.

If you have a question, just ask :)

0
Perfect, exactly what I was looking for thank you! The link to the other KeyCodes was also very useful, much appreciated! :) AlexTheCreator 461 — 6y
0
Np :) User#20388 0 — 6y
Ad

Answer this question