Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.16k stars 587 forks source link

Is it possible to show what a keycode being pressed is? #2937

Open vaterp opened 3 years ago

vaterp commented 3 years ago

More specifically I have a MS ERGO keyboard on a mac and I want to try and use hammerspoon to make some of the special keys work in the top row (the 1,2,3,4,5, calc, home, mail buttons).

My problem is that I dont know how to figure out what thier keycode names would be and if its even possible?

Thanks.

jparoz commented 3 years ago

One approach that I take when trying to trigger things that I'm not sure are captured by Hammerspoon is to do something like this snippet:

hs.eventtap.new({hs.eventtap.event.types.keyDown, hs.eventtap.event.types.systemDefined}, function(event)
    local type = event:getType()
    if type == hs.eventtap.event.types.keyDown then
        print(hs.keycodes.map[event:getKeyCode()])
    elseif type == hs.eventtap.event.types.systemDefined then
        local t = event:systemKey()
        if t.down then
            print("System key: " .. t.key)
        end
    end
end):start()

This will print the keycode of any keyboard key pressed. If pressing the key you'd like to map causes its name to be printed in the console, then you know you can take actions when the key is pressed; the only step remaining is to modify the snippet to test which key is being pressed, instead of always printing its name.

The documentation for hs.eventtap.event:systemKey() might be helpful to you. I suspect that in your particular case of calculator and mail type keys, those keys might use a different USB device descriptor than standard keyboard keys, and so might be unusable for what you want to do. But there's only one way to find out for sure!