当用Hammerspoon将超键/大写锁定重新映射为F18时,是否可以将其与修饰符一起使用

Dou*_*ith 5 hammerspoon

我的问题如下:我使用Karabiner Elements将大写锁定重新映射到F18,然后Hammerspoon使用F18作为“超级键”来执行应用程序特定的快捷方式。

我当前的代码如下所示:

-- 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)
Run Code Online (Sandbox Code Playgroud)

当使用超级键使Vim像导航一样时,我也将HJKJ映射到箭头键:

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)
Run Code Online (Sandbox Code Playgroud)

因此HYPER-H基本上输出左箭头键。但是我的问题是我还想HYPER-COMMAND-H输出command-left-arrow-key,因为这会将光标带到行的开头。

k:bind({'cmd'}, 'n', function() arrowKey('LEFT', {'cmd'}); end, nil, function() arrowKey('LEFT', {'cmd'}); end)
Run Code Online (Sandbox Code Playgroud)

我看起来像那样 但是,问题是HYPER-COMMAND-H有效,而COMMAND-HYPER-H 无效。如果我弄乱了修饰符的顺序(通常没关系),它将完全中断,这非常不方便。

我要如何做才能使订单无关紧要?F18不是合适的修饰键,所以我遇到了麻烦。

小智 0

编写了一个脚本,可以让我获得与创建新的超级密钥类似的效果。

我编写了一个区分左右修饰键的热键扩展。这意味着我可以绑定右命令 ( rcmd) 和 s 的组合来打开 Spotify,而左命令和 s 的组合则保留其默认行为(例如大多数应用程序中的 save 命令)。

要使用此扩展,请将脚本保存hotkeyextension.hs在与 相同的目录中init.hs,然后包含以下行以将其导入require 'hotkeyextension'

然后您可以按如下方式使用它:

hs.hotkeyextension.bind({'rcmd'}, 's', launchOrFocus('Spotify'))
Run Code Online (Sandbox Code Playgroud)

的函数签名hs.hotkeyextension.bind可以位于此处