Get current "content" of the hotstring recognizer

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jmb3ck
Posts: 1
Joined: 28 Mar 2023, 18:05

Get current "content" of the hotstring recognizer

Post by jmb3ck » 29 May 2023, 11:20

Within a script, how can I see the current value or content or string that the hotstring recognizer is using to know if it should trigger a hotstring? I'd like to know, because I am trying to use hotstrings in a Focused Control area that isn't a textbox, and I'd like to have a way to "display" what has been typed with a tooltip, to reset the hotstring recognizer if the hotstring doesn't match any of the hotstrings, and to reset the recognizer after a certain amount of time.

I've tried to search the documentation, forums, and web, and have seen the following posts:
https://gist.github.com/ravikumarjain/26e6551a6a67a629aa75
http://www.autohotkey.com/board/topic/58483-how-to-access-the-hotstring-recognizer/
https://superuser.com/questions/936306/tooltip-should-appear-after-typing-two-letters-of-the-hotstrings/
From what I understand, those scripts use a loop to get the typed input, essentially creating their own stored string of what the recognizer might already have stored somewhere. I have also thought about trying to recreate what the hotstring recognizer is tracking. But I'd like to know if there is already an internal variable (e.g. A_Variable) that already has the recognizer content stored.

A section of my script currently is shown below, which displays the key I just typed with a tooltip, and also resets the recognizer after 3 seconds. And as an aside, I'm using a Map object to establish the hotkeys for other reasons in the script.

Thanks in advance for any help!

Code: Select all

global MapKeyDefs := Map() ; define an object with hotstring abbreviations and replacements
MapKeyDefs["btw"] := Map("replacement", "by the way")
MapKeyDefs["omy"] := Map("replacement", "on my way")

for MapKeyItem, MapKeyParams in MapKeyDefs
{
	Hotstring ":c1ZSI*:" . MapKeyItem, SWSearchKey MapKeyDefs[MapKeyItem]["replacement"] ; create hotstrings for every item in MapKeyDefs 
}

~$b::
~$t::
~$w::
{
	ToolTip substr(A_ThisHotkey,StrLen(A_ThisHotkey)) ; send tooltip of the hotkey without the "~$"
	SetTimer hidetip, -3000 ; hide tip after 3 seconds, do it just once
} 

hidetip() {
	tooltip ;close the tooltip
	Hotstring "Reset"
}

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]

User avatar
mikeyww
Posts: 26856
Joined: 09 Sep 2014, 18:38

Re: Get current "content" of the hotstring recognizer

Post by mikeyww » 29 May 2023, 13:10

Welcome to this AutoHotkey forum!

The documentation contains the listing of all of the built-in variables, as well as the rules for the recognizer. I did not see any variable related to the recognizer.

An idea is below. It might need adjustment.

Code: Select all

; This is a hotstring recognizer.
; The recognizer's text is displayed in a ToolTip.
#Requires AutoHotkey v2.0
hotChars := ''
Loop 0xFF {
 key := GetKeyName(Format('VK{:02X}', A_Index))
 If StrLen(key) = 1 || key ~= 'i)(Enter|Tab|Space|Numpad)'
  Hotkey '~*' key, press, 'On'
}
Loop Parse, '!@#$%^&*()_+<>?{}|:"'
 Hotkey '~*' A_LoopField, press, 'On'

~*LButton::
~*RButton::reset

press(ThisHotkey) {
 Global hotChars
 Static last := '', hWnd := ''
      , regex := '([-()[\]{}`':;"/\\,.?!]|Enter|Tab|Space)'
      , np := Map(
                 'Mult', '*'
               , 'Add' , '+'
               , 'Sub' , '-'
               , 'Dot' , '.'
               , 'Div' , '/'
              )
 If GetKeyState('Alt', 'P') GetKeyState('LWin', 'P') GetKeyState('Ctrl', 'P')
  Return
 last := hWnd, hWnd := WinActive('A')
 hk := Format(GetKeyState('Shift', 'P') GetKeyState('CapsLock', 'T') ? '{:U}' : '{}'
        , StrReplace(SubStr(ThisHotkey, 3), 'Numpad'))
 Try hk := np[hk]
 Switch {
  Case hk ~= regex: reset
  Case hk = 'Backspace': ToolTip hotChars := SubStr(hotChars, 1, -1)
  Default: (last && hWnd != last) && reset(), ToolTip(hotChars .= hk)
 }
}

reset() {
 Global hotChars := ''
 ToolTip
}

Post Reply

Return to “Ask for Help (v2)”