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

How do I detect a number input from the keyboard? [UNSOLVED]

Asked by
u_g 90
9 years ago

So, for example, I want to create a function that fires every time a user presses one. Here's my code so far.

mouse.KeyDown:connect(function(key)
                if key == "f" then
                    -- Code
                end
end)

Except, I want to change the "f" into a one.

3 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

With KeyDown, it's not possible to get the number unless you disabled the Backpack. The reason for KeyDown not working with the numbers is because the key is being utilized by ROBLOX's Core Services and ROBLOX didn't want other scripts to interfere I guess.


KeyDown Method

So if you want to go the KeyDown route which I would advise against you would want to disable the Backpack CoreGui and compare the key's byte code to the byte code of one. Also, with your code, make sure you're getting the mouse otherwise you're calling KeyDown on a nil value.

game.StarterGui:SetCoreGuiEnabled('Backpack',false) --Backpack will no longer function.
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key) --Get the player's mouse and connect it to the KeyDown event.
    if key:byte() == 49 then --If 1 is pressed then the script will run. 49 is the byte code of the string '1'
        print'It Works!'
    end
end)

UserInputService Method

This one is a little trickier to set up however it is a more advanced method of getting a person's input. You will want to use a LocalScript for this as you would need for the KeyDown Method.

With UserInputService you will want to establish the InputBegan event. Since UserInputService is not automatically in DataModel, you will need to use the GetService() function. The InputBegan function returns two variables. One being the key information that was pressed, and the other being if the key being pressed is being handled by the game. For this example, the second variable can be ignored. The first variable returns an array of information which you can find here. Since we are wanting to find out the key pressed, we will only want to utilize the KeyCode value. We can then compare the key that was pressed to an Enumerator code.

game:GetService('UserInputService').InputBegan:connect(function(key,processed) --Establishing event.
    if key.KeyCode == Enum.KeyCode.One then --Comparing the key that was pressed.
        print'It Works!'
    end
end)

Hopefully this answer helped, if it did hit that upvote button. If this answered your question hit the accept answer button. If you have any questions or comments, leave them in the comments below.
0
Thanks! u_g 90 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

Numbers are the word version of themselves, so if the player presses '1' they'll get 'one'. Just refer to this: http://wiki.roblox.com/index.php?title=API:Enum/KeyCode

So:

mouse.KeyDown:connect(function(key)
                if key == "one" then
                    -- Code
                end
end)

Just note, KeyDown is deprecated, so you should try using ContextActionService, or UserInputService.

EDIT:

Like I said above, KeyDown is deprecated, so it may not work. But, the problem may be that you have not put the key into lowercase (the key can sometimes be 'ONE' instead of 'one').

So, add this straight under the start of the function, above the if statement:

key = key:lower()
0
Still doesn't work. u_g 90 — 9y
0
Edited. TheDeadlyPanther 2460 — 9y
0
KeyCode only works with UIS and CAS. Not KeyDown. M39a9am3R 3210 — 9y
0
Really? damn. TheDeadlyPanther 2460 — 9y
0
What M39 said. DragonODeath 50 — 9y
Log in to vote
0
Answered by
Phenite 20
9 years ago

Don't use KeyDown, that's deprecated. Instead, use UserInputService. Sure, UIS may sound complicated, but it's not- it's more useful than it is complicated. Note: UIS can only be accessed in LocalScripts.

local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
uis.InputBegan:connect(function(t) --Fires when the user starts an input, etc. a keypress. t is a table returned by the function that has other things, but we'll be focusing on t.KeyCode.
    if t.KeyCode == Enum.KeyCode.One then --If the keycode represented by the input is One, then
        print("One.") --print one
    end
end)

surprised of how many people still use deprecated events

0
I don't want to change my code; it's not compatible with LocalScript. u_g 90 — 9y

Answer this question