Lua 在我的Hammerspoon脚本中,重复键被延迟

Lua 在我的Hammerspoon脚本中,重复键被延迟,lua,hammerspoon,Lua,Hammerspoon,我将我的CAPSLOCK绑定到F18(卡拉宾)以用作修改键。我试图模拟CAPSLOCK+h,j,k,l来充当VIM移动键。一切正常,但重复时存在延迟问题。也就是说,当我按下CAPSLOCK+h时,速度非常慢,这应该模拟按下“我也有类似的迟钝。看起来在最新版本中引入了一些缓慢性。您可以使用下面的fastKeyStroke函数调用较低级别的函数。我已经包含了我的hjkl实现,因此您可以看到它的使用情况。还要注意,我正在将5个参数传递到for key repeating中指定的hs.hotkey.bi

我将我的CAPSLOCK绑定到F18(卡拉宾)以用作修改键。我试图模拟CAPSLOCK+h,j,k,l来充当VIM移动键。一切正常,但重复时存在延迟问题。也就是说,当我按下CAPSLOCK+h时,速度非常慢,这应该模拟按下“我也有类似的迟钝。看起来在最新版本中引入了一些缓慢性。您可以使用下面的
fastKeyStroke
函数调用较低级别的函数。我已经包含了我的
hjkl
实现,因此您可以看到它的使用情况。还要注意,我正在将5个参数传递到for key repeating中指定的
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'},  -- beginning of line
  { key='p', mod={'cmd'}, direction='right'}, -- end of line
  { key='m', mod={'alt'}, direction='left'},  -- back word
  { key='.', mod={'alt'}, direction='right'}, -- forward word
}, function(hotkey)
  hs.hotkey.bind(hyper, hotkey.key, 
      function() fastKeyStroke(hotkey.mod, hotkey.direction) end,
      nil,
      function() fastKeyStroke(hotkey.mod, hotkey.direction) end
    )
  end
)

关于慢度

是的,这是目前的正确答案-hs.eventtap.keyStroke()实际上是同一个newKeyEvent()对,中间有一个hs.timer.usleep()。我们增加了短暂的睡眠,试图避免关键事件的无序发生。下一个版本将添加一个可配置的延迟,因此您可以在需要时将其关闭:)
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'},  -- beginning of line
  { key='p', mod={'cmd'}, direction='right'}, -- end of line
  { key='m', mod={'alt'}, direction='left'},  -- back word
  { key='.', mod={'alt'}, direction='right'}, -- forward word
}, function(hotkey)
  hs.hotkey.bind(hyper, hotkey.key, 
      function() fastKeyStroke(hotkey.mod, hotkey.direction) end,
      nil,
      function() fastKeyStroke(hotkey.mod, hotkey.direction) end
    )
  end
)