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

How to bind a key?

Asked by
emite1000 335 Moderation Voter
9 years ago

A pretty straight-forward question. What event would I use to have something happen when a certain key is pressed?

I've never done anything like this before so I have no idea where to start and I didn't see any tutorials or anything on the wiki.

0
Why all the downvotes? :( emite1000 335 — 9y
0
Here let me help you out. woodengop 1134 — 9y

3 answers

Log in to vote
1
Answered by 9 years ago

First you need to get the player's mouse.

function Equipped(mouse)
end
script.Parent.Equipped:connect(Equipped)

You don't have to do it with a tool, but this is how I am demonstrating it.

Then, you want to detect when a key is pressed.

function Equipped(mouse)
    function KeyDown(k)
    end
    mouse.KeyDown:connect(KeyDown)
end
script.Parent.Equipped:connect(Equipped)

Same works for when a key is released.

k is the variable for the key that you want. So if you wanted to check for the key "E" then you would do this.

function Equipped(mouse)
    function KeyDown(k)
        if k == "e" then
            --Code
        end
    end
    mouse.KeyDown:connect(KeyDown)
end
script.Parent.Equipped:connect(Equipped)

Some keys like shift or tab don't accept the names of them, so if you want to find out how to use them, use print(k) and put whatever comes out in the output in the if statement. You can also use elseifs if you want to detect different keys.

Hope this helps!

0
According to the wiki, "KeyDown" is deprecated. Is there an event I should use at line 7 instead? emite1000 335 — 9y
0
Just ignore that. I got this from a working script that I made. (The guns on Spectrum) User#348 0 — 9y
0
But is there anything I should use instead? It says don't use in new scripts. emite1000 335 — 9y
0
There is, its called InputObject, but I actually have no idea how to use it, and KeyDown still works on new scripts. If I figure out how to use InputObject (and I intend to) I will be sure to tell you! User#348 0 — 9y
Ad
Log in to vote
1
Answered by
woodengop 1134 Moderation Voter
9 years ago

QuirkySquid's Script, only works with Tools, if you want to Use the keydown function for Other things besides a Tool then you would code like so:

local plr=game.Players.LocalPlayer
local mouse=plr:GetMouse()--see how I used the GetMouse Method

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

This gets the Key that was Pressed which is the letter e, What if you wanted the Space bar or the Control key, then we would use the string.byte, Here is a layout:

mouse.KeyDown:connect(function(key)--I'm using the same variables from the 1st script
    if key:byte()==13 then--Enter Key
        print("Enter")
    end
end)

There is a Wiki article about the Byte codes, but there is a Neat trick to find these Bytes(Credit to BlueTaslem):

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key) 
print(key,key:byte()); end);

Take Note that the KeyDown Function can only be ran on LocalScripts

Log in to vote
1
Answered by 9 years ago

The KeyDown function has been deprecated now. In favour of UserInputService

To get an input we can use this:

function InputDown(input,gameProcessedEvent)
--keys
end

game:GetService("UserInputService").InputBegan:connect(InputDown)

UserInputService is great as it can recognise gamepad and mouse inputs as well! To define what we're looking for we can use this if statement:

if input.UserInputType == Enum.UserInputType.Keyboard then

This if statement will only allow inputs from a keyboard. to Define a key to be pressed we can use this:

if input.KeyCode == Enum.KeyCode.R then

This will fire when the R key is pressed on a keyboard. UserInputService allows for more special keys to be pressed as now you can detect when tab and return are down. All together we get this:

function InputDown(input,gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.R then
            --code      
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(InputDown)

I would highly recommend the switch to UserInputService as it allows you to do a lot more in terms of input.

Answer this question