Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active January 9, 2024 23:44
Show Gist options
  • Star 70 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ttscoff/cce98a711b5476166792d5e6f1ac5907 to your computer and use it in GitHub Desktop.
Save ttscoff/cce98a711b5476166792d5e6f1ac5907 to your computer and use it in GitHub Desktop.
Hammerspoon config examples for hyper key
-- A global variable for the Hyper Mode
k = hs.hotkey.modal.new({}, "F17")
-- Trigger existing hyper key shortcuts
k:bind({}, 'm', nil, function() hs.eventtap.keyStroke({"cmd","alt","shift","ctrl"}, 'm') end)
-- OR build your own
launch = function(appname)
hs.application.launchOrFocus(appname)
k.triggered = true
end
-- Single keybinding for app launch
singleapps = {
{'q', 'MailMate'},
{'w', 'OmniFocus'},
{'e', 'Sublime Text'},
{'r', 'Google Chrome'}
}
for i, app in ipairs(singleapps) do
k:bind({}, app[1], function() launch(app[2]); k:exit(); end)
end
-- Sequential keybindings, e.g. Hyper-a,f for Finder
a = hs.hotkey.modal.new({}, "F16")
apps = {
{'d', 'Twitter'},
{'f', 'Finder'},
{'s', 'Skype'},
}
for i, app in ipairs(apps) do
a:bind({}, app[1], function() launch(app[2]); a:exit(); end)
end
pressedA = function() a:enter() end
releasedA = function() end
k:bind({}, 'a', nil, pressedA, releasedA)
-- Shortcut to reload config
ofun = function()
hs.reload()
hs.alert.show("Config loaded")
k.triggered = true
end
k:bind({}, 'o', nil, ofun)
-- Enter Hyper Mode when F18 (Hyper/Capslock) is pressed
pressedF18 = function()
k.triggered = false
k:enter()
end
-- Leave Hyper Mode when F18 (Hyper/Capslock) is pressed,
-- send ESCAPE if no other keys are pressed.
releasedF18 = function()
k:exit()
if not k.triggered then
hs.eventtap.keyStroke({}, 'ESCAPE')
end
end
-- Bind the Hyper key
f18 = hs.hotkey.bind({}, 'F18', pressedF18, releasedF18)
-- Cursor locator
local mouseCircle = nil
local mouseCircleTimer = nil
function mouseHighlight()
size = 150
-- Delete an existing highlight if it exists
if mouseCircle then
mouseCircle:delete()
mouseCircle2:delete()
if mouseCircleTimer then
mouseCircleTimer:stop()
end
end
-- Get the current co-ordinates of the mouse pointer
mousepoint = hs.mouse.getAbsolutePosition()
-- Prepare a big red circle around the mouse pointer
mouseCircle = hs.drawing.circle(hs.geometry.rect(mousepoint.x-(size/2), mousepoint.y-(size/2), size, size))
mouseCircle2 = hs.drawing.circle(hs.geometry.rect(mousepoint.x-(size/4), mousepoint.y-(size/4), size/2, size/2))
mouseCircle:setStrokeColor({["red"]=1,["blue"]=0,["green"]=0,["alpha"]=1})
mouseCircle2:setStrokeColor({["red"]=1,["blue"]=0,["green"]=0,["alpha"]=1})
mouseCircle:setFill(false)
mouseCircle2:setFill(false)
mouseCircle:setStrokeWidth(3)
mouseCircle2:setStrokeWidth(5)
mouseCircle:show()
mouseCircle2:show()
-- Set a timer to delete the circle after 3 seconds
mouseCircleTimer = hs.timer.doAfter(1, function() mouseCircle:delete() mouseCircle2:delete() end)
end
hs.hotkey.bind({"cmd","alt","shift"}, "D", mouseHighlight)
-- HYPER+L: Open news.google.com in the default browser
-- lfun = function()
-- news = "app = Application.currentApplication(); app.includeStandardAdditions = true; app.doShellScript('open http://news.google.com')"
-- hs.osascript.javascript(news)
-- k.triggered = true
-- end
-- k:bind('', 'l', nil, lfun)
@prenagha
Copy link

so funny I spent the day working on the same thing.
Like your array setting approach here
Here is my somewhat similar config https://gist.github.com/prenagha/1c28f71cb4d52b3133a4bff1b3849c3e

@dalemanthei
Copy link

@ttscoff @prenagha Thank you both. It took a bit to work out the key.triggered = true. For people following the path, here's my simple gist for all my keys mapped in applications other than Keyboard Maestro https://gist.github.com/dalemanthei/dde8bccb22c3a2d3487a6e7d77be33f5

@vbauerster
Copy link

Yet another working hyper/esc config

@clickysteve
Copy link

Thank you for your work on this! I forked and uploaded my tweaked code for my own setup here:

https://gist.github.com/clickysteve/e13a11b8fc9c963c08b3109d95bbacc5

Gave attribution but not sure of the correct etiquette, so let me know if anything should be revised.

@vviikk
Copy link

vviikk commented Nov 1, 2016

Still can't seem to get this to work in Sierra. Disabled Capslock in Sys Preferences > Keyboard. Then installed Karabiner.

Have this in my ~/.karabiner.d/configuration/karabiner.json file:

{
    "profiles": [
        {
            "name": "Default profile",
            "selected": true,
            "simple_modifications": {
                "caps_lock": "f18"
            }
        }
    ]
}

Still no dice. Do I need Seal?

@Behinder
Copy link

I don't get it you have bind f18 key in karabiner.json but in this gist Hyper is bind do F17

@Behinder
Copy link

I decided to delete Hammerspoon and stay with caps-escape binding.
mapping shortcuts in Hammerspoon not work properly because at every capslock release escape is triggered which makes some of my shortcuts unreliable

@andrew-kennedy
Copy link

I'm having a problem with this config, I cannot do multiple keypresses with the hyper key. I made my hyper just press CTRL along with another key, so that I can use Caps Lock as both control and escape. However, I can only press one CTRL chord before having to release the key and press it again for another chord, and it sends ESC in between, which is very inconvenient.

@davidolrik
Copy link

There is missing a k:exit(); for the existing hyper+key shortcuts.

- k:bind({}, 'm', nil, function() hs.eventtap.keyStroke({"cmd","alt","shift","ctrl"}, 'm') end)
+ k:bind({}, 'm', nil, function() k:exit(); hs.eventtap.keyStroke({"cmd","alt","shift","ctrl"}, 'm') end)

If the k:exit(); is absent the modifiers will affect the action you trigger.

E.g. I use it to trigger KeyboardMaestros "Insert Text by Typing" action, and without the k:exit(); any character on the pasteboard that matches a bound shortcut will fire.

@skehlet
Copy link

skehlet commented Apr 25, 2017

Thanks for this, very helpful. One comment: maybe it's just me, but I found the use of F17 on k (and the F16 on a) confusing as a new user. I was puzzling over what these new F keys were for, and wondering why they were necessary since we bound Caps lock to F18. After playing around with it, it seems you can just create the modal object without any associated keybinding:

k = hs.hotkey.modal.new()

and then, as you do later on, bind F18 to enter/exit the modal on key down/up.

If anyone is interested in looking over a very simple version (that also doesn't involve the Escape key), check out my config.

@carrigmore
Copy link

Hi all,
I am using a compact keyboard, the numpad won't work correctly until set Numlock on.
In previous Karabiner version, I can use following settings to achieve this:
KeyToKey KeyCode::KEYPAD_CLEAR, KeyCode::VK_IOHIKEYBOARD_TOGGLE_NUMLOCK

This selection VK_IOHIKEYBOARD_TOGGLE_NUMLOCK doesn't seem to exist in the Karabiner-Elements.

Do you know how to enable this mapping in this elements version? this feature is really useful for users using compact keyboard, as the num pad is disabled by default, without correct mapping, the number pad cannot be used properly.

I find user can edit the JSON file (same as we did in private.xml in old version of Karabiner, but for some reason cannot map a key to windows locking_num_lock. As it has "not_to" : true

{
"name": "locking_num_lock",
"not_to": true
},

Thanks a lot.

@oolugh
Copy link

oolugh commented May 12, 2017

Recently I found a new app called SpaceLauncher. It uses spacebar as a hyper key.
I've played with space launcher for a short time and it looks like a great app. The spacebar is so comfortable to type. Now it has almost replaced my Hammerspoon config.

@whiterock
Copy link

Thanks for this, very helpful. One comment: maybe it's just me, but I found the use of F17 on k (and the F16 on a) confusing as a new user. I was puzzling over what these new F keys were for, and wondering why they were necessary since we bound Caps lock to F18. After playing around with it, it seems you can just create the modal object without any associated keybinding:

k = hs.hotkey.modal.new()

and then, as you do later on, bind F18 to enter/exit the modal on key down/up.

If anyone is interested in looking over a very simple version (that also doesn't involve the Escape key), check out my config.

thanks! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment