Jump to content

React on "ENTER" for combination of input and button


Go to solution Solved by Melba23,

Recommended Posts

Posted

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

  • Moderators
  • Solution
Posted

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Posted

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

  • Moderators
Posted

Autoseek,

Glad we could help. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...