luahammerspoon

How do I add a newline to hs.eventtap.keyStrokes in Hammerspoon?


I just started using Hammerspoon. I'm trying to output multiple lines of text by pressing Cmd+Shift+l .

Here is what I have tried so far :

hs.hotkey.bind({"cmd", "shift"}, "l", function()
  hs.eventtap.keyStrokes('from sklearn import metrics')
  hs.eventtap.keyStroke("return")
  hs.eventtap.keyStrokes('from sklearn.cross_validation import train_test_split')
end)

I also tried with inline "\n" and "%\n"

How can I bind a key combination to output multiple lines of text? Or, How can I send a newline character?


Solution

  • I ran into the same problem. I tried what you tried above and although it worked in many applications, it still didn't work in Chrome. I used the pasteboard (clipboard) as a workaround.

    jira_text = [[a 
    long 
    multi-line
    string]]
    
    -- Hotkey JIRA text
    hs.hotkey.bind({"cmd", "alt", "ctrl"}, "J", function ()
      hs.alert.show("Remove this message after debugging!")
      --hs.eventtap.keyStrokes(jira_text)#don't do this!
      hs.pasteboard.writeObjects(jira_text)
      hs.eventtap.keyStroke("cmd", "v")
      end)
    --
    

    You could improve it further by using a custom named pasteboard so it doesn't overwrite your clipboard contents (if you want that).