I'm trying to use taphold to invoke a menu with one click of !+S, and jump straight to a function with a double tap of the same hotkey (not sure if relevant, but that would translate to {altdown}{shiftsown}{s 2}{altup}{shiftup})
I've tried messing around with taptime, holdtime, and the parameters in each If statement. No matter what combination, I can't get it to consistently do what I want it to do. If there's anything glaringly obvious, here's the code:
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
menu, tray, icon, % a_scriptDir "\Media\Search.ico"
#SingleInstance,Force
;#NoTrayIcon
#Include lib\TapHoldManager.ahk
;------------------------------------------------------------------------------
; Automatically reload after edit, Part 1
;------------------------------------------------------------------------------
FileGetTime ScriptStartModTime, %A_ScriptFullPath%
SetTimer CheckScriptUpdate, 200, 0x7FFFFFFF ; 200 ms, highest priority
;------------------------------------------------------------------------------
; Script
;------------------------------------------------------------------------------
/*
tapTime The amount of time after a tap occured to wait for another tap. Defaults to 150ms.
holdTime The amount of time that you need to hold a button for it to be considered a hold. Defaults to the same as tapTime.
maxTaps The maximum number of taps before the callback will be fired. Defaults to infinite.
Setting this value to 1 will force the callback to be fired after every tap, whereas by default if you tapped 3 times quickly it would fire the callback once and pass it a taps value of 3, it would now be fired 3 times with a taps value of 1. If maxTaps is 1, then the tapTime setting will have no effect.
prefix The prefix used for all hotkeys, default is $window An AHK WinTitle string that defines which windows the hotkey will take effect in For example, to make Hotkeys only work in Notepad, you could use:
*/
thm := new TapHoldManager(300,100) ; <tapTime := -1>, holdTime := -1, <maxTaps := -1>, <prefix := "$">, <window := "">
thm.Add("!+s", Func("SearchMenuHotkey"))
SearchMenuHotkey(isHold, taps, state){
if (isHold=1) & (taps=1) & (state=1){ ; state=1 means it activates when key is down.
Menu, MyMenu, Add, 1 Search using DuckDuckGo, SearchDDGHandler
Menu, MyMenu, Add, 2 Search using WolframAlpha, SearchWAHandler
Menu, MyMenu, Add, 3 Translate using Google, GoogleTranslateHandler
Menu, MyMenu, Add, 4 Correct grammar and spelling with Google, GoogleAutocorrectHandler
Menu, MyMenu, Add, 5 WolframAlpha searchbox, WASearchBoxHandler
Menu, MyMenu, Show
Return
}
if (isHold=1) & (taps=2) & (state=0){ ; state=0 means it activates when key is up.
DDGSearch()
return
}
}
/*
!+S::
Menu, MyMenu, Add, 1 Search using DuckDuckGo, SearchDDGHandler
Menu, MyMenu, Add, 2 Search using WolframAlpha, SearchWAHandler
Menu, MyMenu, Add, 3 Translate using Google, GoogleTranslateHandler
Menu, MyMenu, Add, 4 Correct grammar and spelling with Google, GoogleAutocorrectHandler
Menu, MyMenu, Add, 5 WolframAlpha searchbox, WASearchBoxHandler
Menu, MyMenu, Show
Return
*/
SearchDDGHandler:
DDGSearch()
Return
/*
MyClip := ClipboardAll
Clipboard = ; empty the clipboard
Send, ^c
ClipWait, 2
if ErrorLevel { ; ClipWait timed out.
return
}
if RegExMatch(Clipboard, "^(https?://|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$") {
Run % Clipboard
}
else {
; Modify some characters that screw up the URL
; RFC 3986 section 2.2 Reserved Characters (January 2005): !*'();:@&=+$,/?#[]
StringReplace, Clipboard, Clipboard, `r`n, %A_Space%, All
StringReplace, Clipboard, Clipboard, #, `%23, All
StringReplace, Clipboard, Clipboard, &, `%26, All
StringReplace, Clipboard, Clipboard, +, `%2b, All
StringReplace, Clipboard, Clipboard, ", `%22, All
Run % "https://duckduckgo.com/?q=" . clipboard ; uriEncode(clipboard)
}
Clipboard := MyClip
return
*/
SearchWAHandler:
WAsearch()
Return
GoogleTranslateHandler:
GoogleTranslate()
Return
GoogleAutocorrectHandler:
clipback := ClipboardAll
clipboard=
Send ^c
ClipWait, 0
UrlDownloadToFile % "https://www.google.com/search?q=" . clipboard, temp
FileRead, contents, temp
FileDelete temp
if (RegExMatch(contents, "(Including results for|Showing results for|Did you mean:)(.*?)</a>", match)) {
match2 := RegExReplace(match2,"<.+?>")
StringReplace, match2, match2, ',', All
}
; msgbox %match2%
SendInput %match2%
Sleep 500
clipboard := clipback
Return
WASearchBoxHandler:
Inputbox, WAQuery, Wolfram|Alpha search string
If ErrorLevel {
MsgBox, Search cancelled
Return
}
Else {
If RegExMatch(WAQuery, "^(https?://|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$") {
Run % WAQuery
Return
}
Else {
StringReplace, WAQuery, WAQuery, `r`n, %A_Space%, All
StringReplace, WAQuery, WAQuery, #, `%23, All
StringReplace, WAQuery, WAQuery, &, `%26, All
StringReplace, WAQuery, WAQuery, +, `%2b, All
StringReplace, WAQuery, WAQuery, ", `%22, All
Run % "https://www.wolframalpha.com/input?i=" . WAQuery
Return
}
}
Return
DDGSearch() {
MyClip := ClipboardAll
Clipboard = ; empty the clipboard
Send, ^c
ClipWait, 2
if ErrorLevel { ; ClipWait timed out.
return
}
if RegExMatch(Clipboard, "^(https?://|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$") {
Run % Clipboard
}
else {
; Modify some characters that screw up the URL
; RFC 3986 section 2.2 Reserved Characters (January 2005): !*'();:@&=+$,/?#[]
StringReplace, Clipboard, Clipboard, `r`n, %A_Space%, All
StringReplace, Clipboard, Clipboard, #, `%23, All
StringReplace, Clipboard, Clipboard, &, `%26, All
StringReplace, Clipboard, Clipboard, +, `%2b, All
StringReplace, Clipboard, Clipboard, ", `%22, All
Run % "https://duckduckgo.com/?q=" . clipboard ; uriEncode(clipboard)
}
Clipboard := MyClip
}
WAsearch() {
MyClip := ClipboardAll
Clipboard := ; empty the clipboard
Send, ^c
ClipWait, 2
if ErrorLevel { ; ClipWait timed out.
return
}
if RegExMatch(Clipboard, "^(https?://|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$")
{
Run % Clipboard
}
else { ;*[Search - SHIFT ALT S or T - Google Search & Translate]
; Modify some characters that screw up the URL
; RFC 3986 section 2.2 Reserved Characters (January 2005): !*'();:@&=+$,/?#[]
StringReplace, Clipboard, Clipboard, `r`n, %A_Space%, All
StringReplace, Clipboard, Clipboard, #, `%23, All
StringReplace, Clipboard, Clipboard, &, `%26, All
StringReplace, Clipboard, Clipboard, +, `%2b, All
StringReplace, Clipboard, Clipboard, ", `%22, All
Run % "https://www.wolframalpha.com/input?i=" . clipboard ; uriEncode(clipboard)
}
Clipboard := MyClip
return
}
GoogleTranslate() {
MyClip := ClipboardAll
Clipboard = ; empty the clipboard
Send, ^c
ClipWait, 2
if ErrorLevel { ; ClipWait timed out.
return
}
StringReplace, Clipboard, Clipboard, `%, `%25, All ; has to come first
StringReplace, Clipboard, Clipboard, `r`n, `%0A, All
Run % "https://translate.google.com/#auto|en|" . clipboard ; uriEncode(clipboard)
Clipboard := MyClip
return
}
;------------------------------------------------------------------------------
; Automatically reload after edit, Part 2
;------------------------------------------------------------------------------
CheckScriptUpdate() {
global ScriptStartModTime
FileGetTime curModTime, %A_ScriptFullPath%
If (curModTime <> ScriptStartModTime) {
SetTimer CheckScriptUpdate, Off
Loop
{
reload
Sleep 300 ; ms
MsgBox 0x2, %A_ScriptName%, Reload failed. ; 0x2 = Abort/Retry/Ignore
IfMsgBox Abort
ExitApp
IfMsgBox Ignore
break
} ; loops reload on "Retry"
}
}