Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 485
Tovio Roberts Avatar asked Mar 29 '18 13:03

Tovio Roberts


People also ask

Is it possible to make a hyper key chord in hammerspoon?

You can absolutely do this in Hammerspoon if you want. While it works well, it has its limitations. Using the "hyper chord" as the entire "hyper key", you can't add any more modifiers, because it is already all the modifiers. Because of this a lot of hyper key setups are limited to "leader key" style interactions.

How do I set up local bindings in hammerspoon?

If you set any local_binding key for the configuration, hyper.lua will intercept the F19+<key> keypress and instead send the traditional ⌘⇧⌥⌃+<key> to the operating system… just like the Traditional model. First, set up the local bindings to the app in Hammerspoon. Then, use your new local bindings in the preference pane of the app of your choice.

How to add a hyper key to the keyboard?

Traditionally, a Hyper key is implemented by sending to the Operating System "hyper chord" of ⌘⌥⇧⌃ by modifying the keyboard firmware or using Karabiner-elements.app. The user would then use some kind of automation software like Alfred or Keyboard Maestro to listen for the "hyper chord" and fire different automations.


2 Answers

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).

like image 199
Josh Fox Avatar answered Nov 22 '22 20:11

Josh Fox


I also ran into this problem and improved Josh Fox's answer by saving the contents of the system pasteboard into a temporary pasteboard before loading and pasting the multi-line string.

MULTILINE_STRING = [[multi
line
string]]

-- Paste Multi-line String
hs.hotkey.bind({'ctrl', 'cmd'}, 'F1', function()
    -- save clipboard data to temp
    tempClipboard = hs.pasteboard.uniquePasteboard()
    hs.pasteboard.writeAllData(tempClipboard, hs.pasteboard.readAllData(nil))
    
    -- load string into clipboard and paste
    hs.pasteboard.writeObjects(MULTILINE_STRING)
    hs.eventtap.keyStroke({'cmd'}, 'v')

    -- recall clipboard data
    hs.pasteboard.writeAllData(nil, hs.pasteboard.readAllData(tempClipboard))
    hs.pasteboard.deletePasteboard(tempClipboard)
end)
like image 45
BLiu1 Avatar answered Nov 22 '22 19:11

BLiu1