[SOLVED] RegisterKeyMapping and Holding Keys

Hey there! So I was looking to help my friend with a basic script he was making (yet another radio ptt script… I know, I know, very saturated market…) but he was looking to take the twist of utilizing the newer (yet still old) RegisterKeyMapping native. Now the only issue that I’ve run into is that I can’t figure out how you would actually check if that key is being help down since I can’t get their mapping. Here’s some snippets of the code so far:

local RadioUp = false

Citizen.CreateThread(function()
    RegisterKeyBinds()
	while true do
		Citizen.Wait(0)
		local ped = PlayerPedId()
        local coords = GetEntityCoords(ped)
		if DoesEntityExist(ped) and not IsEntityDead(ped) and not IsPauseMenuActive() then
            if RadioUp then ...
function RegisterKeyBinds() 
    RegisterCommand("radio_toggle", function()
        if RadioUp then 
        RadioUp = false
        ResetChar()
        else 
            RadioUp = true
        end
    end)

    RegisterKeyMapping('radio_toggle', 'Key To Bring Your Radio Up', 'keyboard', 'LMENU')
end

Not sure if I’m just being dumb here but I couldn’t find very many topics around this so any discussion would really help me out as well as the rest of the community.

Thanks in advance! <3

3 Likes

I can kinda anticipate at least one of these comments so I might as well say that YES I have already looked through the docs, google, github and the FiveM cookbook… not sure if I missed something but if the answer lies within one of those sources please actually link me to it rather than saying "ReAd ThE dOcS"

local RadioUp = false

Citizen.CreateThread(function()
    RegisterKeyBinds()
	while true do
		Citizen.Wait(0)
		local ped = PlayerPedId()
        local coords = GetEntityCoords(ped)
		if DoesEntityExist(ped) and not IsEntityDead(ped) and not IsPauseMenuActive() then
            if RadioUp then ...
RegisterCommand('+radio', function()
    RadioUp = true
end, false)
RegisterCommand('-radio', function()
        RadioUp = false
        ResetChar()
end, false)
RegisterKeyMapping('+radio', 'Key To Bring Your Radio Up', 'keyboard', 'LMENU')

While that toggles the issue is that I only want RadioUp to = true when the key is depressed then when it is released RadioUp = false (aka when they hold L ALT bring radio up). I’ve tried your solution but it doesn’t seem to work with the whole holding thing.

Thanks for the quick response tho. LMK if you have any other ideas or if I just goofed

local RadioUp = false

Citizen.CreateThread(function()
    RegisterKeyBinds()
	while true do
		Citizen.Wait(0)
		local ped = PlayerPedId()
        local coords = GetEntityCoords(ped)
		if DoesEntityExist(ped) and not IsEntityDead(ped) and not IsPauseMenuActive() then
            if RadioUp then ...
                 --here the radio should be triggered in order to be displayed
RegisterCommand('+radio', function()
    RadioUp = true
end, false)
RegisterCommand('-radio', function()
        RadioUp = false
        ResetChar()
        --here should be triggered the radio to be hide
end, false)
RegisterKeyMapping('+radio', 'Key To Bring Your Radio Up', 'keyboard', 'LMENU')

check out the update

RegisterCommand('+radio', function() while true do Citizen.Wait(0) local ped = PlayerPedId() local coords = GetEntityCoords(ped) if DoesEntityExist(ped) and not IsEntityDead(ped) and not IsPauseMenuActive() then end, false) RegisterCommand('-radio', function() ResetChar() end, false) RegisterKeyMapping('+radio', 'Key To Bring Your Radio Up', 'keyboard', 'LMENU')

Why not just do it like this where it would just do the while loop when you’re holding then button.

I got it to work by doing the following:

RegisterCommand('+radio', function()
    RadioUp()
end, false)
RegisterCommand('-radio', function()
        ResetChar()
end, false)
RegisterKeyMapping('+radio', 'Key To Bring Your Radio Up', 'keyboard', 'LMENU')
function RadioUp()
-- all that cool shit

and removing the entire citizen thread.

My basic understanding of this native is as follows (since the docs were kinda subpar on this):

RegisterKeyMapping('commandString', 'description ', 'defaultMapper (keyboard)', 'defaultParameter (AKA KEY')

By placing the + infront of the commandString it will create a toggle system. Basing off of the above code snippets it will work as so:

1.) Press Down Left Alt = Call +radio ONE TIME
2.) Release Left Alt = call -radio ONE TIME

With this knowledge creating the toggle was pretty easy as I simply just called a function when the keys were either pressed down or released.

2 Likes

Thanks @Andyyy7666 and @Arazel for the help!

1 Like