Hammerspoon: Listening on Modifier Keys

Created on 4 Oct 2016  路  19Comments  路  Source: Hammerspoon/hammerspoon

I'm looking for a way to listen to "right cmd" button being pressed. I want to remap it to "right alt", because I feel that real "right alt" button is too far away.

This is what I have so far:

rcmd_handler = function(evt)
  ...
end

rcmd_tap = hs.eventtap.new({hs.eventtap.event.types.keyDown}, rcmd_handler)
rcmd_tap:start()

Is there any event that fires when 'right cmd' is pressed? keyDown doesn't seem to work for that button.

Bonus question: how can I trigger 'right alt' key press?

Most helpful comment

Sorry for the delay in getting back to this -- I am working on an addition to eventtap that allows creating raw events of any type, but it has proven to be a little more difficult than originally expected, since some event properties _must_ be created and set through NSEvent rather than CGEvent (which is what the bulk of the module is based on)... I hope to have something for testing later this week/next week, but I did want to note that it's in progress.

All 19 comments

I'm certainly no expert - but maybe something like this would work?

rcmd_tap = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(event)
    local whichFlags = event:getFlags()
    if whichFlags['cmd'] then
        hs.eventtap.keyStroke({"alt"}, "")
    end 
end)

You should use {hs.eventtap.event.types.flagsChanged}.

Okay, I got everything working except for triggering the 'alt' event. Unfortunately hs.eventtap.keyStroke({"alt"}, "") doesn't fire the same key codes as pressing alt would.

Same behavior when I do: hs.eventtap.event.newKeyEvent({"alt"}, "", true):post().

You can use AppleScript: hs.osascript.applescript('tell application "System Events" to key code 58'). This is left alt. The keycode of right alt is 61.

@xream, Interesting... I wonder... I'll need to modify hs.eventtap.event.newKeyEvent to take a numeric value instead of just a character to see if we can replicate this... do you know if there is a list of the "modifier" keys and their keycodes somewhere? hs.keycodes.map only contains the "visible" characters, and I can confirm that 58 and 61 are not set in that table, so I suspect we can probably send these ourselves with a few tweaks.

@asmagill You can print hs.eventtap.event:getKeyCode() to get the keycode. What you said will not work. Because we can only trigger keyDown and keyUp events with/without modifier keys, not flagsChanged which is pressing a modifier key .

@daGrevis, I'd be curious if the following works for generating a "right alt keypress":

hs.eventtap.event.newKeyEvent({}, " ", true):setKeyCode(61):post()
hs.eventtap.event.newKeyEvent({}, " ", false):setKeyCode(61):post()

The second argument to newKeyEvent doesn't matter, as long as it's a valid key, since the second method should reset it to keycode 61, and since we're emulating a keypress, you'll need to do both lines in succession.

If this doesn't work, I'll look into adding the creation of arbitrary events to hs.eventtap.event this weekend and see if we can track down a combination that does work... calling out to AppleScript, while functional, is slow.

@xream, I was hoping for a list defining _all_ of the known keycodes... and I think I found it in the Events.h header from the Carbon framework. The current constructors for hs.eventtap.event are limited to key, mouse, and scroll event types, but the CoreGraphics functions those constructors use _do_ support creating other event types... I'm just trying to decide if it's worth the effort to add that to the Hammerspoon module or not.

@asmagill Wow. Btw, will this work then?

@asmagill When we press the 'opt' key, we get a flagsChanged event with keycode 61 and flag alt and a flagsChanged event with keycode 61 without any flag.

When we use newKeyEvent to generate the opt keypress, we get a keyDown event with keycode 61 no flag and a keyUp event with keycode 61 no flag.

Ok, I was afraid of that... it was worth a shot. I have a couple of other ideas, but unfortunately they require additions to hs.eventtap.event, so it's going to have to wait until tomorrow or Saturday for me to make the additions.

@daGrevis, if I may ask so I can test while tweaking hs.eventtap... what application are you trying to send a right alt to? I can always whip up an event watcher to detect it during my tests, but it's always more effective when I can test it against another application that more closely resembles real-world use. Thanks!

@asmagill

Sorry for the late response.

I'm testing in iTerm2, Terminal and Chrome. Pressing right command key should send the same keycodes as right option.

If you change your keyboard input source to "Latvian" and press e while holding right option, it should input the letter. Remapped right command + e should do the same - it should inputting the letter again.

Sorry for the delay in getting back to this -- I am working on an addition to eventtap that allows creating raw events of any type, but it has proven to be a little more difficult than originally expected, since some event properties _must_ be created and set through NSEvent rather than CGEvent (which is what the bulk of the module is based on)... I hope to have something for testing later this week/next week, but I did want to note that it's in progress.

Hi, just wondering if there was any progress made here? I am interested in binding right-command to right-alt as well. Thanks!

Same here. I'm just interested in toggling modifiers in general, don't care which one specifically. If there is a way please let me know.

This is the purpose: https://github.com/Hammerspoon/hammerspoon/issues/1235#issuecomment-280957090

Thanks!

I want to generally discourage using Hammerspoon for key remapping. karabiner is the tool to use for that, and it should be usable enough on Sierra to swap single keys, and I think it will be able to do more complex mappings pretty soon.

@cmsj we disagree on the "pretty soon" part :)

@aristidesfl I've been watching its commits and I think tekezo is already putting in the required pieces for complex mappings :)

Sorry to dredge up an old issue, but because of stability problems with Karabiner, I'm looking for ways to do some simple modifier key remapping directly in Hammerspoon.

Would like to know if there's a reasonable way of event tapping, for example, just the right聽command key.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lazandrei19 picture lazandrei19  路  4Comments

zhenwenc picture zhenwenc  路  4Comments

jiahut picture jiahut  路  3Comments

latenitefilms picture latenitefilms  路  4Comments

latenitefilms picture latenitefilms  路  3Comments