Jump to content

Trap the ENTER key via GUISetAccelerators() when in EventMode


AndyS01
 Share

Go to solution Solved by AndyS01,

Recommended Posts

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

 

 

Link to comment
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

Link to comment
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
Link to comment
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?

 

Link to comment
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.

. . . . .

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

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

  • Solution

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