Hammerspoon: Function used to perform a keystroke no longer working on Mojave

Created on 14 Nov 2018  路  7Comments  路  Source: Hammerspoon/hammerspoon

After I upgrade to Mojave, my code for performing keystrokes from mouse scroll events is no longer working (specifically the code that performs keystrokes).

Here's the function implementation I was using:

doKeyStroke = function(modifiers, character)
    local event = require('hs.eventtap').event
    event.newKeyEvent(modifiers, string.lower(character), true):post()
    event.newKeyEvent(modifiers, string.lower(character), false):post()
end

This gets called mostly for changing spaces and activating mission control from mouse events, like this:

-- moves left a space
function moveLeftSpace()
    doKeyStroke({'ctrl'}, 'left')
end

-- moves right a space
function moveRightSpace()
    doKeyStroke({'ctrl'}, 'right')
end

-- shows Mission Control
function showMissionControl()
    doKeyStroke({'ctrl'}, 'up')
end

Any ideas on why this would not be working anymore?

Most helpful comment

Well, I found the solution right after posting, after I struggled with this for days. Funny how that works.

From https://apple.stackexchange.com/a/291594:
I had to enable Hammerspoon under System Preferences > Security & Privacy > Privacy > Accessibility

I knew about ensuring it's enabled it under Automation (that was already set), but for some reason Accessibility wasn't set.

All 7 comments

I am having the same problem trying to switch spaces from hammerspoon.

I have also tried to switch the mission control keyboard shortcuts to a different key combinations, e.g. 'f1' for left and 'f2' for right, and it does not work either, but it gives a 'ding' sound so it indicates that some key is sent.

Workaround

-- move left
hs.osascript.applescript('tell application "System Events" to key code 123 using {control down}')

-- move right
hs.osascript.applescript('tell application "System Events" to key code 124 using {control down}')

Could you check if you can emit other things that aren't Mission Control related? (e.g. just sending a Ctrl-a to skip to the beginning of a line in a text field or something).

Other commands seems to work just fine, like

    hs.eventtap.event.newKeyEvent({'cmd'}, 'a', true):post()
    hs.eventtap.event.newKeyEvent({'cmd'}, 'a', false):post()

will select all text without any problem.

I had basically this same issue and found a fix. Here's my code that worked on High Sierra but broke on Mojave:

-- Vim shortcuts for ctrl + left and ctrl + right
local hyperSpace = {'alt'}
hs.hotkey.bind(hyperSpace, 'h', function()
    local modifiers = {'ctrl'}
    hs.eventtap.event.newKeyEvent(modifiers, 'Left', true):post()
    hs.timer.doAfter(.1, function()
        hs.eventtap.event.newKeyEvent(modifiers, 'Left', false):post()
    end)
end)

with a similar shortcut for moving a space right.

I read the docs to see if something had changed and noticed this:

-- According to Apple Documentation, the proper way to perform a keypress with modifiers is through multiple key events; for example to generate '脜', you should do the following:

hs.eventtap.event.newKeyEvent(hs.keycodes.map.shift, true):post()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, true):post()
hs.eventtap.event.newKeyEvent("a", true):post()
hs.eventtap.event.newKeyEvent("a", false):post()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, false):post()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.shift, false):post()

I modified my function to the following, which fixed the issue for me:

-- Vim shortcuts for ctrl + left and ctrl + right
local hyperSpace = {'alt'}
hs.hotkey.bind(hyperSpace, 'h', function()
    hs.eventtap.event.newKeyEvent(hs.keycodes.map.ctrl, true):post()
    hs.eventtap.event.newKeyEvent('Left', true):post()
    hs.eventtap.event.newKeyEvent('Left', false):post()
    hs.eventtap.event.newKeyEvent(hs.keycodes.map.ctrl, false):post()
end)

@connormason I liked your doKeyStroke function and rewrote it to do each key at a time, which also works for me (and it's a great utility function! thanks for sharing):

doKeyStroke = function(modifiers, character)
    if type(modifiers) == 'table' then
        local event = hs.eventtap.event

        for _, modifier in pairs(modifiers) do
            event.newKeyEvent(modifier, true):post()
        end

        event.newKeyEvent(character, true):post()
        event.newKeyEvent(character, false):post()

        for i = #modifiers, 1, -1 do
            event.newKeyEvent(modifiers[i], false):post()
        end
    end
end

-- Vim shortcuts for ctrl + left and ctrl + right
local hyperSpace = {'alt'}
hs.hotkey.bind(hyperSpace, 'h', function() doKeyStroke({'ctrl'}, 'Left') end)

does it work for you?

I have the same problem, and none of the above suggestions work for me. This started happening right after upgrading to Mojave. Hammerspoon picks up my input but it can't send any keystrokes. I've tried:

  • keyStroke/keyStrokes
  • @voldemortensen's doKeyStroke function
  • applescript
  • reinstalling Hammerspoon
    When running any hammerspoon key stroke command (keyStrokes or newKeyEvent), I get the following error in my system log for each key press:
Sender is prohibited from synthesizing events

Well, I found the solution right after posting, after I struggled with this for days. Funny how that works.

From https://apple.stackexchange.com/a/291594:
I had to enable Hammerspoon under System Preferences > Security & Privacy > Privacy > Accessibility

I knew about ensuring it's enabled it under Automation (that was already set), but for some reason Accessibility wasn't set.

@voldemortensen thanks a lot, your function did the job for me!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

reestr picture reestr  路  3Comments

iliyang picture iliyang  路  4Comments

latenitefilms picture latenitefilms  路  3Comments

fent picture fent  路  3Comments

piskov picture piskov  路  4Comments