Jump to content

How to toggle a GUI window with a hotkey


Recommended Posts

#include <GUIConstants.au3>

While 1
    If WinActive("Untitled - Notepad") Then
        HotKeySet("{F1}", "help")
    Else
        HotKeySet("{F1}")
    EndIf
    
    ; other stuff...
    ; other stuff...
    ; other stuff...
    ; other stuff...

    Sleep(100)
WEnd

Func Help()
    GUICreate("Help",-1,-1,-1,-1)
    GuiCtrlCreateLabel("help-help-help :-)", 140, 25)
    GUISetState (@SW_SHOW)
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE
            GUISetState (@SW_HIDE)
            ExitLoop
        EndSelect
    Wend
EndFunc

I know very little about creating GUI's. I'd appreaciate if someone could offer help with this.

I'd like the following:

* F1-key will be set as a hotkey for this function only if another application (for example Notepad) is an active window

* F1-key then will toggle open/close window of my GUI

* The GUI will contain nothing, but several labels

* If I press F1-key too many times quickly, or if I sit on the F1-key for a bit too long, it should not crash the script :-)

Edited by Noob
Link to comment
Share on other sites

Normally, I don't provide the whole script, but since this is a small request, I will generously provide what you've asked:

#include <GUIConstants.au3>

$HelpOn = 0
$GUI_Create = 0
HotKeySet("{F1}", "Help")

While 1
    Sleep(100)
WEnd

Func Help()
    If $HelpOn = 0 Then
        $HelpOn = 1
        If $GUI_Create = 0 Then
            $GUI_Create = 1
            GUICreate("Help")
            GUICtrlCreateLabel("this is a label", 10, 10)
            GUISetState()
        EndIf
        GUISetState(@SW_SHOW)
        While 1
            $msg = GUIGetMsg()
            Select
                Case $msg = $GUI_EVENT_CLOSE
                    $HelpOn = 0
                    GUISetState(@SW_HIDE)
            EndSelect
        WEnd
    Else
        GUISetState(@SW_HIDE)
        $HelpOn = 0
    EndIf
EndFunc

Once again, normally people do not respond to Topic's that asking for scripts/scraps.

Kurt :whistle:

Edited by _Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

Kurt, I thank you very much for taking the time to respond. However, while your code looks MUCH better than the silly variants I tried to create myself, it still gets stuck at the same place. Basically, the problem is that once I open/close the GUI window, the program is stuck in the function. It never leaves the function. I cannot do other stuff with the program, only the opening and closing of the GUI :-)

The bellow example gets stuck at the line $msg = GUIGetMsg()

I need it to exit the function, so I added the ExitLoop there, but it still doesn't exit the function

#include <GUIConstants.au3>

Opt ("TrayIconDebug", 1)  ; use this for debugging

$HelpOn = 0
$GUI_Create = 0


While 1
    If WinActive("Untitled - Notepad") Then
        HotKeySet("{F1}", "help")
    Else
        HotKeySet("{F1}")
    EndIf
    
    ; other stuff...
    ; other stuff...
    ; other stuff...
    ; other stuff...

    Sleep(100)
WEnd

Func Help()
    If $HelpOn = 0 Then
        $HelpOn = 1
        If $GUI_Create = 0 Then
            $GUI_Create = 1
            GUICreate("Help")
            GUICtrlCreateLabel("this is a label", 10, 10)
            GUISetState()
        EndIf
        GUISetState(@SW_SHOW)
        While 1
            $msg = GUIGetMsg()
            Select
                Case $msg = $GUI_EVENT_CLOSE
                    $HelpOn = 0
                    GUISetState(@SW_HIDE)
                    ExitLoop
            EndSelect
        WEnd
    Else
        GUISetState(@SW_HIDE)
        $HelpOn = 0
    EndIf
EndFunc
Edited by Noob
Link to comment
Share on other sites

This can be easily customized. If you think about it, once F1 is pressed, it loops until $GUI_EVENT_CLOSE is returned:

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            $HelpOn = 0
            GUISetState(@SW_HIDE)
            ExitLoop ;We realize here that the loop cannot be exited unless $GUI_EVENT_CLOSE is returned
    EndSelect
WEndoÝ÷ Ù8^­çè­æ¥«y©e˧¶Ú¢+%¢nØ¥¶°whÂ+'¢Z'êÛiËb½ïÛ²­².Ùh¢K(ëax%G­+ºÚ"µÍÚ[ÛYH  ÑÕRPÛÛÝ[Ë]LÉÝÂÜ
    ][ÝÕ^RXÛÛXYÉ][ÝËJHÈÙHÈÜXYÙÚ[ÂÌÍÒ[ÛHÌÍÑÕRWÐÜX]HHÚ[HBYÚ[XÝ]J   ][ÝÕ[]YHÝY   ][ÝÊH[ÝÙ^TÙ]
    ][ÝÞÑ_I][ÝË    ][ÝÒ[ ][ÝÊB[ÙBÝÙ^TÙ]
    ][ÝÞÑ_I][ÝÊB[YÈÝÝYÈÝÝYÈÝÝYÈÝÝYÛY
L
BÑ[[È[

BY  ÌÍÒ[ÛH[ ÌÍÒ[ÛHBY    ÌÍÑÕRWÐÜX]HH[ ÌÍÑÕRWÐÜX]HHBÕRPÜX]J    ][ÝÒ[ ][ÝÊBÕRPÝÜX]SX[
    ][ÝÝÈÈHX[   ][ÝËLL
BÕRTÙ]Ý]J
B[YÕRTÙ]Ý]JÕ×ÔÒÕÊBÚ[HÚ[XÝ]J ][ÝÒ[ ][ÝÊB ÌÍÛÙÈHÕRQÙ]ÙÊ
BÙ[XÝØÙH    ÌÍÛÙÈH ÌÍÑÕRWÑUSÐÓÔÑB ÌÍÒ[ÛHÕRTÙ]Ý]JÕ×ÒQJB^]ÛÜ[Ù[XÝÑ[[ÙBÕRTÙ]Ý]JÕ×ÒQJB  ÌÍÒ[ÛH[Y[[

Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

To test this, scroll over the autoit icon & check what line it's at when the GUI is active, and when it is not.

Kurt

:whistle: it's at the sleep(100) line, where it should be :P

Thanks SO MUCH, Kurt!!!

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