Often I read some code snippets to toggle something on/off by pressing one key to toggle it off, and the capital letter to toggle it on.
This is done, because the g_InKey is triggered too often.
I found a solution/workaround to only use one key/letter.
The following code snippet is for testing if key "o" or "O" is pressed:
function scriptname_init(e)
your_toggle_var = false -- we need this variable to save the state for on/off; you can set it to true, if that what you want to toggle should be active at start; choose a name that fits for your needs
keyPressedO = false -- we need this variable to determine if "o" or "O" is pressed; choose a different name for any other key
end
function scriptname_main(e)
if (g_InKey == "o" or g_InKey == "O") and not keyPressedO then
your_toggle_var = not your_toggle_var -- switch true <-> false
keyPressedO = true
elseif (g_InKey == "o" or g_InKey == "O") then
keyPressedO = true
else
keyPressedO = false
end
if your_toggle_var then
-- do something to toggle this on, what you want to activate, e.g. toggle your own HUD
end
end
With this code it doesn't matter if you press "o" or "O" and you don't need to press "o" to activate and "O" to deactivate your thing.
I hope this is helpful.
regards
Vrakyas