2016-12-06 121 views
3

我有我的CAPSLOCK绑定到F18(karabiner)充当修饰键。我试图模拟CAPSLOCK + h,j,k,l作为VIM运动键。一切正常,但重复时有延迟问题。也就是说,当我按下CAPSLOCK + h时,它会非常缓慢,它应该模拟重复按下“< - ”键,但它非常延迟,并且每秒只发送一次。关于为什么发生这种情况的任何想法我的init.lua低于:关键重复在我的Hammerspoon脚本中被延迟

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

-- Enter Hyper Mode when F18 (Hyper/Capslock) is pressed 
pressedF18 = function() 
    k.triggered = false 
    k.modifier = false 
    k:enter() 

    trigger_modifier = function() 
    k.modifier = true 
    end 

    -- Only trigger as modifier if held longer than thisj 
    hs.timer.doAfter(0.35, trigger_modifier) 
end 

-- Arrow keys 
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) 

-- 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 
    -- If hotkey held longer than this amount of time 
    -- let it remain as modifier and don't send ESCAPE 
    if not k.modifier then 
     hs.eventtap.keyStroke({}, 'ESCAPE') 
    else 
     print("Modifier detected") 
    end 
    end 
end 

-- Bind the Hyper key 
f18 = hs.hotkey.bind({}, 'F18', pressedF18, releasedF18) 

回答

4

我有一些类似的缓慢。看起来在最新版本中引入了一些缓慢。您可以使用下面的fastKeyStroke函数调用较低级别的函数。我已经包含了我的hjkl实现,所以你可以看到它的使用。另请注意,我将docs中指定的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'}, -- 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 
) 

Source约缓慢

+2

是的,这是目前正确的答案 - hs.eventtap.keyStroke()确实是一样的newKeyEvent()对,用hs.timer.usleep()插图中。我们添加了简短的睡眠以尝试避免重要事件不按顺序到达。下一个版本会添加一个可配置的延迟,所以如果您想要,可以关闭它:) –

+0

非常好,感谢info @ChrisJones –