Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Side Specific Binding Limitation #1

Open
overvale opened this issue Sep 28, 2020 · 6 comments
Open

Side Specific Binding Limitation #1

overvale opened this issue Sep 28, 2020 · 6 comments

Comments

@overvale
Copy link

Hey! Using (and loving) your side-specific code to help my finger pain. It seems I've run up against a limit, and am wondering if there's a workaround, or if it's unavoidable.

I can't remap a key + modifier to the same key with another modifier. So this doesn't work:

SIDE_SPECIFIC_HOTKEYS = {
  {'rightCmd', 'a', 'ctrl', 'a'},
}
@elliotwaite
Copy link
Owner

Hey, sorry for the delay in reply. Yeah, for some reason the normal hotkey method doesn't work for mapping hotkeys to a modified version of that same key, but you can work around it by using an eventtap:

keyDownWatcher = hs.eventtap.new({hs.eventtap.event.types.keyDown}, function(event)
  -- Maps rightCmd+a -> ctrl+a. The key code for rightCmd is 54.
  if modStates[54] and event:getKeyCode() == hs.keycodes.map['a'] then
    return true, hs.eventtap.keyStroke('ctrl', 'a', 0)
  end
end):start()

The 54 value is the key code for rightCmd, but if you want to create mappings from other modifier keys, you can find their values in the init.lua file here: https://github.com/elliotwaite/hammerspoon-config/blob/master/init.lua#L30

And if you wanted to require that 2 of modifier keys are pressed, you could and them together, for example, this would detect rightCmd+rightShift+a (the key code for rightShift is 60):

if modStates[54] and modStates[60] and event:getKeyCode() == hs.keycodes.map['a'] then

The return true, hs.eventtap.keyStroke('ctrl', 'a', 0) part returns two values, the first value (true) means that the current event (the 'a' key down event) should be deleted and not propagated, and the second value (hs.eventtap.keyStroke('ctrl', 'a', 0)) is the new event that should be posted, with the 0 value as the third argument meaning that there should be zero delay time between the key down and key up events for that keystroke.

And for when you don't care that it is a side-specific hotkey, you could do something like this, which will get trigger by cmd+a (either cmd) and doesn't depend on the modStates value (which is created and updated by the side-specific code in my init.lua file):

keyDownWatcher = hs.eventtap.new({hs.eventtap.event.types.keyDown}, function(event)
    if event:getFlags():containExactly({'cmd'}) and event:getKeyCode() == hs.keycodes.map['a'] then
        return true, hs.eventtap.keyStroke('ctrl', 'a', 0)
    end
end):start()

Let me know if this solves the issue for you.

Also, thanks for pointing this out. I've added a note about this to the README and comment in the code in case others come across this same issue.

@overvale
Copy link
Author

Thanks for the detailed write up! It still boggles my mind what hammerspoon can do.

Your code works for me with the caveat that the new key-stroke isn't performed until one of the modifiers is released.

@elliotwaite
Copy link
Owner

Strange. For me, it fires the new keystroke on the key down event. There might be a way to fix this for your setup, but I'm not sure how, and since I'm not experiencing that issue, it's hard for me to debug it. But I'll leave this issue open and maybe someone else will be able to suggest a solution.

@overvale
Copy link
Author

I tried removing everything except this from my config and still got the same behavior, so I don't think it's a conflict with my config. Thanks for taking a look.

@elliotwaite
Copy link
Owner

Darn, ok.

@orkhan10
Copy link

orkhan10 commented Mar 22, 2021

Thanks for the detailed write up! It still boggles my mind what hammerspoon can do.

Your code works for me with the caveat that the new key-stroke isn't performed until one of the modifiers is released.

I can confirm that it is the same behaviour with me.

Though, for 'ctrl + a' it is not performed until one of the modifier is released, 'shift+left' is performed immediately.
I know it does not solve the problem, just found it interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants