Hammerspoon: Is the underscore key event lost?

Created on 16 Jan 2020  路  3Comments  路  Source: Hammerspoon/hammerspoon

hs.eventtap.event.newKeyEvent('_', true):post()
hs.eventtap.event.newKeyEvent(94, true):post()

I'm trying to write a automation script with the marvelous hammerspoon for the first time.
I found that I cann't fire an underscore key event, namely the _ char, and the virtual code didn't work either. I cann't find it in hs.keycodes.map or anywhere else. Other characters like hyphen - and arrow key left work. By the way, I intend to add the underscore as prefix for the name field of the SAVE AS modal dialog of PHOTOSHOP.

Source code: https://gist.github.com/BigSully/4eb3bf7e4a989d678fca9fdb69c5e93e

All 3 comments

The underscore doesn't map to a specific key (at least on a US keyboard layout, not sure about other nationalities), but rather is the combination of the shift key and the - key.

Try something like:

hs.eventtap.event.newKeyEvent({"shift"}, '-', true):post()

@asmagill It works, and I just wait some time after first several trial. While another wired thing happens.
If I use hyphen directly, everything goes well.
If I use newKeyEvent({"shift"}, '-', true), then the key events is out of order.
Although I can manipulate the content via pasteboard, I'm wondering whether it's possible to emit several key events in order?

I figure out the workable solution, namely split the shift + - into 4 consecutive key events, emulating the real event flow:

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

Was this page helpful?
0 / 5 - 0 ratings