Passing a keystroke through

150 views
Skip to first unread message

da...@thedavid.co.uk

unread,
Jan 15, 2018, 11:56:34 AM1/15/18
to Hammerspoon
I'm trying to use Hammerspoon to display an alert every time I press Cmd-C (sometimes I hit the keys wrong and I only realise when I try to paste, which is really annoying).

The following code seems to get stuck – I imagine because the hotkey function gets called recursively through hs.eventtap.keyStroke()

hs.hotkey.bind('cmd', 'c', function()
  hs
.eventtap.keyStroke({'cmd'}, 'c', keyDelay)
  hs
.alert.show("Copy")
end)

Any tips on how I can get this working?

Diego Zamboni

unread,
Jan 21, 2018, 7:58:09 AM1/21/18
to da...@thedavid.co.uk, Diego Zamboni, Hammerspoon
Hi David,

The following works for me, using hs.eventtap instead of hs.hotkey.bind to set up the binding:

hs.eventtap.new({hs.eventtap.event.types.keyDown},
  function(e)
    if (e:getCharacters() == "c" and e:getFlags():containExactly({"cmd"})) then
      hs.alert.show("Copy")
    end
    return false
  end
):start()

—Diego


--
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/52463925-f9ee-463f-87f9-6d09f88f3705%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Roberts

unread,
Jan 22, 2018, 4:23:19 AM1/22/18
to Diego Zamboni, Hammerspoon
Thanks, Diego – that works well :)


On Sun, Jan 21, 2018 at 1:56 PM, Diego Zamboni <di...@zzamboni.org> wrote:
Hi David,

The following works for me, using hs.eventtap instead of hs.hotkey.bind to set up the binding:

hs.eventtap.new({hs.eventtap.event.types.keyDown},
  function(e)
    if (e:getCharacters() == "c" and e:getFlags():containExactly({"cmd"})) then
      hs.alert.show("Copy")
    end
    return false
  end
):start()

—Diego

On 15 Jan 2018, at 17:56, da...@thedavid.co.uk wrote:

I'm trying to use Hammerspoon to display an alert every time I press Cmd-C (sometimes I hit the keys wrong and I only realise when I try to paste, which is really annoying).

The following code seems to get stuck – I imagine because the hotkey function gets called recursively through hs.eventtap.keyStroke()

hs.hotkey.bind('cmd', 'c', function()
  hs
.eventtap.keyStroke({'cmd'}, 'c', keyDelay)
  hs
.alert.show("Copy")
end)

Any tips on how I can get this working?

--
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.
Reply all
Reply to author
Forward
0 new messages