Sign in to follow this  
Followers 0
Autoseek

React on "ENTER" for combination of input and button

7 posts in this topic

Hello,

I apologize for asking a (probably) most trivial question, but I am simply not getting anywhere. :

This is the basic setup:

Local $hGUI         = GUICreate("Tracker", 380, 300)
Local $iAddInput    = GUICtrlCreateInput("", 15, 15, 200, 18)
Local $iAddButton   = GUICtrlCreateButton("New", 230, 15, 60, 20)

I have an input field, and if the "New" button is clicked, the input is used for further processing:

While 1
   $iMsg = GUIGetMsg()
   Switch $iMsg
   Case $GUI_EVENT_CLOSE
      ExitLoop

   Case $iAddButton
      ; do something

   EndSwitch
WEnd

That is all working fine.

Now, I would like that the "Case $iAddButton" part is executed also, when the user enters "ENTER" (or "RETURN") when typing in the input field.

I am afraid I don't understand the basic idea on how to implement this. I have reviewed many examples involving _IsPressed() and GUIRegisterMsg($WM_COMMAND, ...), but I am not getting at what I need to do in my specific example.

Would anyone have a code snippet or a pointer to a webpage available, that explains this? That would be greatly appreciated!

Thank you!

Best regards

Reiner

Share this post


Link to post
Share on other sites



Autoseek,

First, welcome to the AutoIt forums. :)

The easiest way to do this is to use an accelerator key and check if the focus is on the required input:

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

$hGUI = GUICreate("Tracker", 380, 300)

$cAddInput = GUICtrlCreateInput("", 15, 15, 200, 18)
$hAddInput = GUICtrlGetHandle($cAddInput)

$cExtraInput = GUICtrlCreateInput("", 15, 45, 200, 18)

$cAddButton = GUICtrlCreateButton("New", 230, 15, 60, 20)

$cEnterPressed = GUICtrlCreateDummy()

GUISetState()

Global $aAccelKeys[][2] = [["{ENTER}", $cEnterPressed]]
GUISetAccelerators($aAccelKeys)

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $cEnterPressed
            If _WinAPI_GetFocus() = $hAddInput Then
                ; Run the button case
                ContinueCase
            EndIf
        Case $cAddButton
            MsgBox($MB_SYSTEMMODAL, "Hi", "Something should happen")
    EndSwitch
WEnd
You can check that if the focus is on the other input then the {ENTER} key does not action. ;)

All clear5? :)

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

#3 ·  Posted (edited)

See here for a snippet I provided just the other day for using Enter on an input field.

Edit: Dang, too slow :)

Edited by JLogan3o13

When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

Thank you both so much. :sweating:

That is really a big help! And, thank you especially for how to solve this in the Switch/Case context using the ContinueCase.

Oh boy, I am really happy. Looks like I can finish this today! :bike:

Thanks!

Best regards

Reiner

Share this post


Link to post
Share on other sites

Autoseek,

Glad we could help. :)

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

#7 ·  Posted (edited)

And, to confirm, it is working perfectly with my application :)  Personally, I like that "ContinueCase" a lot.

Also, thank you for the suggestion with $BS_DEFPUSHBUTTON. This may also be very useful one day. (For this particular application, I need to confirm the focus.)

Again, you made my day!

Cheers

Reiner

Edited by Autoseek

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