[Help] Check if control is pressed for a certain amount of time


#1

Hey, does anyone know a way to check if control is pressed for a certain amount of time?

Thanks,
Scotty


#2

Have a variable for a timer type thing. While the key is pressed, increment it… if timer == x, then do whatever.


#3

I tried doing this:

function holdDownKey(key, time)
  if IsControlJustReleased(1, key) then
    for i in range(time) do
      Citizen.Wait(100)
      if IsControlJustReleased(1, key) then
      else
        return false
      end
    end
    return true
  end
end

But it did not work. I thought maybe isControlPressed only returns when you press the control?


#4

I highly doubt this works but try something with a callback function… idk I have never done this :smile:

local key_holding = false
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)

        if IsControlPressed(1, 38) and not key_holding then
            key_holding = true
            HoldDownKey(38, 10, KeypressResult(result))
        end
    end
end)

function HoldDownKey(key, time, callback)
    for a = 1, time do
        Citizen.Wait(1000)
        if not IsControlPressed(1, key) then
            key_holding = false
            callback(false)
        end
    end
    key_holding = false
    callback(true)
end

function KeypressResult(result)
    if result then
        print("KEYPRESS ACCEPTED!")
    else
        print("FAILED TO HOLD KEY...")
    end
end

#5

I will try it when I get home, thanks :slight_smile:


Can i run this script in Fivem server?
#6

It’s just something I just put together off what you put above. :smile: So no idea if it will work. lol


#7
-- Check for this variable in other parts of your code
-- to determine if the button has been held down for +/- 1 second.
local pressed = false

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
-- Create a timer variable
        local timer = 0
-- Loop as long as the control is held down.
        while IsControlPressed(0, <control>) do
            Citizen.Wait(0)
-- Add 1 to the timer
            timer = timer + 1
-- If the timer is 60 or more, stop the loop (60 ticks/frames = +/- 1second)
            if timer > 60 then
                pressed = true -- or just call a function to be executed here
                break -- Stop the loop
            end
        end
-- Now wait until the button is released (to avoid running the timer above
-- again and again if the player keeps holding down the button)
-- Remove this while not loop if you don't want to wait for the user to
-- let go of the button before re-running the task again.
        while not IsControlJustReleased(0, <control>) do
            Citizen.Wait(0)
        end
-- Reset the pressed variable (remove this if you call a function instead)
        pressed = false
    end
end)

That’s how I used to check if a button was held down for 1 second before opening a menu for example.
(don’t forget the Citizen.Wait(0) in the “while iscontrolpressed” because if you don’t add that, 60 ticks will be reached within 1 frame as it doesn’t pause and wait for the next frame).


#8

Thanks for this :slight_smile: Will be sure to use it when I get home.