Help with holding buttons

Is it possible to detect whether a button is held and tapped without using InputEnded?

The problem with InputEnded is that the key is only registered after button is let go, and not immediate.

Games like a universal time have this feature implemented so I was wondering how they did it.

1 Like

You can use InputBegan for it to be immediate.

3 Likes

Yes, but InputBegan does not work when detecting button holds, only taps.

no, just use inputended and detect how long it took between began and ended

You still have to, but preferably in another way.

I recommend doing something like:

local UIS = game:GetService("UserInputService")

local TapThreshold = 0.02
local KeyCode = Enum.KeyCode.E

UIS.InputBegan:Connect(function(Input,GPE)
    if KeyPressed then return end
    if GPE or Input.UserInputState ~= Enum.UserInputState.Begin then return end
    if Input.KeyCode ~= KeyCode then return end

    local Now = tick()
    local HoldEventThread = task.delay(TapThreshold,function()
        --This is your looped (or not) hold key code
    end)
    
    local InputEnded
    InputEnded = UIS.InputEnded:Connect(function(Input)
        if Input.KeyCode ~= KeyCode then return end
        InputEnded:Disconnect()
        task.cancel(HoldEventThread)
        if tick() - Now < TapThreshold then
            --This is your tap key code
        end
    end
end

You may have to fork and adjust something, maybe fixing some bugs as I can’t access studio currently.

  1. You can use a boolean variable called “buttonHeld” and set it to false initially.
  2. Use the InputBegan event to detect when the button is pressed. Inside the InputBegan event handler, set the “buttonHeld” variable to true.
  3. Use the InputChanged event to continuously check if the button is still being held. Inside the InputChanged event handler, check if the “buttonHeld” variable is true. If it is, then the button is being held.
  4. Additionally, you can use the InputEnded event to detect when the button is released. Inside the InputEnded event handler, set the “buttonHeld” variable to false

I made a code to try and it works for me, this is the code example:

local UserInputService = game:GetService("UserInputService")

local buttonHeld = false

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		buttonHeld = true
		print("Key pressed: " .. input.KeyCode.Name)
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if buttonHeld then
			print("Key held: " .. input.KeyCode.Name)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		buttonHeld = false
		print("Key released: " .. input.KeyCode.Name)
	end
end)

Is that what u meant?

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local buttonHeld = {}

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        buttonHeld[input.KeyCode] = true
        print("Key pressed: " .. input.KeyCode.Name)
    end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        buttonHeld[input.KeyCode] = false
        print("Key released: " .. input.KeyCode.Name)
    end
end)

RunService.RenderStepped:Connect(function()
    for key, isHeld in pairs(buttonHeld) do
        if isHeld then
            print("Key held: " .. key.Name)
        end
    end
end)

If you want to print when the key is holding and what key is being held this is the perfect code

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.