Jump to content

Register/Un-Register Hotkey


will88
 Share

Recommended Posts

Is there a simpler way of writing this other than having 2 While Loops?

What it does is, when the Gui is not active, it un-registers the hot key to prevent it from interfering with other things. And when the GUI is active, it will register the hot key.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
GUICreate($Main_Title,500,500)
GUISetState(@SW_SHOW,$Main_Title)
Dim $Save
$Main_Title=":Will88::"


Loop1()

Func Loop1()
HotKeySet("{Home}","Save")

While 1
If Not WinActive($Main_Title) Then
Loop2()
EndIf
$msg = GUIGetMsg()
If $msg = -3 Then Exit
If $Save=1 Then 
MsgBox(0,"","Save")
$Save=0
EndIf
WEnd
EndFunc

Func Loop2()
HotKeySet("{Home}")
While 1     
If WinActive($Main_Title) Then
Loop1()
EndIf
$msg = GUIGetMsg()
If $msg = -3 Then Exit
Sleep(1000)
WEnd
EndFunc

Func Save()
$Save=1
EndFunc

Yeah I know its not that organized

Edited by will88
Link to comment
Share on other sites

Something like this maybe? :)

$hwnd = GUICreate("Test")
GUISetState()

HotKeySet("{Home}", "hkeyfunc")
Global $HotkeyIsActive = True

While GUIGetMsg() <> -3
    
    $active = WinActive($hwnd)
    If Not $active And $HotkeyIsActive Then
        HotKeySet("{home}")
        $HotkeyIsActive = False
    ElseIf $active And Not $HotkeyIsActive Then
        HotKeySet("{home}", "hkeyfunc")
        $HotkeyIsActive = True
    EndIf
    
WEnd

Func hkeyfunc()
    MsgBox(0, "", "I'm active!")
EndFunc  ;==>hkeyfunc
Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Something like this maybe? :lmao:

$hwnd = GUICreate("Test")
GUISetState()

HotKeySet("{Home}", "hkeyfunc")
Global $HotkeyIsActive = True

While GUIGetMsg() <> -3
    
    $active = WinActive($hwnd)
    If Not $active And $HotkeyIsActive Then
        HotKeySet("{home}")
        $HotkeyIsActive = False
    ElseIf $active And Not $HotkeyIsActive Then
        HotKeySet("{home}", "hkeyfunc")
        $HotkeyIsActive = True
    EndIf
    
WEnd

Func hkeyfunc()
    MsgBox(0, "", "I'm active!")
EndFunc ;==>hkeyfunc
Thanks works great :)
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...