Jump to content

AutoIt Hotkey Assignment for duplicate letters/keys


Go to solution Solved by TheXman,

Recommended Posts

Hi @TheCrimsonCrusader 👋 ,

as you already mentioned the function would be called at the first "w" which was hitten. So I strongly believe this is not achievable via HotKeySet.
But with _IsPressed() you can do such combinations, with few conditions and so on.

Why do you want this?

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

1 hour ago, TheCrimsonCrusader said:

Is there a way to do a <CTRL> + double tap of the "w" key as an example for hotkey assignment?

HotKeySet("^ww", "HotKeyPressed") != HotKeySet("^w", "HotKeyPressed") + HotKeySet("^w", "HotKeyPressed")

Run it in slow motion in your head and you'll see a delay between the first ^w and the next ^w
So treat it as a double click, where you measure the time since the last ^w.
If the second ^w is under 500 ms. then, is your trigger. You can code that idea :)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

39 minutes ago, SOLVE-SMART said:

Hi @TheCrimsonCrusader 👋 ,

as you already mentioned the function would be called at the first "w" which was hitten. So I strongly believe this is not achievable via HotKeySet.
But with _IsPressed() you can do such combinations, with few conditions and so on.

Why do you want this?

Best regards
Sven

 

I don't want to do this, my wife does for various e-mail signatures associated to a hotkey. 🙂  Apparently, Revit or one of her other architectural apps uses a hotkey sequence with a double-tap of the same letter and she likes it.   Women.  smh

Link to comment
Share on other sites

3 minutes ago, TheCrimsonCrusader said:

uses a hotkey sequence with a double-tap of the same letter and she likes it.

I'd like it too. Say you choose ^f. You can do it right now. You'll see that the browser thinks that you wanna find something. Do it twice for your trigger. Makes sense to like it.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Hello everybody :)

When using HotKeys, I like to deactivate them immediately as 1st instruction at the beginning of the function, then reactivate them at the very end of the function. If you don't do that, then be prepared to watch the Hotkey triggered several times if you keep the keys pressed just a bit too long.

So I made some changes to @TheXman good script, to add this functionality. You can try it for example like this :
* Open NotePäd and type anything in it
* Run the script below (the hotkey is Ctrl+a (not the best hotkey but good for the test) then select the NotePad window.
* OP's behavior seems to be respected by the script (the mimic of the "double click" on Ctrl+a)

* If the 1st line of the function is...

HotKeySet("^a")

... then a "long" press on Ctrl+a will select what you typed in NotePad. Is it a good behavior to have it selected ? I'm not sure as you could still select all by using NotePad menu.

* If the 1st line of the function is...

HotKeySet("^a", _DoNothing)

...then a long press on Ctrl+a will not select what you typed in NotePad.

Also please note that while the informative MsgBox is displayed during the HotKey function, then a couple of press on Ctrl+a would indeed buffer the keys (as MsgBox is a blocking function) but they won't be processed with this rewrited script, no matter the 1st line of the function is HotKeySet("^a") or HotKeySet("^a", _DoNothing)
Of course the question could be : "why should the user press for a long time Ctrl+a when a MsgBox is displayed ?"
The answer could be : "just to test !" :D

* The following added part of code should prevent Ctrl+a to be processed when we're inside the HotKey function, in case the user keeps Ctrl+a pressed a "long" time :

While _IsPressed("11", $ghUserDll) And _IsPressed("41", $ghUserDll) ; Ctrl = 11, A = 41
    Sleep(10)
WEnd

* I added a Sleep(10) in main loop to refresh the CPU

* The counter $iCounter is just here to indicate how many times the HotKey function was accessed, so we can see in real time its increment in the Console while pressing this or that.

#include <Misc.au3>

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration

;Declare constants
Const $DOUBLE_CLICK_TIME = 500

;Declare global vars
Global $ghUserDll = DllOpen("user32.dll"), $ghCtrl_ATimer = TimerInit()

;Set hotkey
HotKeySet("^a", do_ctrl_a)

;Loop until ESC pressed
While 1
    If _IsPressed("1B", $ghUserDll) then ExitLoop
    Sleep(10)
WEnd

;==========================================================================
; This hotkey function only do something if called twice
; within a specified time ($DOUBLE_CLICK_TIME)
;==========================================================================
Func do_ctrl_a()

    ; HotKeySet("^a")
    HotKeySet("^a", _DoNothing)

    While _IsPressed("11", $ghUserDll) And _IsPressed("41", $ghUserDll) ; Ctrl = 11, A = 41
        Sleep(10)
    WEnd

    Local Static $iCounter = 0
    $iCounter += 1
    ConsoleWrite($iCounter & " ")

    ;Declare vars
    Local Static $iPrevTime = 0
    Local $iCurrTime = TimerDiff($ghCtrl_ATimer)

    ;If function called twice within specified time
    If $iCurrTime < ($iPrevTime + $DOUBLE_CLICK_TIME) Then

        ;Do something
        MsgBox(BitOR($MB_ICONINFORMATION, $MB_TOPMOST), "INFO", "CTRL+A double click occurred.")

        ;Reset timer
        $ghCtrl_ATimer = TimerInit()
    EndIf

    ;Reset previous Time to current time
    $iPrevTime = TimerDiff($ghCtrl_ATimer)

    HotKeySet("^a", do_ctrl_a)

EndFunc

;==============================================
Func _DoNothing()
EndFunc   ;==>_DoNothing

 

Edited by pixelsearch
typo
Link to comment
Share on other sites

@TheCrimsonCrusader glad we could help, your wife should be happy :)

There is something important I didn't mention in my preceding post as it was already too long.
In real life, you will probably need this Hotkey to be triggered when a specific window is active, but not for any other window.

In the example below, Ctrl+a will act normally if the AutoIt help file is active (select all), but if NotePad is active, then the "double-click Ctrl+a+a" will trigger the code you will associate to Ctrl+a+a

It means that when any Window is active (except NotePad) then we Send Ctrl+a if the user keys it, also there is code to avoid the very annoying issue of "sticky keys" just before we Send Ctrl+a

I tested the script and it seems to work fine. The Window title I used for AutoIt help is "AutoIt Help" (and not "AutoIt Help (v3.3." because if someone tests the script with an older version of AutoIt, then the help file title was simply "AutoIt Help" without any reference to the AutoIt version.

Hope it helps.

#include <MsgBoxConstants.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration

If (Not WinExists("[CLASS:Notepad]")) Or (Not WinExists("AutoIt Help")) Then
    MsgBox($MB_TOPMOST, "Please do the following for this test", _
        "1 - Open NotePad and type something in it" & @crlf & _
        "2 - Open AutoIt Help file" & @crlf & @crlf & _
         "Then run this script again")
    Exit
EndIf

;Declare constants
Const $DOUBLE_CLICK_TIME = 500

;Declare global vars
Global $ghUserDll = DllOpen("user32.dll"), $ghCtrl_ATimer = TimerInit()

;Set hotkey
HotKeySet("^a", do_ctrl_a)

;Loop until ESC pressed
While 1
    If _IsPressed("1B", $ghUserDll) then ExitLoop
    Sleep(10)
WEnd

;==========================================================================
; This hotkey function only do something if called twice
; within a specified time ($DOUBLE_CLICK_TIME)
;==========================================================================
Func do_ctrl_a()

    HotKeySet("^a")

    While _IsPressed("11", $ghUserDll) And _IsPressed("41", $ghUserDll) ; Ctrl = 11, A = 41
        Sleep(10)
    WEnd

    Local Static $iCounter = 0
    $iCounter += 1
    ConsoleWrite($iCounter & " ")

    If WinActive("[CLASS:Notepad]") Then

        ;Declare vars
        Local Static $iPrevTime = 0
        Local $iCurrTime = TimerDiff($ghCtrl_ATimer)

        ;If function called twice within specified time
        If $iCurrTime < ($iPrevTime + $DOUBLE_CLICK_TIME) Then

            ;Do something
            MsgBox(BitOR($MB_ICONINFORMATION, $MB_TOPMOST), "NOTEPAD INFO", "CTRL+A double click occurred.")

            ;Reset timer
            $ghCtrl_ATimer = TimerInit()
        EndIf

        ;Reset previous Time to current time
        $iPrevTime = TimerDiff($ghCtrl_ATimer)

    Else
        While _IsPressed("11") ; Ctrl = 11 . Important While to avoid a possible "sticky key" issue before Send("^a")
            Sleep(10)
        WEnd

        Send("^a")
    EndIf

    HotKeySet("^a", do_ctrl_a)

EndFunc

Ctrlaa.png.05e5a1cb543d080f9a6e820d5de00be1.png

 

Edited by pixelsearch
typo
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...