我的 Hammerspoon 脚本中按键重复延迟的问题

我把 CAPSLOCK 键绑定为 F18(通过 Karabiner),将其作为修改键。我试图模拟 CAPSLOCK+h、j、k、l 来作为 VIM 的移动键。一切都正常,但重复时存在延迟问题。也就是说,当我按下 CAPSLOCK+h 来模拟重复按下“<-”键时,非常缓慢,每秒只发送一个键。有任何想法是出于何种原因?下面是我的 init.lua 文件:

-- 用于 Hyper 模式的全局变量
k = hs.hotkey.modal.new({}, "F17")

-- 当按下 F18(Hyper/Capslock)时进入 Hyper 模式
pressedF18 = function()
  k.triggered = false
  k.modifier = false
  k:enter()

  trigger_modifier = function()
    k.modifier = true
  end

  -- 只有长按超过 0.35 秒才触发修改键
  hs.timer.doAfter(0.35, trigger_modifier)
end

-- 箭头键
k:bind({}, 'h', function()
  hs.eventtap.keyStroke({}, 'Left')
  k.triggered = true
end)

k:bind({}, 'j', function()
  hs.eventtap.keyStroke({}, 'Down')
  k.triggered = true
end)

k:bind({}, 'k', function()
  hs.eventtap.keyStroke({}, 'Up')
  k.triggered = true
end)

k:bind({}, 'l', function()
  hs.eventtap.keyStroke({}, 'Right')
  k.triggered = true
end)

-- 当按下 F18(Hyper/Capslock)时退出 Hyper 模式,
--   若没有按其他键,则发送 ESCAPE。
releasedF18 = function()
  k:exit()

  if not k.triggered then
    -- 若长按时间超过该时间
    -- 就把按键作为修改键保留,不发送 ESCAPE
    if not k.modifier then
      hs.eventtap.keyStroke({}, 'ESCAPE')
    else
      print("检测到修改键")
    end
  end
end

-- 绑定 Hyper 键
f18 = hs.hotkey.bind({}, 'F18', pressedF18, releasedF18)
点赞
用户1717697
用户1717697

我遇到了一些类似的缓慢问题。看起来在最新版本中引入了一些缓慢的问题。您可以使用下面的fastKeyStroke函数调用低级函数。我包含了我的hjkl实现,让您可以看到它的使用。还要注意,我按照文档中指定的按键重复将5个参数传递到hs.hotkey.bind中。

local hyper = {"shift", "cmd", "alt", "ctrl"}

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

hs.fnutils.each({
  -- Movement
  { key='h', mod={}, direction='left'},
  { key='j', mod={}, direction='down'},
  { key='k', mod={}, direction='up'},
  { key='l', mod={}, direction='right'},
  { key='n', mod={'cmd'}, direction='left'},  -- line的开头
  { key='p', mod={'cmd'}, direction='right'}, -- line的结尾
  { key='m', mod={'alt'}, direction='left'},  -- 单词的后退
  { key='.', mod={'alt'}, direction='right'}, -- 单词的前进
}, function(hotkey)
  hs.hotkey.bind(hyper, hotkey.key,
      function() fastKeyStroke(hotkey.mod, hotkey.direction) end,
      nil,
      function() fastKeyStroke(hotkey.mod, hotkey.direction) end
    )
  end
)

【来源】(https://github.com/Hammerspoon/hammerspoon/issues/1011#issuecomment-261114434)有关缓慢问题

2016-12-06 03:09:17