Sign in to follow this  
Followers 0
AndyS01

Trap the ENTER key via GUISetAccelerators() when in EventMode

8 posts in this topic

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

 

 

Share this post


Link to post
Share on other sites



You can't use the same hotkey or accellerator for 2 controls at the same time.


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

Share this post


Link to post
Share on other sites

Try this:

#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 <WinAPI.au3>
#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, $iID_Enter

_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()

    GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft')

    $iID_Enter = GUICtrlCreateDummy()
    GUICtrlSetOnEvent($iID_Enter, "handle_Enter")
    Local $aAccelKeys[1][2]
    $aAccelKeys[0][0] = "{ENTER}"
    $aAccelKeys[0][1] = $iID_Enter

    GUISetAccelerators($aAccelKeys)

    While (1)
        Sleep(250)
    WEnd
EndFunc   ;==>_Main

Func ExitStageLeft()
    Exit (0)
EndFunc   ;==>ExitStageLeft

Func handle_Enter()
  Local $hCtrl = ControlGetHandle( $hGUI, "", ControlGetFocus( $hGUI ) )
  Local $iCtrl = _WinAPI_GetDlgCtrlID( $hCtrl )
  Switch $iCtrl
    Case $iID_list1
      ConsoleWrite("+++: handle_List1() entered" & @CRLF)
    Case $iID_input1
      ConsoleWrite("+++: handle_Input1() entered" & @CRLF)
  EndSwitch
EndFunc
1 person likes this

Share this post


Link to post
Share on other sites

Thank you very much!  Using a dummy control to handle the ENTER key only when it's pressed somewhere on the main GUI did the trick.  I'll add this little gem to my collection of tips and workarounds.

 

Share this post


Link to post
Share on other sites

also have a look >here


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

I ran into a problem implementing LarsJ's code.  If I don't handle the ENTER key for a certain control and I want that control to receive an actual ENTER key event, the ENTER key is ignored.  If I Send("{ENTER}") to those controls, I get into an infinite loop.

Here is my handler code:

Func handle_ENTERKey($iCtrl)
    local $handled = False

    Switch $iCtrl
        Case $iID_list1
            ConsoleWrite("+++: handle_List1() entered" & @CRLF)
            $handled = true
        Case $iID_input1
            ConsoleWrite("+++: handle_Input1() entered" & @CRLF)
            ;;$handled = true
    EndSwitch

    if (not $handled) Then
        Send("{CRLF}")
    EndIf
EndFunc   ;==>handle_ENTERKey
 

I call this code from the dummy control's handle_ENTER() handler, with $iCtrl as its parameter..

If I handle the event, everything is OK, but if I want the ENTER key to go into my list control, sending a "{ENTER}" key gets me into a loop because that will call the ENTER key handler again.

How can I pass the ENTER key to controls I don't want to do any special handling for?

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I ran into a problem implementing LarsJ's code.  If I don't handle the ENTER key for a certain control and I want that control to receive an actual ENTER key event, the ENTER key is ignored.  If I Send("{ENTER}") to those controls, I get into an infinite loop.

. . . . .

How can I pass the ENTER key to controls I don't want to do any special handling for?

 

you should disable accelerators,  ControlSend {ENTER} to control with focus and reenable accelerators again

....have a look to link I posted above ....

edit:

also look to post >#8 of the above topic for an explanation

Edited by PincoPanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Right-o.  I changed my Enter key handler to look something like this:

Func handle_ENTERKey($iCtrl)
    local $handled = False

    Switch $iCtrl
        Case $iID_list1
            ConsoleWrite("+++: ENTER while on list1" & @CRLF)
            ;;  $handled = true
        Case $iID_input1
            ConsoleWrite("+++: ENTER while on Input1" & @CRLF)
            $handled = true
    EndSwitch

    if (not $handled) Then
        GUISetAccelerators(0)
        ConsoleWrite("+++: sending {ENTER} to control ID " & $iCtrl & @CRLF)
        Send("{ENTER}")
        GUISetAccelerators($AccelKeys) ;
    EndIf
EndFunc   ;==>handle_ENTERKey

Works like I want it to.  Thanks, PincoPanco

Edited by AndyS01

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  
Followers 0

  • 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?
    • kcvinu
      By kcvinu
      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.