Jump to content

Use a hotkey to send the same key as the hotkey without triggering itself?


Recommended Posts

I'm trying to make a macro that will spam the 1 key as long as I'm holding it down (as in repeatedly press and release until I release the hotkey.) But what it does is do a loop of the hotkey triggering itself which does absolutely nothing. I'm wondering if there's a way to work around this somehow?

Another example is if I'm trying to make the hotkey "h" send the string "hello", if I do this it inputs 'ello' and triggers itself, which inputs 'ello' again and continues until I terminate it.

Here's the code I'm working with

Global $Paused
HotKeySet("{END}", "Terminate")
HotKeySet("1", "one")

While 1
WEnd

Func Terminate()
    Exit 0
EndFunc

Func one()
    While 1
        Send("1")
        sleep(2)
    WEnd
EndFunc

I'll try to be reasonable, I haven't worked with AutoIt much.

Link to comment
Share on other sites

This does the trick.

#Include <Misc.au3>
While 1
    If _IsPressed(31) Then
        Send("Realse to stop me!" & @CRLF)
    EndIf
WEnd

See here for list of key codes.

http://www.kbdedit.com/manual/low_level_vk_list.html

Ah, this is a step in the right direction, thank you. Now I can send the key, but I've been playing with it and I can't figure out how to make it press and release the key repeatedly until the hotkey is released, rather than just have it press 1 when I press the hotkey and release 1 when I release the hotkey, which is pretty redundant as it does that anyway.

Thanks for any help

Link to comment
Share on other sites

You should unset the hotkey when you enter the function and then reset it before returning.

HotKeySet("1", "0ne")

Func One()
    HotKeySey("1")
    $dll = DllOpen("user32.dll")
    While _IsPressed(31)
        ;; do what you want here.
    WEnd
    DllClose($dll)
    HotKeySet("1", "One")
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

It's getting closer, I just don't know what else to do from here.

If I do this, it does the same thing it did last post:

HotKeySet("1", "one")

Func one()
    HotKeySet("1")
    $dll = DllOpen("user32.dll")
    While _IsPressed(31)
        send("1")
        sleep(1)
    WEnd
    DllClose($dll)
    HotKeySet("1", "One")
EndFunc

However, if I do this, it sends a 1 continuously until I terminate, even if I've released the key:

HotKeySet("1", "one")

Func one()
    HotKeySet("1")
    $dll = DllOpen("user32.dll")
    While _IsPressed(31)
        While 1
            send("1")
            sleep(1)
        WEnd
    WEnd
    DllClose($dll)
    HotKeySet("1", "One")
EndFunc

What I'd think I need is a way to stop the loop, or return to:

While 1

WEnd

upon release of the hotkey, but I don't know how to do that. If not, maybe someone has a different way?

I know this all must be very simple and perhaps sounds redundant, but I am learning.

Thanks again for all the help.

Link to comment
Share on other sites

It's going to stay in the second loop forever the way you have it. The line I added should not be required nor should that sleep(). Take a look at Opt("SendKeyDelay").

HotKeySet("1", "one")

Func one()
    HotKeySet("1")
    $dll = DllOpen("user32.dll")
    While _IsPressed(31)
        send("1")
        sleep(1)
        If NOT _IsPressed(31) Then ExitLoop
    WEnd
    DllClose($dll)
    HotKeySet("1", "One")
EndFunc
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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