Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua

Home Page:http://www.hammerspoon.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I make Hammerspoon press modifier keys (without any other normal key)?

NightMachinery opened this issue · comments

I was using Karabiner to map the FN key to shift+cmd+alt+ctrl (i.e., the hyper key). Karabiner has become very buggy for me on macOS Sonoma (and it wasn't stable before that either), so I am looking for an alternative.

It seems to be possible to use hdiutil to map fn to, e.g., F18:

So now I just need some way to make Hammerspoon press all the modifier keys for me when F18 is pressed down. (I use sticky keys, so I don't need anything to happen on key release.) But I can't find how to do this.

I tried the following, none of which worked:

    -- hs.eventtap.keyStroke({"cmd","alt","shift","ctrl"}, "")

    -- hs.eventtap.event.newKeyEvent("shift", true):post()
    -- hs.eventtap.event.newKeyEvent("shift", false):post()

    -- hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, true):post()
    -- hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, false):post()

    -- hs.osascript.applescript('tell application "System Events" to key code 58')

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

I have seen people use modal bindings as a workaround.

I generally don't like the modal workaround, as I would need to define hotkeys like

-- Trigger existing hyper key shortcuts
hyper_modality:bind({}, 'i', nil, function()
    hs.eventtap.keyStroke({"cmd","alt","shift","ctrl"}, 'i')
    hyper_exit()
end)

for shortcuts defined outside of Hammerspoon.

I also fear that the modal approach might slow down my keyboard input? I have heard that about using hs.eventtap.new.

Related: