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?
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.
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.
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.
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).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With