[RELEASE] Lua Keybind Handler (not while-loop)

FiveM Keybind handler

Installation [EN]

  • Download the latest release
  • Extract the .zip file and add the pmc-keybinds to your resource folder
  • Add to your .cfg file the ensure pmc-keybinds

Usage [EN]

  • Open your fxmanifest.lua file
  • Add to client_scripts the keybind file @pmc-keybinds/import.lua
  • Inside any Lua client-side file use RegisterKey(layout:string, keyname:string, onpress:function[, onrelease:function])

Instalación [ES]

  • Descarga la última version
  • Extrae el archivo .zip y añade pmc-keybinds a tu carpeta resources
  • Añade a tu archivo .cfg ensure pmc-keybinds

Uso [ES]

  • Abre tu archivo fxmanifest.lua
  • Añade a client_scripts el archivo @pmc-keybinds/import.lua
  • Dentro de cualquier archivo Lua client-side utiliza RegisterKey(layout:string, keyname:string, onpress:function[, onrelease:function])

Example / Ejemplo

-- example 1
RegisterKey(
-- layout
'keyboard',
-- keyname
'E',
-- on press
function()
    print('key pressed 1')
end,
-- on release
function()
    print('key released')
end)

-- example 2
RegisterKey('keyboard', 'E',
function()
    print('key pressed 2')
end)

Download

GitHub Page
Release download link

7 Likes

Amazing, i was looking for something like this, i’ll put on my es_extended

1 Like

Pain to change it in every script.

But positive side, no more extra threads to each keypress. Huge optimization

Thanks, very nice idea, optimization is important.

Not really understand the difference but I will test it for sure, thank you so much for share!

anytime a key press is used with this method will make your chat will pop up correct?

Old method creates a while-loop and check every frame if the key is pressed, it takes some cpu usage. This new method don’t take any “cpu usage” from the resource.

1 Like

When I use it, my chat doesn’t pop up.

The only keys you should thread are keys that aren’t static (only used for 1 thing [inventory/phone/etc])


I don’t see the difference between this and the RegisterKeyMapping native above other than you execute a function and the native handles it via commands.

It’s just an unified wrapper for it, it seems:

Omg amazing…!!!

thanks for educating people who still use while loop keypress
this was already in cookbook ages ago. :smiley:

I like it

I also have ptoblems with changing the chat to this one…

1 Like

What type of problem do you have?

Thanks for sharing ^^

When i change the chat to this RegisterKey then the chat seems not to initialize correctly so on a restart the default GTA chat is enabled

how can I make it work with a controller?

Why are you not initializing the resource chat?
Only start the pmc-keybinds do not stop any other resource!
Check the Usage [EN] at the post.

You should add into the import.lua file the controller layout name and the keys.
I didn’t added them because I don’t have a controller to get them, if you can get the layout and the name of the keys and share it I will be adding it to the resource.

How to get more keys:

How I search for keybinds is to bind it in FiveM and then extract it from the fivem.cfg config file:

Location: %appdata%\CitizenFX\fivem.cfg

You will see a keybind like:
rbind {resource name} {defaultMapper} {defaultParameter} {command}

Example:
rbind keybind_example MOUSE_BUTTON MOUSE_LEFT "+example_keybind"
Where MOUSE_BUTTON is defaultMapper and MOUSE_LEFT is defaultParameter

credits to “Other Timmy” from Cfx.re discord