Jump to content

Help Needed with GUISetAccelerators


vindicta
 Share

Recommended Posts

Hello everyone,

I am trying to setup a GUI accelerator to close the utility as soon as esc is pressed.  It works fine with hotkey but I want to make sure that the utility only exits if its window was active when esc was pressed. My utility has 2 buttons, Backup and Restore.

The exit function checks if the button clicked was Backup or Restore and then displays an error message accordingly.

If pressed backup, $button = 1

if pressed restore, $button = 2

the value of $button is set inside backup() or restore() functions

Opt("GUIOnEventMode", 1)
Opt("GUICoordMode", 1)

$Form1 = GUICreate("Form1", 419, 124, 238, 194, $WS_DLGFRAME)
$B_backup = GUICtrlCreateButton("Backup", 48, 40, 145, 41)
$B_restore = GUICtrlCreateButton("Restore", 224, 40, 145, 41)

;================ > HotKeySet('{ESC}', "terminate")
GUICtrlSetOnEvent($B_backup, "Backup")
GUICtrlSetOnEvent($B_restore, "Restore")
GUISetState(@SW_SHOW)
Dim $accelKey[1][2] = [["{ESC}", terminate()]]
GUISetAccelerators($accelKey)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

Exit function:

func terminate()
Do
    $msg = GUIGetMsg()
Until $msg <> 0

While 1
        If $button = 1 Then
            ProcessClose("robocopy.exe")
            MsgBox(16, "Abort!", "Backup Aborted!")
            exit
        ElseIf $button = 2 Then
            ProcessClose("robocopy.exe")
            MsgBox(16, "Abort!", "Restore Aborted!")
            exit
        Else
            While 1
                For $i = 3 To 1 Step -1
                    SplashTextOn("Closing Utility...", $i, 130, 54, -1, -1, 0, "Calibri", "", 900)
                    Sleep(1000)
                Next
                ExitLoop
            WEnd
            SplashOff()
            Exit
        Endif
WEnd

What am I doing wrong here? The utility closes as soon as it launches with Splash text.

Edited by vindicta
missing endif added.
Link to comment
Share on other sites

Accelerator keys work on controls, not on functions. You have to create a control for it to action, and a function associated with that control. Something like this,

$id_Dummy = GUICtrlCreateDummy() ; <<<<<<<<<<<<<<<<<<<<
GUICtrlSetOnEvent($id_Dummy, "terminate") ; <<<<<<<<<<<<<<<<<<<<
;================ > HotKeySet('{ESC}', "terminate")
GUICtrlSetOnEvent($B_backup, "Backup")
GUICtrlSetOnEvent($B_restore, "Restore")
GUISetState(@SW_SHOW)
Dim $accelKey[1][2] = [["{ESC}", $id_Dummy] ; <<<<<<<<<<<<<<<<
GUISetAccelerators($accelKey)

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thank you for this simple tutorial. Helped me understand accelerators better. One more question I have is that HotKey allowed me to exit right away, regardless of what the program was doing. I am assuming that it is not possible to do so with Accelerators? I ran backup function and hit the ESC key, nothing happened. I assumed it was waiting for copy to finish before it can run the exit function? If that's the case, is HotKey my only way out?

Link to comment
Share on other sites

While Using HotKey to exit, I added an extra if statement to confirm that the ESC was really pressed to exit the utility. This gives me at least a chance to continue with the backup/restore if I want to.

function terminate()
While 1
    If MsgBox(36, "", "You pressed ESC" & @CRLF & "Do you want to abort the operation?") = 6 Then
        If $button = 1 Then
            ProcessClose("robocopy.exe")
            MsgBox(16, "Abort!", "Backup Aborted!")
            Exit
        ElseIf $button = 2 Then
            ProcessClose("robocopy.exe")
            MsgBox(16, "Abort!", "Restore Aborted!")
            Exit
        Else
            While 1
                For $i = 3 To 1 Step -1
                    SplashTextOn("Closing Utility...", $i, 130, 54, -1, -1, 0, "Calibri", "", 900)
                    Sleep(1000)
                Next
                ExitLoop
            WEnd
            SplashOff()
            Exit
        EndIf
    Else
            Sleep(10)
            ExitLoop
    EndIf
Wend
Endfunc

Would still love to know if this can be achieved any other way.

Edited by vindicta
Link to comment
Share on other sites

Your confirm exit function could use some work. See this example.

#include <MsgBoxConstants.au3>
Global $ubound = 5 * @DesktopRefresh
_Init()
Func _Init()
    ProgressOn("Pretending to do some work.", "", "This is an example script. Press esc to exit.")
    HotKeySet("{esc}", "_ConfirmExit")
EndFunc   ;==>_Init
While 1
    For $i = 1 To $ubound
        ProgressSet($i / $ubound * 100)
        Sleep(1000 / @DesktopRefresh)
    Next
    Sleep(550)
WEnd
Func _ConfirmExit()
    HotKeySet("{esc}")
    ProgressOff()
    If MsgBox(BitOR($MB_YESNO, $MB_SETFOREGROUND), @ScriptName, "Are you sure you want to exit?") = $IDYES Then
        Exit
    Else
        _Init()
    EndIf
EndFunc   ;==>_ConfirmExit

 

 

 

Link to comment
Share on other sites

1 hour ago, BetaLeaf said:

Your confirm exit function could use some work. See this example.

#include <MsgBoxConstants.au3>
Global $ubound = 5 * @DesktopRefresh
_Init()
Func _Init()
    ProgressOn("Pretending to do some work.", "", "This is an example script. Press esc to exit.")
    HotKeySet("{esc}", "_ConfirmExit")
EndFunc   ;==>_Init
While 1
    For $i = 1 To $ubound
        ProgressSet($i / $ubound * 100)
        Sleep(1000 / @DesktopRefresh)
    Next
    Sleep(550)
WEnd
Func _ConfirmExit()
    HotKeySet("{esc}")
    ProgressOff()
    If MsgBox(BitOR($MB_YESNO, $MB_SETFOREGROUND), @ScriptName, "Are you sure you want to exit?") = $IDYES Then
        Exit
    Else
        _Init()
    EndIf
EndFunc   ;==>_ConfirmExit

 

Thank you for improving on this. :thumbsup:
I am really a noob programmer and just stepping into creating automated utilities. I will try to figure out how all this is working and try to make it work with my rest of the script.
Thanks again!

Edited by vindicta
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

×
×
  • Create New...