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

Announcements, questions
Post Reply
AnnaV
Posts: 8
Joined: 17 Jan 2019, 20:41

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

Post by AnnaV » 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 :)

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?

admin
Site Admin
Posts: 735
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

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

Post by admin » 28 Jan 2019, 10:59

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
Petr Medek
LUAmacros author

AnnaV
Posts: 8
Joined: 17 Jan 2019, 20:41

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

Post by AnnaV » 28 Jan 2019, 11:19

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?

AnnaV
Posts: 8
Joined: 17 Jan 2019, 20:41

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

Post by AnnaV » 28 Jan 2019, 11:50

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?

admin
Site Admin
Posts: 735
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

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

Post by admin » 28 Jan 2019, 14:08

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.
Petr Medek
LUAmacros author

Post Reply