Jump to content
Sign in to follow this  
Klovis

2 Hotkeys for the same Key

Recommended Posts

Klovis

What I want to do is have a hotkey turned on or off, so not really two hotkeys but depending on if the button says on or off I want the hotkey to be turned on or off. Here's my attempt.

Mt Attempt:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("HotKeyOnOrOff", 76, 26, 192, 114)
$Button1 = GUICtrlCreateButton("On", 0, 0, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

HotKeySet("a", "_main")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $r = GUICtrlRead($Button1)
            If $r = "On" Then GUICtrlSetData($Button1, "Off")
            If $r = "Off" Then GUICtrlSetData($Button1, "On")
    EndSwitch
WEnd

Func _main()
    If $r = "On" Then
        Send("a")
    EndIf
    If $r = "Off" Then
        Send("")
    EndIf
EndFunc

Share this post


Link to post
Share on other sites
smartee

Hi Klovis,

Calling HotKeySet with no second parameter turns it off :)

See the help-file for details

-smartee

Share this post


Link to post
Share on other sites
smartee

Ok here's an example :)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("a", "_ConsoleW")

Global $iCount = 0

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 50)
$Checkbox1 = GUICtrlCreateCheckbox("Activate HotKey", 10, 10, 230, 30)
GUICtrlSetState(-1, $GUI_CHECKED)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Checkbox1
            If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
                HotKeySet("a", "_ConsoleW")
            Else
                HotKeySet("a")
            EndIf
    EndSwitch
WEnd

Func _ConsoleW()
    $iCount += 1
    ConsoleWrite($iCount & ". You pressed the HotKey" & @CRLF)
EndFunc   ;==>_ConsoleW

Share this post


Link to post
Share on other sites
Klovis

This is what I got from your reply and it doesnt work :/

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("HotKeyOnOrOff", 76, 26, 192, 114)
$Button1 = GUICtrlCreateButton("On", 0, 0, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $r = GUICtrlRead($Button1)
            If $r = "On" Then
                GUICtrlSetData($Button1, "Off")
                HotKeySet("a", "_main")
            EndIf
            If $r = "Off" Then
                GUICtrlSetData($Button1, "On")
                HotKeySet("a")
            EndIf
    EndSwitch
WEnd

Func _main()
    Send("a")
EndFunc

Share this post


Link to post
Share on other sites
smartee

Did you try the example i posted? How did that work out?

:) don't send the key that is your hot-key without turning off your hot-key first else it will go into a recursive loop until AutoIt crashes.

Share this post


Link to post
Share on other sites
Klovis

Sorry didnt see your reply when you posted it :)

Share this post


Link to post
Share on other sites
Klovis

works good don't know much about check boxes but I'll see if I can decipher the code

Share this post


Link to post
Share on other sites
smartee

Ok, here you go, I modified your code with the corrections i advised in post#5, :)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("a", "_main") ;On by default

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("HotKeyOnOrOff", 155, 45)
$Button1 = GUICtrlCreateButton("On", 40, 10, 75, 25)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $r = GUICtrlRead($Button1)
            If $r = "On" Then
                GUICtrlSetData($Button1, "Off")
                HotKeySet("a", "_main")
            EndIf
            If $r = "Off" Then
                GUICtrlSetData($Button1, "On")
                HotKeySet("a")
            EndIf
    EndSwitch
WEnd

Func _main()
    HotKeySet("a") ;turn off hotkey before sending the same key
    Send("a")
    HotKeySet("a", "_main") ;turn it back on
EndFunc   ;==>_main

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.