hotkey repeat

563 views
Skip to first unread message

wheresw...@gmail.com

unread,
Feb 4, 2015, 12:16:12 PM2/4/15
to hamme...@googlegroups.com
Is there a way to do automatic hotkey repeat when a hotkey is held?

Thanks,
Max

Chris Jones

unread,
Feb 5, 2015, 6:07:05 PM2/5/15
to wheresw...@gmail.com, hamme...@googlegroups.com
Hi

At the moment, no, we trigger callbacks when the keys are initially pressed down.

Out of interest, what's your use case for repeating hotkeys?

Cheers,
--
Chris Jones
--
You received this message because you are subscribed to the Google Groups "Hammerspoon" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hammerspoon...@googlegroups.com.
To post to this group, send email to hamme...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/hammerspoon/3e547fb4-11c7-4f89-aa3e-72e6259ec3b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Rothman

unread,
Feb 5, 2015, 7:16:20 PM2/5/15
to Chris Jones, hamme...@googlegroups.com
I'd like to be able to, say, move a window in a direction continuously by holding a hotkey. Could this be accomplished with modal hotkeys? Maybe using the pressedfn to activate a move and the releasedfn to stop it?

Thanks,
Max

boole...@gmail.com

unread,
Mar 2, 2015, 3:04:07 AM3/2/15
to hamme...@googlegroups.com, cm...@tenshu.net, wheresw...@gmail.com
I thought this message was newer but anyway I implemented my own function for dealing with this

function keyRepeat(mods, key, delay, interval, repeatfn)
local modal_key = hs.hotkey.modal.new(nil, nil)
local key_repeat = false
local timer
timer = hs.timer.new(interval, function() if not key_repeat then timer:stop() else repeatfn() end end)
modal_key:bind(mods, key, 
function()
key_repeat = true
repeatfn()
hs.timer.doAfter(delay, function() if key_repeat then timer:start() end end)
end, 
function()
key_repeat = false
timer:stop()
end
)
modal_key:enter()
end

the delay parameter is there to not start repeating until x seconds, this was needed in my specific case where I wanted to use ctrl + j/j//h/l as movement keys, without it I would end up moving more than one line sometimes when pressing once, probably not needed in the cases where you want to resize or move window, etc, and using the same number as interval would be ideal

this is how I'm using it:

keyRepeat(ctrl_mod, 'j', 0.5, 0.1, function() hs.eventtap.event.newKeyEvent(no_mod, 'down', true):post() end)
keyRepeat(ctrl_mod, 'k', 0.5, 0.1, function() hs.eventtap.event.newKeyEvent(no_mod, 'up', true):post() end)
keyRepeat(ctrl_mod, 'h', 0.5, 0.1, function() hs.eventtap.event.newKeyEvent(no_mod, 'left', true):post() end)
keyRepeat(ctrl_mod, 'l', 0.5, 0.1, function() hs.eventtap.event.newKeyEvent(no_mod, 'right', true):post() end)




On Thursday, February 5, 2015 at 5:16:20 PM UTC-7, Max Rothman wrote:
I'd like to be able to, say, move a window in a direction continuously by holding a hotkey. Could this be accomplished with modal hotkeys? Maybe using the pressedfn to activate a move and the releasedfn to stop it?

Thanks,
Max

On Thursday, February 5, 2015, Chris Jones <cm...@tenshu.net> wrote:
Hi

At the moment, no, we trigger callbacks when the keys are initially pressed down.

Out of interest, what's your use case for repeating hotkeys?

Cheers,
--
Chris Jones

On 4 Feb 2015, at 17:16, wheresw...@gmail.com wrote:

Is there a way to do automatic hotkey repeat when a hotkey is held?

Thanks,
Max

--
You received this message because you are subscribed to the Google Groups "Hammerspoon" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hammerspoon+unsubscribe@googlegroups.com.

Chris Jones

unread,
Mar 3, 2015, 5:43:36 AM3/3/15
to boole...@gmail.com, hamme...@googlegroups.com, Max Rothman
Hi


On 2 March 2015 at 08:04, <boole...@gmail.com> wrote:
I thought this message was newer but anyway I implemented my own function for dealing with this

So, interestingly enough, the system APIs for doing hotkeys don't actually support repeating and so the typical way people do it is exactly how your Lua implementation works :)

I am considering adding support for a repeat callback to hs.hotkey, but I'm now wondering if putting your Lua version in the Getting Started Guide would be sufficient.

What do people think?

--
Cheers,

Chris

Max Rothman

unread,
Mar 3, 2015, 10:02:55 AM3/3/15
to Chris Jones, boole...@gmail.com, hamme...@googlegroups.com
Thanks, boolemilio, that looks great!

Chris: That's odd, given that the keyboard preferences have options for key repeat. But I'd vote to have it be added to hs.hotkey, since otherwise, people will just be copy-pasting it wholesale into their inits, and that seems to go against the modularity of Mjolnir and the abstraction of Hammerspoon.

Chris Jones

unread,
Mar 3, 2015, 6:14:41 PM3/3/15
to Max Rothman, boole...@gmail.com, hamme...@googlegroups.com
Hi

On 3 March 2015 at 15:02, Max Rothman <wheresw...@gmail.com> wrote:
Chris: That's odd, given that the keyboard preferences have options for key repeat. But I'd vote to have it be added to hs.hotkey, since otherwise, people will just be copy-pasting it wholesale into their inits, and that seems to go against the modularity of Mjolnir and the abstraction of Hammerspoon.

As of http://git.io/xP99 I've pushed what I think is complete support for a third callback function to hs.hotkey.new() and hs.hotkey.bind(), so you can now have a callback for pressing a key, one for releasing a key and one for the key being held down long enough to repeat.

It'll likely be a few days before 0.9.25, so I'll keep testing it locally and see if I can find any issues (although mostly I'll be testing normal hotkeys, to make sure this hasn't broken anything!)
 
--
Cheers,

Chris

Max Rothman

unread,
Mar 3, 2015, 8:42:12 PM3/3/15
to Chris Jones, Emilio Bool, hamme...@googlegroups.com
Thanks Chris, that looks great! Does it support customization for repeat delay and repeat rate?

Max Rothman

unread,
Mar 3, 2015, 8:49:31 PM3/3/15
to Chris Jones, Emilio Bool, hamme...@googlegroups.com
Also, it looks like you can access at least the user settings for key repeat delay and rate: http://stackoverflow.com/questions/13073328/cocoa-detecting-key-held-down

Chris Jones

unread,
Mar 4, 2015, 3:16:56 AM3/4/15
to Max Rothman, Emilio Bool, hamme...@googlegroups.com
Hi

Haha, I knew I'd get asked for that, but I didn't expect it straight away ;)

Right now, no, but the NSEvent values you found should make it very easy to add. Thanks!

Cheers,
--
Chris Jones

Chris Jones

unread,
Mar 4, 2015, 3:19:17 AM3/4/15
to Max Rothman, Emilio Bool, hamme...@googlegroups.com
Hi

Oh, also, I meant to say that unlike the code in your link, I have made the timer cancellation be *extremely* conservative. If it sees *any* event, it cancels the repeat, it doesn't check for the Release event of the specific key that's repeating. I want to be very very sure that we don't get people stuck in a repeat loop.
Also I cancel the timer if the repeat function causes a Lua trace back. 

Cheers,
--
Chris Jones
Reply all
Reply to author
Forward
0 new messages