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

How to check if a key is being held down?

Asked by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago

I’m using UserInputService, I know how to check if a key was pressed, but I’m not sure how to tell if it’s being held down, how would I do that? How would I check if It weren’t held anymore too?

0
InputBegan for held down, InputEnded for released GoldAngelInDisguise 297 — 5y
0
I remember a more efficient way of doing it, are you sure that’s the only method? Ziffixture 6913 — 5y
0
I’m trying to create an event when the ‘E’ key is being held, whilst Left-Clicking, this is to drop an Item, I assumed it wans’t exactly possible to tie both events together, so I’m using a Boolean variable as a tie instead, just using it in am if statement using the Button2Down event, I don’t want to go through all this trouble to use up so much space for a simple Boolean switch Ziffixture 6913 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago

You can use the :IsKeyDown() method of UserInputService. Just pass the key you want and :IsKeyDown() will return true if the key is being held down, false otherwise.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gpe)
    if gpe then return end

    if input.UserInputType == Enum.UserInputType.MouseButton2 then -- you can detect mouse input
        if UserInputService:IsKeyDown(Enum.KeyCode.E) then
            -- Code
        end
    end
end)

Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button, and optionally, give an upvote. It helps out a lot. If you have any other questions, then feel free to leave them down in the comments.
0
I love your style of answering, it just has a tad bit of "unique" to it. Fifkee 2017 — 5y
0
Thank you! Ziffixture 6913 — 5y
Ad
Log in to vote
0
Answered by 4 years ago

First, insert a LocalScript into the StarterPlayerScripts in the StarterPlayer. Remove the print function and replace it with this:

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)

end

UserInput.InputBegan:Connect(inputR)

Now insert a simple if statement inside the local function with a return.

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)
    if gameProcessedEvent then return end -- I did it this way so it doesn't take up space.
end

UserInput.InputBegan:Connect(inputR)

Now make sure the user has a keyboard, so no weird errors occur.

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)
    if gameProcessedEvent then return end
    if inputObject.UserInputType == Enum.UserInputType.Keyboard then

    end
end

UserInput.InputBegan:Connect(inputR)

Now test the key you would like to be pressed. I Added more comments to make more clear.

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)
    if gameProcessedEvent then return end
    if inputObject.UserInputType == Enum.UserInputType.Keyboard then -- So we know that the player is holding down a key on their keyboard instead of a fake one
        if inputObject.KeyCode == Enum.KeyCode.R then -- Replace "KeyCode.R" with any other key, for example, left shift is "Enum.KeyCode.LeftShift". --
            --Do code for your key pressed here
        end
    end
end

UserInput.InputBegan:Connect(inputR) -- So we can fire the function when any key/mouse is pressed. Anything else will also fire the function, like gamepad.

I hope this was helpful!

0
Uh, I am a master Lua scripter now man. This is easy, you also got it wrong, is just as easy as one line... :IsKeyDown(), a method of the UIS family. This post is also a year old.. Ziffixture 6913 — 3y

Answer this question