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

Why wont this KeyDown work?

Asked by
Dr_Doge 100
8 years ago

Problem: Im trying to make it so that when I press the ' key on my Keybord CaptureFocus is called on Textbox

Mouse=game.Players.LocalPlayer:GetMouse()
Mouse.KeyDown:connect(function(Key)
    if Key == string.char(34) then -- Why wont this work?
    TextBox:CaptureFocus()
end
end)

Can anyone tell me what Im doing wrong?

http://wiki.roblox.com/index.php?title=Keyboard_input_(deprecated)#Full_list

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Problem

It appears, this was when I just tested your script in studio, that the Shift + ' (which is the ") will only post quotes in the output (hard to explain). With keydown there are some keys that are able to be picked up by the event and therefore will not work. However sometimes you would need to mess around with the function to try and get it to detect if you're holding shift and are at the same time pressing '.


Solution

Use another tactic and use a different service. As you can see in the link you posted the event is deprecated. You should not be using it in new works. It is recommended you use UserInputService, in this case it is easier. You would want to use the InputBegan event and compare KeyCodes to find if a user is using double quotes. With this service it is easier to detect certain keys, and they do not get canceled out in favor of Roblox keys like KeyDown does.


Final Script

game:GetService('UserInputService').InputBegan:connect(function(Input, GameProcessed) --The Input is a list of different items, one of which being KeyCode which we'll utilize. GameProcessed will help us detect if the input was used by the game rather than this script.
    if Input.KeyCode == Enum.KeyCode.QuotedDouble and not GameProcessed then --If Double Quotes were used, then that will qualify for one of two conditions. If GameProcessed is not false then that likely means the player isn't chatting and you can capture focus on the TextBox.
        TextBox:CaptureFocus() --Just make sure TextBox is defined in your script.
    end
end)

Hopefully this answered your question, if so do not forget to hit the accept answer button. If you have any questions feel free to post them in the comments below.
Ad

Answer this question