Jump to content

HELP with switching hotkeys for different function when I press a key


Dxnny
 Share

Recommended Posts

I'm currently running a script so when I press F it will do something, then G will do something else, is there a way to make it so that when I press a button it will pause this part of the code and allow me to use those some hotkeys (F, G, etc) for a different part of code?

Link to comment
Share on other sites

#include <Misc.au3>
$dll = DllOpen("user32.dll")
Global $Paused
HotKeySet("{NUMPADSUB}", "TogglePause")
HotKeySet("{NUMPADENTER}", "Terminate")


While 1
Sleep ( 50 )

; Actions 1
If _IsPressed("46", $dll) Then 

Send ("I")
Sleep (5001)
MouseClickDrag("left", 736, 684, 728, 252, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("47", $dll) Then

Send ("I")
Sleep (5001)
MouseClickDrag("left", 736, 684, 728, 348, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("48", $dll) Then

Send ("I")
Sleep (5001)
MouseClickDrag("left", 736, 684, 728, 444, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("4A", $dll) Then

Send ("I")
Sleep (5001)
MouseClickDrag("left", 736, 684, 728, 540, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("4B", $dll) Then

Send ("I")
Sleep (5001)
MouseClickDrag("left", 736, 684, 814, 513, 1 )
Sleep (100)
Send ("I")
EndIf

; Actions 2

If _IsPressed("4C", $dll) Then
Send ("I")
Sleep (5001)
MouseClickDrag("left", 728, 252, 736, 684, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("BA", $dll) Then
Send ("I")
Sleep (5001)
MouseClickDrag("left", 728, 348, 736, 684, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("BC", $dll) Then
Send ("I")
Sleep (5001)
MouseClickDrag("left", 728, 444, 736, 684, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("BE", $dll) Then

Send ("I")
Sleep (5001)
MouseClickDrag("left", 728, 540, 736, 684, 1 )
Sleep (100)
Send ("I")
EndIf
If _IsPressed("BF", $dll) Then

Send ("I")
Sleep (5001)
MouseClickDrag("left", 814, 513, 736, 684, 1 )
Sleep (100)
Send ("I")
EndIf
WEnd
DllClose($dll)

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
    WEnd
EndFunc

Func Terminate()
    Exit 0
EndFunc

This is the code I have right now, very messy, I'm trying to figure out a way so when I press a button, Actions 1 section pauses, and Actions 2 category becomes active, that way I can have the same hotkeys for both categories without them messing up each other, I hate having all these buttons for different actions, rather just keep it to 5.

Link to comment
Share on other sites

#include <Misc.au3>

HotKeySet(("{ESC}"),"Terminate")
HotKeySet(("{f}"),"Notepad")

ConsoleWrite("'f' key will now open notepad" & @CRLF)
ConsoleWrite("'a' to switch hotkey" & @CRLF)
ConsoleWrite("'esc' to stop script" & @CRLF)

Local $vhotkey = 0

While Sleep (100)
    If _IsPressed("41") AND $vhotkey = 0 Then ;41 = a
        ConsoleWrite("'f' key will now open calulator" & @CRLF)
        HotKeySet(("{f}"),"Calc")
        $vhotkey = 1
    ElseIf _IsPressed("41") AND $vhotkey = 1 Then ;41 = a
        ConsoleWrite("'f' key will now open notepad" & @CRLF)
        HotKeySet(("{f}"),"Notepad")
        $vhotkey = 0
    EndIf

WEnd




Func Notepad()
    Run("notepad.exe")
EndFunc


Func Calc()
    Run("calc.exe")
EndFunc

Func Terminate()
    Exit
 EndFunc

You could try something like this. 

Change notepad and calc to a msgbox if you need to test. 

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

2 minutes ago, SkysLastChance said:
#include <Misc.au3>

HotKeySet(("{ESC}"),"Terminate")
HotKeySet(("{f}"),"Notepad")

ConsoleWrite("'f' key will now open notepad" & @CRLF)
ConsoleWrite("'a' to switch hotkey" & @CRLF)
ConsoleWrite("'esc' to stop script" & @CRLF)

Local $vhotkey = 0

While Sleep (100)
    If _IsPressed("41") AND $vhotkey = 0 Then ;41 = a
        ConsoleWrite("'f' key will now open calulator" & @CRLF)
        HotKeySet(("{f}"),"Calc")
        $vhotkey = 1
    ElseIf _IsPressed("41") AND $vhotkey = 1 Then ;41 = a
        ConsoleWrite("'f' key will now open notepad" & @CRLF)
        HotKeySet(("{f}"),"Notepad")
        $vhotkey = 0
    EndIf

WEnd




Func Notepad()
    Run("notepad.exe")
EndFunc


Func Calc()
    Run("calc.exe")
EndFunc

Func Terminate()
    Exit
 EndFunc

You could try something like this. 

Change notepad and calc to a msgbox if you need to test. 

Ohh perfect, thanks a lot! Will incorporate this into my script :)

Link to comment
Share on other sites

#include <Misc.au3>
$dll = DllOpen("user32.dll")
Global $Paused
HotKeySet("{NUMPADSUB}", "TogglePause")
HotKeySet("{NUMPADENTER}", "Terminate")

Local $vhotkey = 0

While Sleep (100)
    If _IsPressed("6A") AND $vhotkey = 0 Then ;6A = *
        ConsoleWrite("Cat A hotkeys will now work" & @CRLF)
        If _IsPressed("46", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 736, 684, 728, 252, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("47", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 736, 684, 728, 348, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("48", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 736, 684, 728, 444, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("4A", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 736, 684, 728, 540, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("4B", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 736, 684, 814, 513, 1 )
         Sleep (100)
         Send ("I")
         EndIf
        $vhotkey = 1
    ElseIf _IsPressed("6A") AND $vhotkey = 1 Then ;6A = *
        ConsoleWrite("Cat B hotkeys will now work" & @CRLF)
         If _IsPressed("46", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 728, 252, 736, 684, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("47", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 728, 348, 736, 684, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("48", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 728, 444, 736, 684, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("4A", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 728, 540, 736, 684, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         If _IsPressed("4B", $dll) Then
         Send ("I")
         Sleep (5001)
         MouseClickDrag("left", 814, 513, 736, 684, 1 )
         Sleep (100)
         Send ("I")
         EndIf
         DllClose($dll)
         $vhotkey = 0
    EndIf
WEnd


Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
    WEnd
EndFunc

Func Terminate()
    Exit 0
EndFunc

Ok, so it didn't work, this is what I've got.

Link to comment
Share on other sites

7 minutes ago, Nine said:

What are all those mouse drag ?  I wonder if there would be a better approach.  What is the application you are trying to automate ?

Just a load of actions that I have to keep repeating, I just want the 2 categories to have the same hotkeys, would make things so much easier.

Link to comment
Share on other sites

Link to comment
Share on other sites

There is probably a way easier way to do this. However using my code you would need to do this. 

#include <Misc.au3>


HotKeySet(("{ESC}"),"Terminate")
HotKeySet(("{f}"),"CatA_F")
HotKeySet(("{g}"),"CatB_G")
;... add more keys

ConsoleWrite("'f' and 'g' key will now us CatA" & @CRLF)
ConsoleWrite("'a' to switch to CatB" & @CRLF)
ConsoleWrite("'esc' to stop script" & @CRLF)

Local $vhotkey = 0 ;$vhotkey =0 starts on category A $vhotkey =1 starts on category B


While $vhotkey = 1 or $vhotkey = 0


    If _IsPressed("41") AND $vhotkey = 0 Then ;41 = 'A' key
        ConsoleWrite("Cat A hotkeys will now work" & @CRLF)
        HotKeySet(("{f}"),"CatA_F")
        HotKeySet(("{g}"),"CatB_G")
        ;... add more keys
        Sleep (100)

        $vhotkey = 1


    ElseIf _IsPressed("41") AND $vhotkey = 1 Then ;41 = 'A' key
        ConsoleWrite("Cat B hotkey will now work" & @CRLF)
        HotKeySet(("{f}"),"CatB_F")
        HotKeySet(("{g}"),"CatB_G")
        ;... add more keys
        Sleep (100)

        $vhotkey = 0
    EndIf

    ;......your code


WEnd




Func CatA_F() ;Category A 'F' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 736, 684, 728, 252, 1 )
    Sleep (100)
    Send ("I")
EndFunc


Func CatB_F() ;Cateogry B 'F' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 728, 252, 736, 684, 1 )
    Sleep (100)
    Send ("I")
EndFunc

Func CatA_G() ;Cateogry A 'G' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 736, 684, 728, 348, 1 )
    Sleep (100)
    Send ("I")
EndFunc

Func CatB_G() ;Category B 'G' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 728, 348, 736, 684, 1 )
    Sleep (100)
    Send ("I")
EndFunc


;add more funchs

Func Terminate()
    Exit
 EndFunc

 

Edited by SkysLastChance

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

11 hours ago, SkysLastChance said:

There is probably a way easier way to do this. However using my code you would need to do this. 

#include <Misc.au3>


HotKeySet(("{ESC}"),"Terminate")
HotKeySet(("{f}"),"CatA_F")
HotKeySet(("{g}"),"CatB_G")
;... add more keys

ConsoleWrite("'f' and 'g' key will now us CatA" & @CRLF)
ConsoleWrite("'a' to switch to CatB" & @CRLF)
ConsoleWrite("'esc' to stop script" & @CRLF)

Local $vhotkey = 0 ;$vhotkey =0 starts on category A $vhotkey =1 starts on category B


While $vhotkey = 1 or $vhotkey = 0

    If _IsPressed("41") AND $vhotkey = 0 Then ;41 = 'A' key
        ConsoleWrite("Cat A hotkeys will now work" & @CRLF)
        HotKeySet(("{f}"),"CatA_F")
        HotKeySet(("{g}"),"CatB_G")
        ;... add more keys
        $vhotkey = 1


    ElseIf _IsPressed("41") AND $vhotkey = 1 Then ;41 = 'A' key
        ConsoleWrite("Cat B hotkey will now work" & @CRLF)
        HotKeySet(("{f}"),"CatB_F")
        HotKeySet(("{g}"),"CatB_G")
        ;... add more keys
        $vhotkey = 0
    EndIf

    ;......your code


WEnd




Func CatA_F() ;Category A 'F' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 736, 684, 728, 252, 1 )
    Sleep (100)
    Send ("I")
EndFunc


Func CatB_F() ;Cateogry B 'F' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 728, 252, 736, 684, 1 )
    Sleep (100)
    Send ("I")
EndFunc

Func CatA_G() ;Cateogry A 'G' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 736, 684, 728, 348, 1 )
    Sleep (100)
    Send ("I")
EndFunc

Func CatB_G() ;Category B 'G' Key
    Send ("I")
    Sleep (5001)
    MouseClickDrag("left", 728, 348, 736, 684, 1 )
    Sleep (100)
    Send ("I")
EndFunc

Func Terminate()
    Exit
 EndFunc

 

Will try this when I’m home, thank you

Link to comment
Share on other sites

6 hours ago, Dxnny said:

Will try this when I’m home, thank you

Hey, it's still not working :( When I press the button to switch to the other category, it spams the message repeatedly in console
Cat A hotkeys will now work
Cat B hotkey will now work
And it switches to the other category but doesn't let me switch back

Link to comment
Share on other sites

23 hours ago, SkysLastChance said:

I edited my response.

You just needed a sleep. 

Thank you, that fixed it. How would I go about making a hotkey to toggle pause the entire script so I can use the keys as normal to type.

I've tried adding a hotkey bound to this function:
 

Func TogglePause()
    $Paused = NOT $Paused
    ConsoleWrite("Script is now being paused/resumed" & @CRLF)
    While $Paused
        sleep(100)
    WEnd
EndFunc

And it writes to console but doesn't pause the script.

Edited by Dxnny
Link to comment
Share on other sites

#include <Misc.au3>
#include <AutoItConstants.au3>

HotKeySet(("{ESC}"),"Terminate")
HotKeySet(("{p}"),"TogglePause")
HotKeySet(("{f}"),"CatA_F")
HotKeySet(("{g}"),"CatA_G")


ConsoleWrite("'a' to switch hotkey" & @CRLF)
ConsoleWrite("'esc' to stop script" & @CRLF)
ConsoleWrite("'p' to toggle pause" & @CRLF)

Global $Paused,$vhotkey = 0




While Sleep (50)

    If _IsPressed("41") AND $vhotkey = 1 Then ;41 = a
        Sleep (100)
        ConsoleWrite("Cat A hotkeys will now work" & @CRLF)
        HotKeySet(("{f}"),"CatA_F")
        HotKeySet(("{g}"),"CatA_G")
        $vhotkey = 0


    ElseIf _IsPressed("41") AND $vhotkey = 0 Then ;41 = a
        Sleep (100)
        ConsoleWrite("Cat B hotkeys will now work" & @CRLF)
        HotKeySet(("{f}"),"CatB_F")
        HotKeySet(("{g}"),"CatB_G")
        $vhotkey = 1
    EndIf

WEnd




Func CatA_F()
    ConsoleWrite("A_F" & @CRLF)
EndFunc


Func CatB_F()
    ConsoleWrite("B_F" & @CRLF)
EndFunc

Func CatA_G()
    ConsoleWrite("A_G" & @CRLF)
EndFunc

Func CatB_G()
    ConsoleWrite("B_G" & @CRLF)
EndFunc

Func Terminate()
    Exit
EndFunc


Func TogglePause()

   $Paused = NOT $Paused


    If $Paused = True Then
      ConsoleWrite("Hotkeys Disabled" & @CRLF)
      HotKeySet(("{f}"),"DisableHotKey")
      HotKeySet(("{g}"),"DisableHotKey")
      ;add hotkeys that you want to be disabled during the pause

   ElseIf $Paused = False Then
        Switch $vhotkey = $vhotkey
            Case $vhotkey = 0
                HotKeySet(("{f}"),"CatA_F")
                HotKeySet(("{g}"),"CatA_G")
                ;add  Cat A Keys`
                ConsoleWrite("Hotkeys Enabled Cat A" & @CRLF)
            Case $vhotkey = 1
                HotKeySet(("{f}"),"CatB_F")
                HotKeySet(("{g}"),"CatB_G")
                ConsoleWrite("Hotkeys Enabled Cat Baaaaaaaaaa" & @CRLF)
                ;add Cat B keys
        EndSwitch
    EndIf
EndFunc

Func DisableHotKey()
    ConsoleWrite("Push 'a' to enable hotkeys" & @CRLF)
EndFunc

This will work.  😕 It's not pretty but it will work.

You just need to make sure you add the keys to the pause section as well. 

If you give a better idea of what you are trying to do someone a lot smarter than me can help you do this a lot easier and with a lot less code.  

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

21 hours ago, SkysLastChance said:
#include <Misc.au3>
#include <AutoItConstants.au3>

HotKeySet(("{ESC}"),"Terminate")
HotKeySet(("{p}"),"TogglePause")
HotKeySet(("{f}"),"CatA_F")
HotKeySet(("{g}"),"CatA_G")


ConsoleWrite("'a' to switch hotkey" & @CRLF)
ConsoleWrite("'esc' to stop script" & @CRLF)
ConsoleWrite("'p' to toggle pause" & @CRLF)

Global $Paused,$vhotkey = 0




While Sleep (50)

    If _IsPressed("41") AND $vhotkey = 1 Then ;41 = a
        Sleep (100)
        ConsoleWrite("Cat A hotkeys will now work" & @CRLF)
        HotKeySet(("{f}"),"CatA_F")
        HotKeySet(("{g}"),"CatA_G")
        $vhotkey = 0


    ElseIf _IsPressed("41") AND $vhotkey = 0 Then ;41 = a
        Sleep (100)
        ConsoleWrite("Cat B hotkeys will now work" & @CRLF)
        HotKeySet(("{f}"),"CatB_F")
        HotKeySet(("{g}"),"CatB_G")
        $vhotkey = 1
    EndIf

WEnd




Func CatA_F()
    ConsoleWrite("A_F" & @CRLF)
EndFunc


Func CatB_F()
    ConsoleWrite("B_F" & @CRLF)
EndFunc

Func CatA_G()
    ConsoleWrite("A_G" & @CRLF)
EndFunc

Func CatB_G()
    ConsoleWrite("B_G" & @CRLF)
EndFunc

Func Terminate()
    Exit
EndFunc


Func TogglePause()

   $Paused = NOT $Paused


    If $Paused = True Then
      ConsoleWrite("Hotkeys Disabled" & @CRLF)
      HotKeySet(("{f}"),"DisableHotKey")
      HotKeySet(("{g}"),"DisableHotKey")
      ;add hotkeys that you want to be disabled during the pause

   ElseIf $Paused = False Then
        Switch $vhotkey = $vhotkey
            Case $vhotkey = 0
                HotKeySet(("{f}"),"CatA_F")
                HotKeySet(("{g}"),"CatA_G")
                ;add  Cat A Keys`
                ConsoleWrite("Hotkeys Enabled Cat A" & @CRLF)
            Case $vhotkey = 1
                HotKeySet(("{f}"),"CatB_F")
                HotKeySet(("{g}"),"CatB_G")
                ConsoleWrite("Hotkeys Enabled Cat Baaaaaaaaaa" & @CRLF)
                ;add Cat B keys
        EndSwitch
    EndIf
EndFunc

Func DisableHotKey()
    ConsoleWrite("Push 'a' to enable hotkeys" & @CRLF)
EndFunc

This will work.  😕 It's not pretty but it will work.

You just need to make sure you add the keys to the pause section as well. 

If you give a better idea of what you are trying to do someone a lot smarter than me can help you do this a lot easier and with a lot less code.  

Didn't seem to work 🤔

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