This short script sends predefined pieces of text found by the beginnings of their first word.
It is based on the built-in incremental search functionality of ListBoxes, so its features list longer than the script itself.
The text-pieces are delimited by pipe, "|", can contain spaces, special characters.
The last entry has to be "[" for technical reasons.
When the Ctrl-Space HotKey is hit, the script takes the (partial) word left to the insertion point and looks in the user-defined List if there are continuations.
- If there is none, nothing happens, otherwise a ListBox pops up with the first match highlighted.
Usually there is just one match, so keeping the Ctrl key pressed & hitting the space bar again sends the selected text to the current window (the most convenient way, but Enter works, too).
If you keep typing, an incremental search highlights always the first match, while the original window shows the typed text, which is being used to search for the long pieces of text. Here you can type space and other characters, too, which would delimit the initial word.
If there is no more match, the search aborts, leaving the typed search text intact.
Up, Down and Mouse clicks change the selection in the ListBox.
Mouse click on Scroll bar or the mouse wheel scrolls the text.
Double click on an entry in the list box sends that text to the window, erasing the text typed after the last Ctrl-Space.
Ctrl-Space:
1. No abbreviation defined: no effect.
2. There are abbreviations: pop up GUI, first match selected in a ListBox.
-- a. 2nd Ctrl-Space or Enter: Send selected text from List, replace typed text
-- b. Esc or TAB: Return, no text insertion
-- c. Keep typing: Select incremental matches, keys echoed in original window.
---- If there is no more match, exit
-- d. BackSpace:
---- If no more key left since Ctrl-Space, exit
---- Delete keys typed since Ctrl-Space activation, update selection
-- e. Up, Down, Mouse click: change selection
-- f. Double click sends the corresponding text
Limitations:
- Only works in applications using Shift-Ctrl-Left to select the word left of the insertion point
- Ctrl-X/Send to cut/paste might have side effects (change indentation, paragraph format...)
- Changing the active window makes text sent there
ToDo:
- Replace GoTo's with functions, for the purists
- Suspend while other windows are active
- Save/Load list from/to a .ini file
- Dynamically Add/Remove entries from the list
- Movable, resizable GUI
- Save/Load GUI position
Code:
Process Priority,,High ; little load, but need fast reaction
Autotrim OFF ; Space can be appended
; Your text pieces. Last "[" needed for no match detection & move first match to the top
List = Péter-Pál|South America|South Carolina|South Dakota|South Pole*|Southern|Souter, David (American jurist & statesman)|time|XEROX|Zeppelin|####|[
Gui -Caption AlwaysOnTop
Gui Margin, 2,2
Gui Add, ListBox, x2 y2 r5 w180 Sort vChoice gClicked, %List%
Return
^Space:: ; Search/send-text HotKey
ek =
ClipBoard = ; empty the ClipBoard
Send ^+{Left}^x ; cut out word on the left
ClipWait 2 ; wait until it gets to the ClipBoard
c = %ClipBoard% ; shorthand
SendRaw %c% ; send word back (^v has side effects)
Gui Show, x0 y200 ; show GUI: <-- your favorite location!
WinGet GuiID, ID, A ; ID of the GUI for ControlSend below
Send !{TAB} ; back to caller
Loop
{ ; key from the previous Loop iteration
If ek in Up,Down ; navigate in the ListBox
ControlSend ListBox1, {%ek%}, ahk_id %GuiID%
Else { ; select bottom entry, then abbreviation
GuiControl ChooseString,Choice,[ ;... to show it on top
GuiControl ChooseString,Choice,%c%
GuiControlGet Choice ; get selected
If Choice = [ ; still on "[" if no match
GoTo OUT ; exit
}
Input k, I L1 M, {Enter}{Esc}{TAB}{BS}{Up}{Down}
StringTrimLeft ek, ErrorLevel, 7 ; Remove "Endkey:"
If ek in Escape,TAB
GoTo OUT ; exit
If ek = Enter
GoTo SendText ; send full text, exit
If (k = " " and GetKeyState("Ctrl"))
GoTo SendText ; send full text, exit
If ek = Backspace
{
IfEqual c,, GoTo Out ; all have been deleted
StringTrimRight c,c,1 ; remove last char
Send {BS} ; delete last char in application window
}
Send %k% ; echo key in application window
c = %c%%k% ; incremental search
}
Clicked: ; @ mouse click in the ListBox
IfNotEqual A_GuiControlEvent,DoubleClick, Return
SendText:
Gui Submit
Send % "{BS " StrLen(c) "}" ; remove typed abbreviation
SendRaw %Choice% ; send full text
OUT:
Gui Show, Hide ; hide GUI
Return