detecting "Double-click" vs "single click" from numpad
Posted: 27 Jan 2019, 22:25
Hi.
I have a cheap USB numpad that has a button for "00". Unfortunately this keypad has been made in such a way that this key isn't unique but just sends the "0" twice.
Is there a neat way to detect if a key is pressed quickly twice in succession? (debounce, is essence)
I was thinking of setting up a boolean with a timer and then checking the time between clicks, but that's bit of a "brute-force" way of doing it, or at least it feels like a duct-tape solution Is there a better way of achieving this?
The way I was thinking of doing it is about this (not tested, on the top of my head, improvising on the fly here, don't sue me for crappy code
In my mind at the moment, this works (except I don't know how to set the timer). It would start as false, at first keydown it would skip the if clause, set the boolean to true and continue to keyup. keyup sees boolean as true, sends input and sets the timer. Second keydown comes and sees the boolean as true, triggers the if clause and just returns. Second keyup still sees the boolean as false, because the last if clause skipped setting it true. keydown's if clause wouldn't trigger because boolean is false and does nothing. After 500ms things return to starting position.
Any tips, ideas, etc?
I have a cheap USB numpad that has a button for "00". Unfortunately this keypad has been made in such a way that this key isn't unique but just sends the "0" twice.
Is there a neat way to detect if a key is pressed quickly twice in succession? (debounce, is essence)
I was thinking of setting up a boolean with a timer and then checking the time between clicks, but that's bit of a "brute-force" way of doing it, or at least it feels like a duct-tape solution Is there a better way of achieving this?
The way I was thinking of doing it is about this (not tested, on the top of my head, improvising on the fly here, don't sue me for crappy code
Code: Select all
global:
boolean = false
keydown:
if boolean == true then return end
boolean = true
keyup:
if boolean = true then
send_input
timer for 500ms, set boolean = false
end
Any tips, ideas, etc?