Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Created June 17, 2022 15:56
Show Gist options
  • Save JoeGlines/99882c867039c10b69579073dbadce14 to your computer and use it in GitHub Desktop.
Save JoeGlines/99882c867039c10b69579073dbadce14 to your computer and use it in GitHub Desktop.
#NoEnv
#SingleInstance force
ToolTipDuration := 2000
;Set up for MSWORD for testing only.
;Three issues with the script
;1 - SaveWindow sometimes doesn't refocus as expected ---
;2 - Escaping causes the Tooltip to show the last saved; would like to remove that ---
;3 - Option 2 for matching the fields seems better (The A_Loopfield block at the bottom of the script)
+F12::
; Gosub, SaveWindow ;Save current window info to refocus for script execution
;above not working consistently; sometimes grabs a different window.
ActiveHwnd := WinExist("A")
HotkeyList:="^B|+F7|F7|"
Entry:=Combobox("Bold|Thesaurus|Spelling|")
;if statement works; pausing for testing of the loop feature. If I go back to this need to fix issue sending message when Esc.
If (Entry = "Bold")
Tooltip, %Entry%`rCTRL+B
Send, ^B
If (Entry = "Thesaurus")
Tooltip, %Entry%`rSHIFT+F7
Send, +{F7}
If (Entry = "Spelling")
Tooltip, %Entry%`rF7
Send, {F7}
If (Entry = "") ;not working
MsgBox, Entry: Escape probably hit or doesn't match
SetTimer, ttOFF, -2000
Escape::
ComboboxGuiEscape:
Gui, Combobox:destroy
WinActivate, ahk_id %ActiveHwnd%
return
#IfWinActive, GuiCombobox
Enter::
;comment out for testing of loop
Gui, Combobox:SubMit
Gui, Submit, NoHide
Gui, Combobox:Destroy
return
Combobox(var)
{
Static
Choice := ""
id:=WinActive(A)
CoordMode,Mouse,Screen
MouseGetPos,x,y
Gui, +OwnDialogs +AlwaysOnTop
Gui, Font, cBlack, s30, Consolas
Gui, Combobox:New
Gui, -Caption Border
Gui, Margin, 10, 10
Gui, Add, ComboBox, w200 vChoice gCbAutoComplete HwndCbHwnd r8 Sort, %Var% ;AltSubmit to return position # instead of word
;removed Simple from above as it creates a permanently open listview below
Gui, Combobox:+LastFound
Gui, Show, x%x% y%y%, GuiCombobox
CoordMode,Mouse,Window
WinWaitClose
WinActivate, ahk_id %id%
Return Choice
;=======================================================================================
; https://www.autohotkey.com/boards/viewtopic.php?t=15002
;
; Function: CbAutoComplete
; Description: Auto-completes typed values in a ComboBox.
;
; Authors: Pulover [Rodolfo U. Batista], DRock, MrHue
; Usage: Call the function from the Combobox's gLabel.
;
;=======================================================================================
CbAutoComplete:
;CB_GETEDITSEL = 0x0140, CB_SETEDITSEL = 0x0142
SendMessage, 0x0140, 0, 0,, ahk_id %CbHwnd% ; Get current edit selection
Pos := ErrorLevel ; Position = Start in low word, end in high word; note any keypress cancels selection before arriving at the g-label
GuiControlGet, cbCurContent,, %CbHwnd% ; Get current content
If BS:=GetKeyState("Backspace", "P") ; If backspace
if BS:=(choice==cbCurContent) ; If after backspace current input is same as last input
cbCurContent := SubStr(cbCurContent,1,-1) ; then remove one character (backspace only deletes selected when selection is present)
GuiControl, ChooseString, %CbHwnd%, %cbCurContent% ; Auto-complete with first matching string
If (ErrorLevel) { ; If ChooseString failed (which will wipe out entire input), restore previous input (minus one char if BS pressed - a problem if there was no previous selection)
ControlSetText,, %cbCurContent%, ahk_id %CbHwnd% ;original to restore?
PostMessage, 0x0142, 0, Pos,, ahk_id %CbHwnd% ;original to restore
;~ ; there's anther version in the thread Imay want to try. Supposed to search again if you hit backspace.
;~ Send {ESC} ;added to keep it from sending in error when Escape hit. Not working.
} ; Otherwise select everything from cursor (-1 if BS done on selection) to end of string (length = -1)
else PostMessage, 0x0142, 0, (Pos & 0xFFFF) - BS + 0xFFFF0000,, ahk_id %CbHwnd%
choice := cbCurContent ; Save last input so can check if BS changed input length or not
return
}
;~ ; Option 2 that I wanted to use from Hellbent https://youtu.be/pt3nMqExSMs?t=1647
;Seems like a much better way to match up the fields & I had it working with a simple combobox
;can't get it to work with the autocomplete combobox. Am probably not referencing the lists correctly.
;~ Gui, +OwnDialogs
;~ Gui, Combobox:SubMit
;~ Loop, Parse, HotkeyList, | ;parse token list (List 2) to send
;~ {
;~ if(Choice=A_Index) ;match to this number list (Visible) for value
;~ temp_Var:= A_LoopField
;~ Msgbox %temp_Var%
;~ }
;~ ;msgbox, 2 temp var is %temp_Var% and Loopfield is %A_LoopField%
;~ ;Clipboard:= % temp_Var ;From List 2
;~ Gui, Submit, NoHide
;~ Gui, Combobox:Destroy
;~ ;end hellbent bit
;~ return
SaveWindow:
;Save current window info to refocus for script execution
;from https://www.autohotkey.com/boards/viewtopic.php?style=1&t=97358
winNumber = 0
WinGet, win, List
Loop, %win% {
WinGetTitle, ttitle, % winTitle := "ahk_id " win%A_Index% ; Window title
WinGet, proc, ProcessName, %winTitle% ; Window process
WinGetClass, class, %winTitle% ; Window class
winNumber += !(class ~= "i)Toolbar|#32770") && ttitle > ""
&& (ttitle != "Program Manager" || proc != "Explorer.exe")
} Until (winNumber = 1)
WinActivate, %winTitle%
sleep,500
return
ttOFF:
ToolTip
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment