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

Help me with Key events?

Asked by
xTalzo 10
9 years ago

I need help with assigning to Keys. I have no knowledge on this category, so please explains slowly.

0
You must give it a try, start here! -- http://wiki.roblox.com/index.php?title=API:Class/Mouse/KeyDown dragonkeeper467 453 — 9y

3 answers

Log in to vote
7
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Prerequisite Knowledge

There are a few things you need to know prior to attempting to do actions with keys. Firstly, you need to realize that pressing keys, as well as any other think like clicking, or scrolling your camera in and out, is directed from User Input. User Input will ALWAYS be on the client, hence the User part of it.

What I mean by this is that whenever working with User Input, you must have the code in a LocalScript. Said localscript has to be in somewhere that replicates to the client. If you don't understand the concept of replication, think of it like what you have and what the server has. Server to client communication is the key to games on roblox. Anyways, as I was saying.. The localscript has to be in somewhere that replicates to the client; e.g StarterGui, StarterPack, ReplicatedFirst, etc..

Now that you know all of that useful information.. i'll explain both ways to access User Input of keys on the keyboard.


UserInputService

This way, isn't as widely known four the usage of key detection.. but it's absolutely the best way. I reccomend this entirely for you. UserInputService is the service, there is an event of this called InputBegan. Events are listeners for certain actions to happen .. in this case, a listener for the client to prove any User Input.

The InputBegan event is detection for when any and all User Input is initially detected.

This event returns an instance object for what input was provided, as well as if the input was provided as a Game Processed Even(GPE). A GPE is say.. whether you were chatting when the event was activated.. Thus, a perfect way to make sure the client is not chatting to activate code.

--Define UserInputService
local uis = game:GetService('UserInputService')

--InputBegan
uis.InputBegan:connect(function(input,process)
    --Make sure they're not chatting
    if not process then
        --Check if the key was 'E'
        if input.KeyCode == Enum.KeyCode.E then
            print('E was pressed')
        end
    end
end)

KeyDown

This is a more 'mainstream' way of accessing keys due to the fact that it has been around much longer, but it's definitely not the best way. This way has limited access to keys as well as is deprecated. It purely exists for compatibility of older code, and shouldn't be used in code nowadays. But knowledge is power, my friend.

KeyDown is an event. It is a member of the user's mouse.. which can be accessed through the GetMouse function of the player object.

This event returns a string of the key that was pressed. So, naturally, to check which key was pressed you must compare the returned value with another string.. the said character that you want to be pressed.

--Access the player
local plr = game.Players.LocalPlayer
--Access their mouse
local mouse = plr:GetMouse()

--Keydown event, parameter 'key' being the key that was pressed
mouse.KeyDown:connect(function(key)
    --Check if the key was 'e'
    if key == 'e' then
        print('E was pressed')
    end
end)
Ad
Log in to vote
0
Answered by 9 years ago

To set a hotkey, put the following code into a LocalScript in StarterPack/put the LocalScript into Backpack through another script.

local p = game.Players.LocalPlayer
local mouse = p:GetMouse()

mouse.KeyDown:connect(function(key)
    key = key:lower()
    if key == "f" then
        -- insert code here
    end
end)
Log in to vote
-2
Answered by 9 years ago

Next time use the ROBLOX Lua Wiki: http://wiki.roblox.com/index.php?title=Scripting

Local Script in the StarterPlayerScripts:

local players = game:GetService:Players() -- get player service
local player = players.LocalPlayer -- gets the player
repeat wait() until player:GetMouse() -- yield until the mouse exists
local mouse = player:GetMouse() -- gets mouse because key events happen w/ mouse

function onKeyDown(key) -- our function that will be called every time the client press's a key
    -- uncomment the key:lower() or string.byte(key) to see what it does.
    --key = key:lower() -- just makes the key lowercase in case the player had uppercase/caplocks on
    --key = string.byte(key) -- turns key into a number which can be useful for spacebar, esc, et cetera
    print(key)
end

mouse.KeyDown:connect(onKeyDown) -- connects the event to the function
0
This could be shortened down a lot I'm unsure why you are making it so complicated to get the player and mouse. Ryzox 220 — 9y
0
KeyDown is deprecated, it is not good advice to recommend this to someone. Goulstem 8144 — 9y

Answer this question