I'm sure there's a better way to do this but I cant find itĮvent:setType(hs. The user data byte check is to prevent the event handler from triggering itself (SEE PressKey FUNCTION ABOVE) If event:getKe圜ode() = testKe圜ode and event:getProperty(hs.) = 0 then - Check if keycode is the shortcut keycode and check if the user data byte is set to 0 (default) RANDOM NOTE: Also turns out all functions in lua are anonymous which is pretty interesting, the interpreter just converts the structure "function foo(x) return 2*x end" into "foo = function (x) return 2*x end" FOR BEGINNERS CONTINUED: An anonymous function is just a function without an identifier (name), instead they're objects and often behave kinda like variables (look this up, it's kinda hard to explain and not relevant here) FOR BEGINNERS: "function(event)" creates anonymous function containing the below code and passes it the keydown event as an object called "event" (Just makes the code neater, you could use a separate function if you want) 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Įventtap.new(, function(event) Return condsSinceEpoch() - time < timeFrame -if the time passed from the first press to the second was less than the timeframe, then it was in time Return ke圜ode = key -return if ke圜ode is keyįunction inTime(time) -checks if the second press was in time Hs.alert("Pressed ` twice!") -the handler for the double pressįunction correctKe圜hecker(event) -keypress validator, checks if the keycode matches the key we're trying to detect print(keycodes.map) you can look up the certain keycode by accessing the map Key = 50 -the specific keycode we're detecting, in this case, 50 Within the function keep a static instance variable that will keep previous key strokes. TimeFrame = 1 -this is the timeframe in which the second press should occur, in seconds Instead you can use this function: So the most straight forward programming language agnostic approach is as follows: Call your function for any key stroke. Note that the flags could be implemented as booleans here, or as an internal table holding keypresses so far which you could check: local timer = require("hs.timer") Translated into code, this is what is could look like (I'm not a Lua expert). If it wasn't the right key then reset the timer and first flag.If it was the second press and was in time, then execute our handler and reset timer and first flag.If it was the right key, check if it's the second press and if it was in time, if it wasn't in time or was not the second press then set the timer to the current time and first flag to true.When the event ( keyPress) happens, check if the key pressed was the correct key.Observe and watch keypresses with hs.eventtap, specifically hs.Start a timer from zero, and set a flag for the first press initially to false, which indicates the first press has not happened yet.Here's a basic summary of how to accomplish this: I have some translate_ keys set up for certain language pairs for when this doesn’t quite work (I don’t think I’ve ever needed them).You're going to have to implement this yourself. Super useful! Usually, Google’s auto-detect feature works fine, so the translate_to_ keys are sufficient. It allows me to select some text and, with a keystroke, translate it to any of three languages using Google Translate. I live in Switzerland, and my German is far from perfect, so the PopupTranslateSelection spoon helps me a lot. Install:andUse("BetterTouchTool", ) Pop-up translation
0 Comments
Leave a Reply. |