आपको इसे स्वयं लागू करना होगा।
- शून्य से एक टाइमर प्रारंभ करें, और गलत पर शुरू में पहली प्रेस के लिए एक ध्वज सेट, जो इंगित करता पहली प्रेस अभी तक
- ऐसा नहीं होता है ध्यान से देखें और keypresses देखने के लिए: यहाँ कैसे पूरा करने के लिए इस की एक बुनियादी सारांश है यदि यह सही महत्वपूर्ण था
hs.eventtap
, विशेष रूप से hs.eventtap.event.types.keyPress
- जब घटना (
keyPress
) होता है के साथ, देखें कि क्या कुंजी दबाने सही कुंजी
- था, देखें कि क्या यह दूसरी प्रेस है और अगर यह समय में किया गया था, अगर यह समय में नहीं था या नहीं था दूसरे प्रेस बाद में सही मायने
- को वर्तमान समय और पहले ध्वज के लिए टाइमर सेट यदि यह दूसरा प्रेस था और था समय में, तो हमारे हैंडलर निष्पादित और टाइमर और पहले झंडा
- रीसेट करते हैं यह नहीं था दायां कुंजी फिर टाइमर को रीसेट करें और पहला ध्वज
कोड में अनुवादित, यह ऐसा दिख सकता है (मैं लुआ विशेषज्ञ नहीं हूं)।ध्यान दें कि ध्वजा बूलियन्स के रूप में यहाँ लागू किया जा सकता है, या के रूप में एक आंतरिक तालिका पकड़े keypresses अब तक जो आप जांच कर सकता है:
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 या कुछ अन्य मनमाने ढंग से एन प्रेस का पता लगाना चाहते हैं, तो बस एन -1 प्रेस के लिए झंडे सेट करें और कुछ चेक जोड़ें, लेकिन यह महत्वपूर्ण है कि 2 से अधिक लगातार प्रेस लेने वाले महत्वपूर्ण संयोजन हों। यह थोड़ा वर्बोज़ प्रतीत होता है, लेकिन AFAIK यह है कि आप इसे कैसे करते हैं। डुप्लिकेट कोड और बॉयलरप्लेट से बचने के लिए, इसे कक्षा-जैसी संरचना या मॉड्यूल में डालने का प्रयास करें ताकि आप कोड का पुन: उपयोग कर सकें।
2 लगातार प्रेस, या 3 लगातार प्रेस के लिए एक अलग हैंडलर निष्पादित करने के लिए, यह थोड़ा और हैकी होगा क्योंकि आपको यह जानने से पहले पूरे समय सीमा का इंतजार करना होगा कि उपयोगकर्ता कौन सा हैंडलर निष्पादित करने के लिए फिर से दबाएगा । इससे थोड़ी देर हो सकती है और खराब उपयोगकर्ता अनुभव होगा, मैं इसके खिलाफ सुझाव दूंगा, हालांकि आप शायद कोड को दोबारा सुधारकर और कुछ और जांच कर सकते हैं जैसे कि यह समय सीमा है और पहला ध्वज ट्रिगर किया गया था, फिर हैंडलर निष्पादित करें एक प्रेस के लिए।
आप एक और दो प्रेस के बीच कैसे अंतर करेंगे? – Li357
@AndrewLi मैंने प्रश्न अपडेट किया है, उम्मीद है कि यह स्पष्ट हो जाएगा –
निश्चित रूप से कंप्यूटर एक 1 प्रेस और तीन प्रेस के बीच कैसे अंतर करेगा? कंप्यूटर को कैसे पता चलेगा कि मैं 1 या दो बार बार प्रेस करने जा रहा हूं? – Li357