Jump to content

HotStrings - String hotkeys


jvanegmond
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • 11 months later...

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]
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 months later...

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

  • 1 month later...

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...