Jump to content
Sign in to follow this  
Phenom

assign hotkeys to GUI buttons

Recommended Posts

Phenom

Can GUI controls act as hotkeys?

Edited by Phenom

Share this post


Link to post
Share on other sites
Melba23

Phenom,

A HotKey will (in almost all circumstances) interrupt whatever the script is doing to run the code associated with it.

How a GUI control reacts depends on the mode you use:

- OnEvent mode: The control acts like a HotKey and interrupts the current function: :mellow:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

$hButton = GUICtrlCreateButton("Interrupt", 10, 10, 80, 30)
GUICtrlSetOnEvent($hButton, "_On_Interrupt")

GUISetState()

_Long_Function() ; Start a long function

; Enter an infinite loop
While 1
    Sleep(10)
WEnd

Func _Long_Function()
    $iBegin = TimerInit()
    While TimerDiff($iBegin) < 100000
        ConsoleWrite(@SEC & @CRLF)
        Sleep(1000)
    WEnd
EndFunc

Func _On_Interrupt()
    MsgBox(0, "Hi", "Interrupted")
EndFunc

Func CLOSEClicked()
    Exit
EndFunc

- GetMessage mode: Control clicks are queued and you cannot break into a running function easily. :party:

Does that answer your question? :P

M23

Edit: typnig!

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
picea892

He might mean Guictrlsetaccelerators....

; A simple custom messagebox that uses the MessageLoop mode

#include <GUIConstantsEx.au3>

GUICreate("Custom Msgbox", 210, 80)

GUICtrlCreateLabel("Please click a button!", 10, 10)
$YesID = GUICtrlCreateButton("Yes", 10, 50, 50, 20)
$NoID = GUICtrlCreateButton("No", 80, 50, 50, 20)
$ExitID = GUICtrlCreateButton("Exit", 150, 50, 50, 20)

; Set accelerators for Ctrl+y and Ctrl+n
Dim $AccelKeys[2][2]=[["^y", $YesID], ["^n", $NoID]]
GUISetAccelerators($AccelKeys)

GUISetState()  ; display the GUI

Do
    $msg = GUIGetMsg()

    Select
        Case $msg = $YesID
            MsgBox(0, "You clicked on", "Yes")
        Case $msg = $NoID
            MsgBox(0, "You clicked on", "No")
        Case $msg = $ExitID
            MsgBox(0, "You clicked on", "Exit")
        Case $msg = $GUI_EVENT_CLOSE
            MsgBox(0, "You clicked on", "Close")
    EndSelect
Until $msg = $GUI_EVENT_CLOSE Or $msg = $ExitID

Share this post


Link to post
Share on other sites
Phenom

OnEvent mode did the trick. Thanks. :mellow:

Share this post


Link to post
Share on other sites
AndreyS

Hi developers! Tell me, please, why the next small change example, Button does not work and how to make it work? Functions usually start with buttons. I would be very grateful!

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

$hButton = GUICtrlCreateButton("Interrupt", 10, 10, 80, 30)
GUICtrlSetOnEvent($hButton, "_On_Interrupt")

$hButton2 = GUICtrlCreateButton("Interrupt2", 100, 10, 80, 30)
GUICtrlSetOnEvent($hButton2, "_Long_Function")

GUISetState()

; Enter an infinite loop

While 1
Sleep(10)
WEnd

Func _Long_Function()
$iBegin = TimerInit()
While TimerDiff($iBegin) < 100000
     ConsoleWrite(@SEC & @CRLF)
     Sleep(1000)
WEnd
EndFunc


Func _On_Interrupt()
MsgBox(0, "Hi", "Interrupted")
EndFunc

Func CLOSEClicked()
Exit
EndFunc
Edited by AndreyS

Share this post


Link to post
Share on other sites
Melba23

AndreyS,

The Interrupting a running function tutorial in the Wiki will explain all. :)

But do ask again if you still have problems. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
AndreyS

Thank you! This is exactly what I'd had! :)

Not an easy decision, of course, but probably the best in the language Autoit.

Tell me more, please, how to recognize / catch pressing the close button (the event $ GUI_EVENT_CLOSE) using the GUIRegisterMsg ($WM_COMMAND, "_WM_COMMAND")?

Share this post


Link to post
Share on other sites
Melba23

AndreyS,

how to recognize / catch pressing the close button (the event $ GUI_EVENT_CLOSE) using the GUIRegisterMsg

To do that you use the WM_SYCOMMAND message like this: ;)

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

$hGUI = GUICreate("Test", 500, 500)

GUISetState()

GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

While 1
    Sleep(10)
WEnd

Func _WM_SYSCOMMAND($hWnd, $iMsg, $wParam, $lParam)

    Switch $wParam
        Case 0xF060
            MsgBox(0, "Hi", "[X] pressed")
            Exit
    EndSwitch

EndFunc

All clear? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
AndreyS

Yes, very grateful! All clear so far! :rolleyes:

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  

×