Jump to content

Input Control Accepting ENTER


Recommended Posts

This is a newbie question.

I want an input control to accept the text typed by simply pressing the ENTER key, instead of having to click an "OK" button somewhere else. From what I understand, I need to register an event handler/look for message correponding to the pressing the ENTER key. How should I do that?

I guess this is what I should do but I still have a problem:

Description for $ES_WANTRETURN says:

Specifies that a carriage return be inserted when the user presses the ENTER key while typing text into a multiline edit control in a dialog box. If you do not specify this style, pressing the ENTER key has the same effect as pressing the dialog box's default push button. This style has no effect on a single-line edit control.

So this means by looking for the click event/message for my default push button, I can identify the ENTER key was just pressed.

But what is this default push button?

Link to comment
Share on other sites

Is this what you mean?

GUICreate("test",300,100)
$input = GUICtrlCreateInput("input test",10,10,250,50)
GUISetState()
#Include <Misc.au3>

Do
 Sleep(1)
Until _IsPressed("0D")

MsgBox(0,"test","The input was: "&GUICtrlRead($input))

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Can I listen for some specific events/messages rather than doing infinite polling checking _IsPressed()? The issue is I have to decide when my input control get focus to start my polling and somehow stop it when the focus leave my control.

Maybe use ControlGetFocus() to determine what has focus?

/edit: example, this only shows the input when enter was pressed AND the focus is on the first edit box

GUICreate("test",300,100)
$input1 = GUICtrlCreateInput("input test",10,10,250,30)
$input2 = GUICtrlCreateInput("input test",10,40,250,30)
GUISetState()
#Include <Misc.au3>

Do
 Sleep(1)
Until ControlGetFocus("test","")="Edit1" And _IsPressed("0D")

MsgBox(0,"test","The input was: "&GUICtrlRead($input1))
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

If you create a button on your GUI that is an "OK" or "Start" etc you can make it the "default push button by adding this style option $BS_DEFPUSHBUTTON.

GUICtrlCreateButton ( "text", left, top [, width [, height [, style [, exStyle]]]] )

Hope that helps :)

Link to comment
Share on other sites

This is a newbie question.

I want an input control to accept the text typed by simply pressing the ENTER key, instead of having to click an "OK" button somewhere else. From what I understand, I need to register an event handler/look for message correponding to the pressing the ENTER key. How should I do that?

Here's a method based on Zedna's cool idea of using Accelerator Keys tied to dummy controls.

#include <GuiConstants.au3>

$gui = GUICreate("test", 300, 100)
$input1 = GUICtrlCreateInput("input test", 10, 10, 250, 30)
$input2 = GUICtrlCreateInput("another input test", 10, 40, 250, 30)
$accEnter = GUICtrlCreateDummy()
Dim $AccelKeys[1][2] = [["{ENTER}", $accEnter]]
GUISetAccelerators($AccelKeys)
GUISetState()

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $accEnter
            $currCtrl = ControlGetFocus($gui)
            $currVal = ControlGetText($gui, "", $currCtrl)
            MsgBox(0, "", "The input was ''" & $currVal & "''")
    EndSwitch

WEnd
Edited by ResNullius
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

  • Recently Browsing   0 members

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