Skip to content

Instantly share code, notes, and snippets.

@x4fx77x4f
Last active September 25, 2021 20:04
Show Gist options
  • Save x4fx77x4f/00b423e7b6ec4eacb612fe89f82a1e4f to your computer and use it in GitHub Desktop.
Save x4fx77x4f/00b423e7b6ec4eacb612fe89f82a1e4f to your computer and use it in GitHub Desktop.
KeyPress and KeyRelease debouncer for StarfallEx, to fix the hooks being run multiple times when the key was only hit once
--@name Key debouncer
--@shared
local hook = hook
local hook_add = hook.add
local hook_remove = hook.remove
local hook_run = hook.run
local isFirstTimePredicted = isFirstTimePredicted
local string = string
local string_lower = string.lower
local string_sub = string.sub
local function patchHookname(hookname)
hookname = string_lower(hookname)
if
hookname == 'keypress' or
hookname == 'keyrelease' or
string_sub(hookname, 1, 1) == '_'
then
hookname = '_'..hookname
end
return hookname
end
function hook.add(hookname, name, func)
return hook_add(patchHookname(hookname), name, func)
end
function hook.remove(hookname, name)
return hook_remove(patchHookname(hookname), name)
end
function hook.run(hookname, ...)
return hook_run(patchHookname(hookname), ...)
end
hook_add('keypress', '', function(ply, key)
if isFirstTimePredicted() then
return hook_run('_keypress', ply, key)
end
end)
hook_add('keyrelease', '', function(ply, key)
if isFirstTimePredicted() then
return hook_run('_keyrelease', ply, key)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment