Page 1 of 1

detecting "Double-click" vs "single click" from numpad

Posted: 27 Jan 2019, 22:25
by AnnaV
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 :)

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
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?

Re: detecting "Double-click" vs "single click" from numpad

Posted: 28 Jan 2019, 10:59
by admin
Timer would be resource consuming.
Better remember timestamp of the key press, store it to some global variable and on every 0 key press compare current timestamp with the last one stored. Get the difference and decide.
Lua probably has something built in to get current miliseconds (or from system startup would work as well) and if not, the timestamp info should be provided as 3rd parameter of macro callback.
See e.g. sample callback function at https://github.com/me2d13/luamacros/blo ... st.lua#L70

Re: detecting "Double-click" vs "single click" from numpad

Posted: 28 Jan 2019, 11:19
by AnnaV
How do I get the timestamp though? setting the handler like

Code: Select all

function(button, direction, flags, ts) 
doesn't work. How to get the timestamp?

Re: detecting "Double-click" vs "single click" from numpad

Posted: 28 Jan 2019, 11:50
by AnnaV
With the lack of timestamp, I'm trying to use os.clock(). Like this:

Code: Select all

if (button == 45) then button_handler(os.clock()) end;
Then my handler has:

Code: Select all

button_handler = function(time)
local t_Delta = time - b_Lastpress
local doubleclick = false

print('Delta: ' .. t_Delta ..', time: ' .. time .. ', b_Lastpress: ' .. b_Lastpress)

if (t_Delta > 0.5) then
 doubleclick = false
else
 doubleclick = true
end

b_Lastpress = time

if (doubleclick) then
print('True')
else
print('False')
end

end
And as far as I can see, it is working, kind of. Now I'm just stumped on how do I distinguish a "single click" , from the "first of double click"?

I need X to happen when the button is pressed, and Y if it's a doubleclick. BUT "X" can't happen when it is a doubleclick!
How do I detect -on the first press- if there will be a second one?? or How do I cancel the first click if there WAS a second one?

Re: detecting "Double-click" vs "single click" from numpad

Posted: 28 Jan 2019, 14:08
by admin
Hmm if you need some action also on single click (and different on double click) then it currently not possible with luamacros :-(.
In this case you would really need to start timer on first click and on timer "alarm" perform action for single click if send press hasn't arrived.
You can log timers as feature request in github issues... I may add it if there's a need.