Hammerspoon: Bind EJECT key

Created on 1 Feb 2017  路  5Comments  路  Source: Hammerspoon/hammerspoon

I'd like to bind EJECT key on macOS 10.12.2 but the key does not trigger keyDown event:

hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(event)
    print('tap!')
    return false
end):start()

Most helpful comment

Here is the code for those who stumble upon:

hs.eventtap.new({ hs.eventtap.event.types.NSSystemDefined }, function(event)
    -- http://www.hammerspoon.org/docs/hs.eventtap.event.html#systemKey
    event = event:systemKey()
    -- http://stackoverflow.com/a/1252776/1521064
    local next = next
    -- Check empty table
    if next(event) then
        if event.key == 'EJECT' and event.down then
            print('This is my EJECT key event')
        end
    end
end):start()

All 5 comments

The system keys do not generate traditional key-down events and need to be caught with the NSSystemDefined. See the help for hs.eventtap.event:systemKey as well, since you'll need this to identify which system key is being pressed.

This works for some system keys and not so well for others... it depends (I think) on what system process normally handles the system key, but I haven't deduced the pattern. I am in the process of rewriting the eventtap module so capturing some of these common cases is more like the hs.hotkey model, but for now, you'll have to do this the hard way.

Here is the code for those who stumble upon:

hs.eventtap.new({ hs.eventtap.event.types.NSSystemDefined }, function(event)
    -- http://www.hammerspoon.org/docs/hs.eventtap.event.html#systemKey
    event = event:systemKey()
    -- http://stackoverflow.com/a/1252776/1521064
    local next = next
    -- Check empty table
    if next(event) then
        if event.key == 'EJECT' and event.down then
            print('This is my EJECT key event')
        end
    end
end):start()

@seguri thanks!

(I'm going to close this out because I don't think there is any work to be done in Hammerspoon. Please re-open if you disagree!)

Here is the code for those who stumble upon:

hs.eventtap.new({ hs.eventtap.event.types.NSSystemDefined }, function(event)
    -- http://www.hammerspoon.org/docs/hs.eventtap.event.html#systemKey
    event = event:systemKey()
    -- http://stackoverflow.com/a/1252776/1521064
    local next = next
    -- Check empty table
    if next(event) then
        if event.key == 'EJECT' and event.down then
            print('This is my EJECT key event')
        end
    end
end):start()

This works, thanks. But how can I check if another key is pressed at the same time?
I want to fire an action when I press the EJECT button and 'S' simultaneously.

Was this page helpful?
0 / 5 - 0 ratings