[addressed] Function for detecting "double press" on grid in lua

Hi,
I’m trying to use double press on my grid in a script I’m writing for my norns.
Does anyone have a suggestion on how to do this? Double press meaning, the same button pushed twice fast, on my grid.
I’m imaging something like a function that would return true/1 if it had been triggered twice fast enough by two button presses of the same button.
That way I could use it in a condition for executing some specific code only for double press.

if x == 1 and y == 3 and z == 1 then
If doublepress() true then … end
end

I’m still learning lua, and I tried to have a look through some of the other scripts, but couldn’t find something I understood to be it. Is this even the right way to approach this problem?
Thanks in advance,
Anders

Thinking out loud - you could store the grid key press in a map with the timestamp as each key’s value and on each press check if that grid key exists in the map. If so check it with the current timestamp and check whether it’s within the timeframe you constitute as fair for a double click. This may be overcomplicated for what you’re trying to do though.

4 Likes

as mentioned by mrbillz, you’d probably need to use a timer of some sort to figure out when something is a doublepress.

look at the metro functions and the norns studies as a good place to start.

1 Like

maybe something like

local last_press_time = 0

function is_doublepress()
  return util.time() - last_press_time < doublepress_threshold
end

function keydown()
  if is_doublepress() then
    -- do stuff
  else
    last_press_time = util.time()
  end
end
8 Likes

Just thinking out loud, but you could set a parameter when the button is pressed and use a clock to clear that value after n milliseconds.

When a button press comes in, if that parameter is set, then it’s a double click (for the given time value).

1 Like

I enden up doing this:

function doublepress(x,y,threshold)
if util.time() - last_pressed[x][y] < threshold then
return true
else
last_pressed[x][y] = util.time()
end
end

last_pressed is an array that stores information on when all of my buttons where last pressed. Not very elegant, and there probably is a smarter way, but this way it can recognize the buttons from each other, and it is only a doublepress when it’s the same button being pressed.
But it seems to work well enough.
Thanks for all the suggestions!

(next up is learning how to get the syntax highlighting to show when posting on the forum…)

4 Likes

Conceptually this seems really nice, and in terms of efficiency is almost certainly better than starting a new timer on each button press. An array of 128 elements is quite small, and calling util.time() twice per press seems fine to me?

That said, I’m new to lua, so I’d be happy to hear from people with more knowledge.

if you wrap your code in ``` it should do the syntax highlighting :slight_smile:

1 Like

this is exactly what i would recommend!

1 Like

Don’t forget if you have a function mapped to a single press you’ll need to cancel it out (and/or not action it until your threshold time expires)

2 Likes

I wrote a little patch to do this with grid + Max: Grid Single/Double/Long Press Detector. I used timers and gates to get the job done. Looks like you solved it but there could be some inspiration for you in here if you ever need to add long presses, too!

1 Like

Thanks, this is cool.
I wanted to do a long press as well, but haven’t gotten into it yet. I’ll take a look at your max patch

I don’t think you need to remember the last-pressed of all the buttons, but simply the location of the last press. It’s a double-press if the current press is the same button as the previous press, and the interval is sufficiently short.

Also, be aware that a triple-press might come in as two double-presses.

Just be thankful that you don’t have to debounce. (I assume the grid hardware is doing that.) Debouncing is a pain in the neck.

1 Like