HotStrings - String hotkeys

67 posts in this topic

#1 ·  Posted (edited)

HotStrings are similar to AutoIts HotKey function but they trigger on a string of characters, instead of a key or key combination. This has been built because of popular requested from the community.

Instructions for use: Download the HotString.au3 (from the GitHub link) and put it in the same directory as your script. Then try out the code example below, or use the library in your own application.

 

Please visit https://github.com/jvanegmond/hotstrings for the download (download zip in bottom right). Or get it directly from master.

Example

#include <HotString.au3>

HotStringSet("callme{enter}", examplefunction)

While 1
    Sleep(10)
WEnd

Func examplefunction()
    MsgBox(0,"","You typed callme! :)")
EndFunc
Edited by Manadar
2 people like this

Share this post


Link to post
Share on other sites



#3 ·  Posted

This is cool I was in need of this a year ago, gave up though :D

After my tic-tac-toe game I should continue it :D

Share this post


Link to post
Share on other sites

#4 ·  Posted

Is there anychance you can put the link back up for this?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The link is cold... I tried to browse your site, however I didn't know the title name!

thx

8)

Edited by Valuater

Ad_Smallest_zpsjjqeqtel.jpg

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

If I still have the file (not sure I do) I will upload it for ya'll!

I lost the file and decided to rewrite it on request.

Suggestions and code improvements are very much welcome. Especially to get rid of the ugly Adlib. :]

Edited by Manadar

Share this post


Link to post
Share on other sites

#8 ·  Posted

Woah, thats cool. Nice work here! :D

AlmarM


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

#9 ·  Posted

Cool! Do I understand it right, that I can register HotStrings as I can with hotkeys, so that whereever I type the defined string (eg. in browser or open office), the script that could be running in background, will execute the defined function?


[font="Courier New"]http://RomanK.hondadesigns.com[/font]

Share this post


Link to post
Share on other sites

#10 ·  Posted

Cool! Do I understand it right, that I can register HotStrings as I can with hotkeys, so that whereever I type the defined string (eg. in browser or open office), the script that could be running in background, will execute the defined function?

Yes.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

wow, that is great, thx for the cornfirmation, Manadar! A few hours ago I didn't even know that HotStrings existed outside of games (cheats... which I don't use by the way) :D

EDIT: Oh, and thank you for the code itself of course :o

hmm, does the hotstring also get unregistered like what should be done with hotkeys when closing the autoit app?

Edited by RomanK

[font="Courier New"]http://RomanK.hondadesigns.com[/font]

Share this post


Link to post
Share on other sites

#12 ·  Posted

Well, uuh, there's really no such thing as a "HotString" in Windows. I just made one using a keyboard hook, so.. There you have it.

Share this post


Link to post
Share on other sites

#13 ·  Posted

I think you should rethink the buffer thing. If I have 2 functions named "a" and "b" and I press "aa" "bb" it's gonna be cool but maybe not what the user expect HotString. ;]

Share this post


Link to post
Share on other sites

#14 ·  Posted

I lost the file and decided to rewrite it on request.

Suggestions and code improvements are very much welcome. Especially to get rid of the ugly Adlib. :]

Some thoughts:

[1] Perhaps it should check that the currently active window is the same before concatenating the $buffer

[2] Maybe there could be a timeout so that consecutive characters have to be pressed within a certain timeframe

[3] If you made a GUI (maybe even a hidden one) then you could post a message (_WinAPI_PostMessage()) to the GUI at the end of EvaluateKey. Combine that with subclassing or GUIRegisterMsg() and you could get rid of that beautiful Adlib :D

There's no reason this couldn't become a full blown app similar to Lifehacker's Texter which is written in some other macro language :o

WBD

Share this post


Link to post
Share on other sites

#15 ·  Posted

[3] If you made a GUI (maybe even a hidden one) then you could post a message (_WinAPI_PostMessage()) to the GUI at the end of EvaluateKey. Combine that with subclassing or GUIRegisterMsg() and you could get rid of that beautiful Adlib :D

Brilliant.

Share this post


Link to post
Share on other sites

#16 ·  Posted

hey,

this code is really great... i'm using it at work to monitor what's being typed and remember strings of a certain format. the only problem is that it doesn't seem to detect the ENTER key at all (ie, $keycode has no value when enter is pressed). can anyone explain why this is the case?

thanks

Share this post


Link to post
Share on other sites

#17 ·  Posted

hey,

this code is really great... i'm using it at work to monitor what's being typed and remember strings of a certain format. the only problem is that it doesn't seem to detect the ENTER key at all (ie, $keycode has no value when enter is pressed). can anyone explain why this is the case?

thanks

I'm using ENTER key internally to indicate a new sequence of characters. I think it is possible to remember the last 100 characters pressed and see if they match a part of the ...........

Try this new version:

; Authors: Manadar, GarryFrost
; Contributor: WideBoyDixon

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

#include-once

Dim $hHook
Dim $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
Dim $hmod = _WinAPI_GetModuleHandle(0)
Dim $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
Dim $buffer = ""
Dim $hotstrings[1]
Dim $hotfuncs[1]
Dim $hWnd = GUICreate("")
GUIRegisterMsg($WM_KEYDOWN, "_GUIKeyProc")

;; ==========================
;; This is your actual script.
;; ==========================

;#include <HotString.au3>

HotStringSet("date","examplefunction")

While 1
    Sleep(10)
WEnd

Func examplefunction()
    Send("{BACKSPACE 4}")
    Send(@WDAY & " - " & @MON & " - " & @YEAR)
EndFunc

;; ==========================
;; End of actual script.
;; The part above the start of the actual script
;; and below this comment
;; script can be put in another
;; file and included.
;; ==========================

Func HotStringSet($hotstring, $func)
    _ArrayAdd($hotstrings, $hotstring)
    _ArrayAdd($hotfuncs, $func)
EndFunc

Func EvaluateKey($keycode)
    If (($keycode > 64) And ($keycode < 91)) _ ; A - Z
            Or (($keycode > 47) And ($keycode < 58)) Then ; 0 - 9
        $buffer &= Chr($keycode)
        $buffer = StringRight($buffer, 50)
        
        _CheckHotkeys($buffer)
    EndIf
EndFunc   ;==>EvaluateKey

Func _CheckHotkeys($s)
    For $i = 0 to UBound($hotstrings)-1
        if ( $hotstrings[$i] = StringRight($s, StringLen($hotstrings[$i])) ) Then
            Call($hotfuncs[$i])
        EndIf
    Next
EndFunc

Func _GUIKeyProc($hWnd, $Msg, $wParam, $lParam)
    EvaluateKey(Number($wParam))
EndFunc

;===========================================================
; callback function. This function must return as quickly as possible or it will stall and block user input
;===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN Then
        $vkKey = DllStructGetData($tKEYHOOKS, "vkCode")
        _WinAPI_PostMessage($hWnd, $WM_KEYDOWN, $vkKey, 0)
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>OnAutoItExit

Share this post


Link to post
Share on other sites

#18 ·  Posted

What I mean is what if you want to make sure a non-character is entered after the HotString? If you look at $keycode after each keypress, certain keys aren't detected such as ESC, TAB and ENTER. It's just a bit weird (CAPS and SHIFT, etc work fine).

Share this post


Link to post
Share on other sites

#19 ·  Posted

They are detected, but they are not printed in the version given at the top. The newer version adds all characters to the buffer, but is only converted using the Chr function. I should probably convert them to the way AutoIt writes them down, or vice versa.

Share this post


Link to post
Share on other sites

#20 ·  Posted

First of all a big thanks to the authors of this wonderful post.

Texter was a good option but it couldn't do mouse click on writing specific text. Texter will not even come near any program, if made properly, from this text. Oh and texter used to make my programs go funny!

Since then I had been looking for a good program in this relation. Something which could send both text and /or mouse actions on typing keywords. I think I have it now. Below is a simple input gui for the code to do text replacement, if you want to execute code, just add another input box to the gui and code accordingly.

I have divided the original code into multiple files so as to be able to work. I am not a programmer so my attempts in creating a full fledged program will be futile. This solution works for me, change it to suit your individual needs.

You will require (see codes below):

  • autotextmain.au3
  • autotextcode.au3
  • autotextfunctions.au3
  • autotextform.au3

  • autotextmain.au3

    ; Authors: Manadar, GarryFrost
    ; Contributor: WideBoyDixon
    ; Gui: Celestialspring
    
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <File.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>
    #include <Array.au3>
    #include <autotextcode.au3>
    #include <autotextfunctions.au3>
    
    #include-once
    
    Dim $hHook
    Dim $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    Dim $hmod = _WinAPI_GetModuleHandle(0)
    Dim $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
    Dim $buffer = ""
    Dim $hotstrings[1]
    Dim $hotfuncs[1]
    Dim $hWnd = GUICreate("")
    GUIRegisterMsg($WM_KEYDOWN, "_GUIKeyProc")
    
    
    ;; ==========================
    ;; This is your actual script.
    ;; ==========================
    
    ;#include <HotString.au3>
    
    
    HotStringSet("test","test")
    
    
    
    
    
    While 1
        Sleep(10)
    WEnd

  • autotextcode.au3

    Func HotStringSet($hotstring, $func)
        _ArrayAdd($hotstrings, $hotstring)
        _ArrayAdd($hotfuncs, $func)
    EndFunc
    
    Func EvaluateKey($keycode)
        If (($keycode > 64) And ($keycode < 91)) _ ; A - Z
                Or (($keycode > 47) And ($keycode < 58)) Then ; 0 - 9
            $buffer &= Chr($keycode)
            $buffer = StringRight($buffer, 50)
    
            _CheckHotkeys($buffer)
        EndIf
    EndFunc   ;==>EvaluateKey
    
    Func _CheckHotkeys($s)
        For $i = 0 to UBound($hotstrings)-1
            if ( $hotstrings[$i] = StringRight($s, StringLen($hotstrings[$i])) ) Then
                Call($hotfuncs[$i])
            EndIf
        Next
    EndFunc
    
    Func _GUIKeyProc($hWnd, $Msg, $wParam, $lParam)
        EvaluateKey(Number($wParam))
    EndFunc
    
    ;===========================================================
    ; callback function. This function must return as quickly as possible or it will stall and block user input
    ;===========================================================
    Func _KeyProc($nCode, $wParam, $lParam)
        Local $tKEYHOOKS
        $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
        If $nCode < 0 Then
            Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
        EndIf
        If $wParam = $WM_KEYDOWN Then
            $vkKey = DllStructGetData($tKEYHOOKS, "vkCode")
            _WinAPI_PostMessage($hWnd, $WM_KEYDOWN, $vkKey, 0)
        EndIf
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndFunc   ;==>_KeyProc
    
    Func OnAutoItExit()
        _WinAPI_UnhookWindowsHookEx($hHook)
        DllCallbackFree($hStub_KeyProc)
    EndFunc   ;==>OnAutoItExit

  • autotextfunctions.au3

    Func test()
    send("{BS 4}")
    send("this is some text")
    EndFunc

  • autotextform.au3 --- This is your input GUI

    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <File.au3>
    
    #Region ### START Koda GUI section ### Form=
    $Form1_1 = GUICreate("Auto-it Autotext", 332, 443, 192, 125)
    $Main_label = GUICtrlCreateLabel("Auto-it Autotext", 17, 8, 300, 33, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    $Shortcutkeylabel = GUICtrlCreateLabel("Shortcut key", 17, 60, 104, 33, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    $Shortcut_input = GUICtrlCreateInput("", 17, 112, 300, 21)
    $Label1 = GUICtrlCreateLabel("Code or Full Text", 17, 156, 124, 33, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    $code_input = GUICtrlCreateEdit("", 17, 208, 300, 178)
    GUICtrlSetData(-1, "")
    $ok_button = GUICtrlCreateButton("Ok", 17, 400, 97, 25, $WS_GROUP)
    $Cancel_button = GUICtrlCreateButton("Cancel", 193, 400, 95, 25, $WS_GROUP)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    
    
    While 1
    
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Cancel_button
                ;FileClose("D:\Program Files\Autoit3\Include\autotextmain.au3",1)
                ;FileClose("D:\Program Files\AutoIt3\Include\autotextfunctions.au3", 1)
                Exit
            Case $ok_button
                $shortinputfile = FileOpen("D:\Program Files\AutoIt3\Include\autotextmain.au3", 1)
                $codeinputfile = FileOpen("D:\Program Files\AutoIt3\Include\autotextfunctions.au3", 1)
                $shortinputread = GUICtrlRead($Shortcut_input)
                If $shortinputread = "" Then Exit
                If $shortinputread <> "" Then
                    _FileWriteToLine("D:\Program Files\AutoIt3\Include\autotextmain.au3", 39, @CRLF & "HotStringSet(" & """" & $shortinputread & """" & "," & """" & $shortinputread & """" & ")", 1)
                    FileClose($shortinputfile)
                    ;FileWrite($shortinputfile,$shortinputread & @CRLF & While & 1 @CRLF Sleep(10)
                    ;WEnd)
                EndIf
    
                $codeinputread = GUICtrlRead($code_input)
                If $codeinputread = "" Then Exit
                $len=StringLen($shortinputread)
                FileWrite($codeinputfile, "Func "&$shortinputread&"("&")"&@CRLF&"send("&""""&"{BS "&$len&"}"&""""&")"&@CRLF&"send("&""""&$codeinputread&""""&")"&@CRLF&"EndFunc"&@CRLF&@CRLF)
                FileClose($codeinputfile)
    
    
                Exit
    
        EndSwitch
    WEnd

A few things to note with this code

1. I wasn't able to get Autoit to accept relative file names.

2. I don't know how to combine the three files into one single exe file which is capable of read/write.

3. I run the form code as an exe on my machine when i need to add a shortcut and keep running the autotextmain.au3 fie in the background. Would really appreciate if someone with higher programming knowledge converts this into a texter like app.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now