Can't use hs.eventtap.keyStroke() to send the same key you're binding?

2,893 views
Skip to first unread message

da...@thedavid.co.uk

unread,
May 31, 2015, 1:34:48 PM5/31/15
to hamme...@googlegroups.com
The code below doesn't seem to work when the first and second lines use the same key (in this case, 'right'):

hs.hotkey.bind({'cmd'}, 'right', function()
  hs
.eventtap.keyStroke({}, 'right')
end)


This is relevant for me because I'd like to do things like:

hs.hotkey.bind(hyper, 'delete', function()
 
-- Delete the current word
  hs
.eventtap.keyStroke({'alt'}, 'left')
  hs
.eventtap.keyStroke({'alt', 'shift'}, 'right')
  hs
.eventtap.keyStroke({}, 'delete')
end)

but the last line doesn't seem to execute. Any known workaround for this?

Chris Jones

unread,
Jun 24, 2015, 4:48:24 AM6/24/15
to David Roberts, hamme...@googlegroups.com
Hey

At the moment the only real option is to use hs.timer.doAfter() to trigger the event after you've released the keyboard shortcut. It's kinda hacky, but the OS' input layers don't deal particularly well with the situation you're in, unfortunately!

Cheers,
Chris

--
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/5809e36b-9937-4d26-8459-b7550c4aa1c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Chris

David Roberts

unread,
Jun 24, 2015, 7:02:43 AM6/24/15
to Chris Jones, hamme...@googlegroups.com
Thanks, Chris - that's a workaround I'm happy with.

carlp...@gmail.com

unread,
Sep 21, 2016, 12:31:03 PM9/21/16
to Hammerspoon, cm...@tenshu.net, da...@thedavid.co.uk
I'm having this issue too but new to lua.. what would an example look like?

Chris Jones

unread,
Sep 21, 2016, 12:50:12 PM9/21/16
to carlp...@gmail.com, Hammerspoon, da...@thedavid.co.uk
Hey

Things have improved since that original post, if you want to have a keyboard shortcut that emits another keyboard event, use the 'releasefn' argument to hs.hotkey.bind() instead of the 'pressedfn' one (see http://www.hammerspoon.org/docs/hs.hotkey.html#bind )

So, an example that would emit Cmd-C when you press (and release) Ctrl-C would be:

hs.hotkey.bind({"ctrl"}, 'c', nil, function() hs.eventtap.keyStroke({"cmd"}, 'c') end)

Cheers,
Chris

David Roberts

unread,
Sep 21, 2016, 5:35:10 PM9/21/16
to Chris Jones, carlp...@gmail.com, Hammerspoon
Great to see things keep getting better :)


Message has been deleted

jiaxi...@gmail.com

unread,
Apr 6, 2017, 1:06:36 AM4/6/17
to Hammerspoon, carlp...@gmail.com, da...@thedavid.co.uk
Hi, Chris: 

When pressing a key, people are expecting the character to be inserted upon a key down event, instead of a key up. The current solution seems to change the behavior. 

I understand this is not a limitation of OSX. In the following eventtap, if we are able to change the event in the callback, we are going to be able to have the key mapped upon a key down event. 

CGEventRef eventtap_callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
    ....
    change the event
    ...
    return event;
}

I wonder if it's possible to change hammerspoon's code to pass in the event into the pressedfn function?

Thanks, 
Jiaxin. 

在 2016年9月21日星期三 UTC-7上午9:50:12,Chris Jones写道:

asmagill

unread,
Apr 6, 2017, 8:44:30 PM4/6/17
to Hammerspoon, carlp...@gmail.com, da...@thedavid.co.uk, jiaxi...@gmail.com
This *is* a limitation of the CarbonEvents currently being used by `hs.hotkey`.  What you're talking about is true, but only if we switch hotkey over to use CoreGraphics events.  Doing so has it's own issues, most notably that when Hammerspoon gets bogged down for any reason, keyboard input across *all* applications pauses until Hammerspoon goes idle again.

I am working on a rewrite that I hope can mitigate this, but since it's a fundamental re-write of `hs.hotkey` and `hs.eventtap`, it's proceeding slowly.

Jiaxin Cao

unread,
Apr 7, 2017, 2:18:23 PM4/7/17
to asmagill, Hammerspoon, carlp...@gmail.com, da...@thedavid.co.uk
Thanks, asmagill. This is a feature that I'm really looking forward to. So I wonder if you have a rough timeline about it? Any plans to implement it within a year?

--
You received this message because you are subscribed to a topic in the Google Groups "Hammerspoon" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hammerspoon/yp4AvJr5v7Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hammerspoon+unsubscribe@googlegroups.com.

To post to this group, send email to hamme...@googlegroups.com.

edj...@gmail.com

unread,
Jun 8, 2017, 7:39:07 PM6/8/17
to Hammerspoon, carlp...@gmail.com, da...@thedavid.co.uk
Thanks for this suggestion! I have tried this solution and I cannot get the result I want.

I have tried 
hs.hotkey.bind({"alt"}, 'left', nil, function() hs.eventtap.keyStroke({"ctrl"}, 'left') end)

and
hs.hotkey.bind({"alt"}, 'left', nil, nil, function() hs.eventtap.keyStroke({"ctrl"}, 'left') end)

and even
hs.hotkey.bind({"alt"}, 'left', nil, nil, function() hs.eventtap.keyStroke({"ctrl"}, 'left') end, nil)

But I think I am missing something because when I press alt+left literally nothing happens.

Thanks!

kevinforr...@gmail.com

unread,
Jun 22, 2019, 7:11:29 PM6/22/19
to Hammerspoon
Any update?  I really need this functionality, as I'm doing something on keydown, and then emitting the keys on keyup.

Jiaxin Cao

unread,
Jun 3, 2022, 11:44:00 PM6/3/22
to Hammerspoon
I'm curious that if there is any update for this thread?

Pablo Escobar

unread,
Mar 20, 2023, 4:41:49 PM3/20/23
to Hammerspoon
Me too
Reply all
Reply to author
Forward
0 new messages