绑定到多个按钮点击
问题描述:
绑定到我用的是键:绑定到多个按钮点击
hs.hotkey.bind(hyper, '1'
如何绑定到关键的印刷机多台?例如:
hs.hotkey.bind(hyper, '1+1'
读the documentation,这个功能没有提到。
通过多次按下我的意思是按两次1
运行一些代码,然后按1
三次运行一段单独的代码。
答
你将不得不自己实现这个。下面是如何做到这一点的基本摘要:
- 从零启动计时器,并初步建立起了第一次按下标志设置为false,表示第一次记者还没有发生
- 观察,看按键与
hs.eventtap
,特别hs.eventtap.event.types.keyPress
- 当事件(
keyPress
)发生时,检查按下的键是正确的密钥 - 如果这是正确的钥匙,检查它是否是第二次按下,如果这是在时间上,如果它不及时或不是然后第二次按下计时器当前的时间和第一标志设置为true
- 如果是第二次按下和在时间,然后执行我们的处理程序和复位定时器和第一标志
- 如果不是右键然后重置定时器和第一个标志
转换成代码,这是什么可能看起来像(我不是一个Lua专家)。请注意,标志可以以布尔这里来实现,或者作为内部表格保存按键到目前为止,你可以检查:
local timer = require("hs.timer")
local eventtap = require("hs.eventtap")
local keycodes = require("hs.keycodes")
local events = eventtap.event.types --all the event types
timeFrame = 1 --this is the timeframe in which the second press should occur, in seconds
key = 50 --the specific keycode we're detecting, in this case, 50
--print(keycodes.map["`"]) you can look up the certain keycode by accessing the map
function twoHandler()
hs.alert("Pressed ` twice!") --the handler for the double press
end
function correctKeyChecker(event) --keypress validator, checks if the keycode matches the key we're trying to detect
local keyCode = event:getKeyCode()
return keyCode == key --return if keyCode is key
end
function inTime(time) --checks if the second press was in time
return timer.secondsSinceEpoch() - time < timeFrame --if the time passed from the first press to the second was less than the timeframe, then it was in time
end
local pressTime, firstDown = 0, false --pressTime was the time the first press occurred which is set to 0, and firstDown indicates if the first press has occurred or not
eventtap.new({ events.keyDown }, function(event) --watch the keyDown event, trigger the function every time there is a keydown
if correctKeyChecker(event) then --if correct key
if firstDown and inTime(pressTime) then --if first press already happened and the second was in time
twoHandler() --execute the handler
elseif not firstDown or inTime(pressTime) then --if the first press has not happened or the second wasn't in time
pressTime, firstDown = timer.secondsSinceEpoch(), true --set first press time to now and first press to true
return false --stop prematurely
end
end
pressTime, firstDown = 0, false --if it reaches here that means the double tap was successful or the key was incorrect, thus reset timer and flag
return false --keeps the event propogating
end):start() --start our watcher
我评论为更好地理解代码行由行。如果您想要检测3或4或其他任意N个印刷机,只需设置N-1个印刷机的标志并添加一些检查,但使用多于两次连续印刷机的组合键的情况并不常见。它似乎有点冗长,但AFAIK这是你如何做到这一点。为了避免重复的代码和样板文件,请尝试将它放在类结构或模块中,以便重用代码。
至于为2次连续的按键或3次连续的按键执行不同的处理程序,这将会更加棘手,因为在知道用户是否再次按下以知道执行哪个处理程序之前,您必须等待整个时间范围。这会导致轻微的延迟和不良的用户体验,我会建议您不要这样做,尽管您可以通过重构代码并执行一些更多检查来实现这一点,例如时间范围和第一个标志被触发,然后执行处理程序一次印刷。
答
您无法绑定所有密钥或多个密钥。相反,您可以使用此功能:http://www.hammerspoon.org/docs/hs.eventtap.html#keyStroke
所以最简单的编程语言无关的方法如下:
- 调用函数的任意键。
- 在该函数中保留一个静态实例变量,该变量将保持以前的键击。
- 作为函数的第一个任务,将新来的字符添加到该变量中。
- 如果它们是所需的“11”字符串,请检查最后3个字符。
额外的极端条件:
- 如果可变长度通过某一点将其降低到长度为1,以便它不保留不需要的地方在存储器中。
你会如何区分一台和两台印刷机? – Li357
@AndrewLi我更新了问题,希望这个更清晰 –
当然,但计算机将如何区分一台压机和三台压机?计算机如何知道我是否要再按一次或两次? – Li357