When remapping hyper key/caps lock to F18 with Hammerspoon, is it possible to use it with modifiers
Asked Answered
K

1

7

My issue is as follows: I have my caps lock remapped to F18 using Karabiner Elements, and then Hammerspoon uses F18 as a "hyper key" to perform app specific shortcuts.

My current code looks like this:

-- A global variable for the Hyper Mode
k = hs.hotkey.modal.new({}, "F17")

launch = function(bundleID)
  hs.application.launchOrFocusByBundleID(bundleID)
  k.triggered = true
end

-- Single keybinding for app launch
singleapps = {
  {'f', 'com.apple.Finder'},
  {'c', 'com.google.Chrome'},
}

for i, app in ipairs(singleapps) do
  k:bind({}, app[1], function() launch(app[2]); end, nil, function() launch(app[2]); end)

I also have HJKJ mapped to arrow keys when using the hyper key to have vim like navigation everywhere:

arrowKey = function(arrow, modifiers) 
  local event = require("hs.eventtap").event
  event.newKeyEvent(modifiers, string.lower(arrow), true):post()
  event.newKeyEvent(modifiers, string.lower(arrow), false):post()
end

k:bind({}, 'h', function() arrowKey('LEFT', {}); end, nil, function() arrowKey('LEFT', {}); end)
k:bind({}, 'j', function() arrowKey('DOWN', {}); end, nil, function() arrowKey('DOWN', {}); end)
k:bind({}, 'k', function() arrowKey('UP', {}); end, nil, function() arrowKey('UP', {}); end)
k:bind({}, 'l', function() arrowKey('RIGHT', {}); end, nil, function() arrowKey('RIGHT', {}); end)

So HYPER-H outputs the left arrow key basically. But my issue is that I also want HYPER-COMMAND-H to output command-left-arrow-key as that brings the cursor to the beginning of the line.

k:bind({'cmd'}, 'n', function() arrowKey('LEFT', {'cmd'}); end, nil, function() arrowKey('LEFT', {'cmd'}); end)

I have it looking like that. However, the issue is that HYPER-COMMAND-H works, but COMMAND-HYPER-H does not. If I mess up the order of the modifiers (which normally does not matter) it completely breaks, which is super inconvenient.

How would I make it so that the order doesn't matter? F18 isn't a proper modifier key so I'm having real trouble.

Karakalpak answered 7/12, 2016 at 19:4 Comment(0)
I
0

I have written a script that allows me to attain a similar effect as creating a new hyper key.

I have written a hotkey extension that distinguishes between left and right modifier keys. This means that I can bind the combination of right command (rcmd) and s to open Spotify, while the combination of left command and s retains its default behavior (such as the save command in most applications).

To use this extension, save the script hotkeyextension.hs in the same directory as init.hs, and then include the following line to import it require 'hotkeyextension'.

You can then use it as follows:

hs.hotkeyextension.bind({'rcmd'}, 's', launchOrFocus('Spotify'))

The function signature for hs.hotkeyextension.bind can be located here.

Interoceptor answered 1/1, 2023 at 14:14 Comment(1)
The link is broken, the correct link is this.Missis

© 2022 - 2024 — McMap. All rights reserved.