kcvinu

If GUISetAccelerators() for a GUI then, what is for a specific control ?

13 posts in this topic

Hi all,

I know GUISetAccelerators() function is helpful to set a hotkey for my GUI. But which function should i use for setting a hotkey for a specific control only. Say, i have 3 Textboxes and a button in my form. I want to display a msg box when i press the enter key while keyboard focus is in the first textbox. When i used GUISetAccelerators function, it is firing the event regardless the focuds of the control.


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites



kcvinu,

Just check which control has focus when the Accelerator key is actioned and only proceed if the correct one is focused.

M23

1 person likes this

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

@kcvinu You can use a dummy control for the purpose M23 mentioned :)

1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@Melba23 I have tried, but not succeeded. What is worng with this code ?

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

Local $hGUI = GUICreate("Window", 381, 237, -1, -1)
Local $hButton = GUICtrlCreateButton("Button", 97, 145, 192, 77)
Local $hInput = GUICtrlCreateInput("", 60, 37, 267, 35)
Local $hInput2 = GUICtrlCreateInput("", 60, 97, 267, 35)
Local $Accle = [["{ENTER}",$hButton]]
GUISetAccelerators($Accle)

GUISetState()

While 1
    $hMsg = GUIGetMsg()
    Switch $hMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hInput

        Case $hButton
            If GUICtrlGetState($hInput) = $GUI_FOCUS Then
                BClick()
            EndIf
    EndSwitch
WEnd


Func BClick()
    MsgBox(0,"","Button is Clicked")
EndFunc

 

Edited by kcvinu

My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

@kcvinu Because Case $hInput is empty :P


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

@TheDcoder , I am learing about dummy controls. 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

@kcvinu $hInput is a input control, not a dummy control... They are created using:

GUICtrlCreateDummy

:P


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

kcvinu,

I always use _WinAPI_GetFocus to test which control has it:

Case $hButton

            If _WinAPI_GetFocus() = GUICtrlGetHandle($hInput) Then
                BClick()
            EndIf

and that works for me.

M23

1 person likes this

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

#9 ·  Posted (edited)

Here is a live example which I use in my program:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Global $g_hMainGUI = GUICreate("Test")

Global $g_idMainGUI_YesDummy = GUICtrlCreateDummy()
Global $g_idMainGUI_NoDummy = GUICtrlCreateDummy()
Global $g_idMainGUI_TryAgainDummy = GUICtrlCreateDummy()
Global $g_idMainGUI_AbortDummy = GUICtrlCreateDummy()

Local $aAccelKeys[4][2] = [["y", $g_idMainGUI_YesDummy], ["n", $g_idMainGUI_NoDummy], ["t", $g_idMainGUI_TryAgainDummy], ["a", $g_idMainGUI_AbortDummy]]
GUISetAccelerators($aAccelKeys, $g_hMainGUI)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $g_idMainGUI_YesDummy
            MsgBox($MB_OK, "Test", "You pressed 'y' which means Yes!")

        Case $g_idMainGUI_NoDummy
            MsgBox($MB_OK, "Test", "You pressed 'n' which means No!")

        Case $g_idMainGUI_TryAgainDummy
            MsgBox($MB_OK, "Test", "You pressed 't' which means Try Again!")

        Case $g_idMainGUI_AbortDummy
            MsgBox($MB_OK, "Test", "You pressed 'a' which means Abort!")
    EndSwitch
WEnd

TD :D

Edited by TheDcoder
1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

@TheDcoder , Thanks for the example. 

@Melba23 , What i was planned is to display the MsgBox in 2 conditions.

1. when user presses enter after typing in text box

2. when user clicks the button with mouse. 

So this is the way i solved this problem. Please look this and tell me if there is any better way.

Case $hButton
        If _WinAPI_GetFocus() = GUICtrlGetHandle($hInput) Or _WinAPI_GetFocus() = GUICtrlGetHandle($hButton) Then
            BClick()
        EndIf

 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

@kcvinu My pleasure :)

1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

kcvinu,

I would use a dummy control like this:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Local $hGUI = GUICreate("Window", 381, 237, -1, -1)
Local $hButton = GUICtrlCreateButton("Button", 97, 145, 192, 77)
Local $hInput = GUICtrlCreateInput("", 60, 37, 267, 35)
Local $hInput2 = GUICtrlCreateInput("", 60, 97, 267, 35)
Local $cInputDummy = GUICtrlCreateDummy()

GUISetState()

Local $Accle = [["{ENTER}",$cInputDummy]]
GUISetAccelerators($Accle)

While 1
    $hMsg = GUIGetMsg()
    Switch $hMsg

        Case $GUI_EVENT_CLOSE
            Exit
        Case $cInputDummy

            If _WinAPI_GetFocus() = GUICtrlGetHandle($hInput) Then
                BClick()
            EndIf

        Case $hButton

            BClick()
    EndSwitch

WEnd



Func BClick()
    MsgBox(0,"","Button is Clicked")
EndFunc

M23

1 person likes this

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

@Melba23 Thanks a lot.  :)


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

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

  • Similar Content

    • John C.
      By John C.
      ;Adding $BS_DEFPUSHBUTTON doesn't work
              $idCopy_Data = GUICtrlCreateButton("Copy Data Only", $iButtonWidth_2, $aiGUISize[1] - $iButtonMargin, $iButtonWidth_2, 20, $BS_DEFPUSHBUTTON)
      ;Adding these two lines doesn't work either
               Local $aAccelKeys[2][2] = [["{enter}", $idCopy_Data], ["^{enter}", $idCopy_ID]]
              GUISetAccelerators($aAccelKeys)
       
    • vindicta
      By vindicta
      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.
    • Skysnake
      By Skysnake
      Hi All
      Question, the Help file contains this:
      So, is it necessary to put 
      GUISetAccelerators(0) ; Remove accelerator link in the GUI?  Once the GUI is deleted, the Accelerators are gone...?
      Enter like tab discussion contains this item.  Is it essential?
    • AndyS01
      By AndyS01
      I have a GUI with several input boxes and I want to trap the ENTER key so I can call handlers for each one.   However, when I put in a HotKeySet() to trap the ENTER key, it traps it OK, but I also get traps when I press the ENTER key anywhere else on my desktop, no matter what GUI has focus, so I changed the code to use GUISetAccelerators().  I cannot see how to attach a handler for the ENTER key for each of my input controls.   My real script uses event mode (Opt("GUIOnEventMode", 1)), so that's what I'm using in my test code.   Here is my test code: #NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseUpx=n #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <GUIConstantsEx.au3> #include <StructureConstants.au3> OnAutoItExitRegister("ExitStageLeft") Opt("GUICloseOnESC", 1) Opt("GUIEventOptions", 1) Opt("GUIOnEventMode", 1) ;; <===== Opt("WinTitleMatchMode", 1) Opt('MustDeclareVars', 1) Global $hWnd_list1, $hWnd_input1, $hGUI, $sWinTitle Global $iID_list1, $iID_input1 _Main() Exit (1) Func _Main() $sWinTitle = "ENTER key trap test" $hGUI = GUICreate($sWinTitle, 500, 230) GUICtrlCreateButton("Test", 5, 5, 50, 25) $iID_input1 = GUICtrlCreateInput("input1", 70, 5, 100, 25) $hWnd_input1 = GUICtrlGetHandle($iID_input1) $iID_list1 = GUICtrlCreateEdit("list1", 10, 40, 475, 190) $hWnd_list1 = GUICtrlGetHandle($iID_list1) GUICtrlSetData($iID_list1, "abcdefghijklmnopqrstuvwxyz" & @CRLF, 1) ConsoleWrite("+++: $iID_list1 = " & $iID_list1 & @CRLF) ConsoleWrite("+++: $iID_input1 = " & $iID_input1 & @CRLF) GUISetState() GUICtrlSetOnEvent($iID_list1, "handle_List1") GUICtrlSetOnEvent($iID_input1, "handle_Input1") GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft') Local $aAccelKeys[2][2] $aAccelKeys[0][0] = "{ENTER}" $aAccelKeys[0][1] = $iID_input1 $aAccelKeys[1][0] = "{ENTER}" $aAccelKeys[1][1] = $iID_list1 GUISetAccelerators($aAccelKeys) While (1) Sleep(250) WEnd EndFunc ;==>_Main Func ExitStageLeft() Exit (0) EndFunc ;==>ExitStageLeft Func handle_Input1() ConsoleWrite("+++: handle_Input1() entered" & @CRLF) EndFunc ;==>handle_Input1 Func handle_List1() ConsoleWrite("+++: handle_List1() entered" & @CRLF) EndFunc ;==>handle_List1