Jump to content
Sign in to follow this  
sensalim

Inputbox and {ENTER}?

Recommended Posts

sensalim

I have a simple GUI that has 2 controls: input and button.

How do I make it so that if I enter a text in the input and hit the ENTER button, it would do as if I press the button?

Thanks!

Share this post


Link to post
Share on other sites
Madza91

Set button as $BS_DEFPUSHBUTTON

Example:

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 386, 150, -1, -1)
$Input1 = GUICtrlCreateInput("Input1", 16, 40, 353, 21)
$Button1 = GUICtrlCreateButton("OK", 152, 88, 75, 25, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            MsgBox(0,"",GUICtrlRead($Input1))
    EndSwitch
WEnd

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites
sensalim

Ah very nice.

The description

"

Creates a push button with a heavy black border. If the button is in a dialog box, the user can select the button by pressing the ENTER key, even when the button does not have the input focus. This style is useful for enabling the user to quickly select the most likely option, or default.

"

is confusing to me.

Thank you.

Share this post


Link to post
Share on other sites
martin

Ah very nice.

The description

"

Creates a push button with a heavy black border. If the button is in a dialog box, the user can select the button by pressing the ENTER key, even when the button does not have the input focus. This style is useful for enabling the user to quickly select the most likely option, or default.

"

is confusing to me.

Thank you.

Whatever the description it doesn't work for me. The first time I press ENTER then it works, but after that the button does nothing if it doesn't have the focus.

One way would be to use HotKetSet like this

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 386, 150, -1, -1)
$Input2 = GUICtrlCreateInput("Input2", 16, 120, 353, 21)
$Input1 = GUICtrlCreateInput("Input1", 16, 40, 353, 21)
$Button1 = GUICtrlCreateButton("OK", 152, 80, 75, 25);, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW)

HotKeySet("{ENTER}", "MyEnter")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ConsoleWrite("Button pressed " & Hex(GUICtrlGetState($Input1)) & @CRLF)
    EndSwitch
WEnd

Func MyEnter()
    Local $nCtrlGetFocus = _GUICtrlGetFocus($Form1)
    If @error Then Return
    If $nCtrlGetFocus = $Input1 Then
        MsgBox(0, "1", GUICtrlRead($Input1))
    EndIf
    If $nCtrlGetFocus = $Input2 Then
        MsgBox(0, "2", GUICtrlRead($Input2))
    EndIf
EndFunc  ;==>MyEnter

Func _GUICtrlGetFocus($hWin)
    Local $aDLL = DllCall('User32.dll', 'int', 'GetDlgCtrlID', 'hwnd', _
            ControlGetHandle($hWin, '', ControlGetFocus($Form1)))
    If @error = 0 Then Return $aDLL[0]
    Return SetError(1, 0, '')
EndFunc  ;==>_GUICtrlGetFocus

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Madza91

This is bad, because is hotkey with that script for whole windows, not only for your script...

But here is new example :)

#include <GUIConstants.au3>
Opt("GUIOnEventMode", True)
$Form1 = GUICreate("AForm1", 389, 100, -1, -1)
$Input1 = GUICtrlCreateInput("Nesto", 8, 8, 369, 21, $ES_WANTRETURN)
$Label1 = GUICtrlCreateLabel(GUICtrlRead($Input1), 8, 32, 371, 17)
$button = GUICtrlCreateButton("OK",150,50,70,20, $BS_DEFPUSHBUTTON)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUICtrlSetOnEvent($Input1, "focus")
GUICtrlSetOnEvent($button, "ok")
GUISetState(@SW_SHOW)
func focus()
    GUICtrlSetData($Label1, GUICtrlRead($Input1))
    ControlFocus("", "", $Label1)
    ControlFocus("", "", $Input1)
EndFunc
func close()
    Exit
EndFunc
Func ok()
    MsgBox(0,"",GUICtrlRead($Input1))
EndFunc
While 1
    Sleep(10)
WEnd

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites
martin

This is bad, because is hotkey with that script for whole windows, not only for your script...

True but that's easily fixed.

EDIT: Even so, I think your second method is a better approach than my suggestion.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Madza91

This is bad, because is hotkey with that script for whole windows, not only for your script...

True but that's easily fixed.

Btw, how to do that ? I know for If winactive...


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites
martin

Btw, how to do that ? I know for If winactive...

I would do it something like this

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 386, 150, -1, -1)
$Input2 = GUICtrlCreateInput("Input2", 16, 120, 353, 21)
$Input1 = GUICtrlCreateInput("Input1", 16, 40, 353, 21)
$Button1 = GUICtrlCreateButton("OK", 152, 80, 75, 25);, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW)

HotKeySet("{ENTER}", "MyEnter")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ConsoleWrite("Button pressed " & Hex(GUICtrlGetState($Input1)) & @CRLF)
    EndSwitch
WEnd

Func MyEnter()
    if WinGetHandle(WinGetTitle("")) <> $Form1 Then
        hotkeyset("{ENTER}")
        Send("{ENTER}")
        HotKeySet("{ENTER}","MyEnter")
        Return
    EndIf
    
    Local $nCtrlGetFocus = _GUICtrlGetFocus($Form1)
    If @error Then Return
    If $nCtrlGetFocus = $Input1 Then
        MsgBox(0, "1", GUICtrlRead($Input1))
    EndIf
    If $nCtrlGetFocus = $Input2 Then
        MsgBox(0, "2", GUICtrlRead($Input2))
    EndIf
EndFunc ;==>MyEnter

Func _GUICtrlGetFocus($hWin)
    Local $aDLL = DllCall('User32.dll', 'int', 'GetDlgCtrlID', 'hwnd', _
            ControlGetHandle($hWin, '', ControlGetFocus($Form1)))
    If @error = 0 Then Return $aDLL[0]
    Return SetError(1, 0, '')
EndFunc ;==>_GUICtrlGetFocus

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Madza91

Great :D


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites
NELyon

How about this?

#include <GuiConstantsEx.au3>
$GUI = GUICreate("Test", 288, 100)
$hInput = GUICtrlCreateInput("Enter something here", 44,  20)
$hButton = GUICtrlCreateButton("Click or press enter", 95, 50)
Dim $hotkey[1][2] = [["{enter}", $hButton]]
GUISetAccelerators($hotkey)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hButton
            MsgBox(0, "Ok", "You typed "&GUICtrlRead($hInput))
    EndSwitch
WEnd

(Assuming you use Beta)

Share this post


Link to post
Share on other sites
ResNullius

How about this?

#include <GuiConstantsEx.au3>
$GUI = GUICreate("Test", 288, 100)
$hInput = GUICtrlCreateInput("Enter something here", 44,  20)
$hButton = GUICtrlCreateButton("Click or press enter", 95, 50)
Dim $hotkey[1][2] = [["{enter}", $hButton]]
GUISetAccelerators($hotkey)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hButton
            MsgBox(0, "Ok", "You typed "&GUICtrlRead($hInput))
    EndSwitch
WEnd

Also note this works best with beta 3.2.15 as after the first cycle using 3.2.10 the button responds as if clicked twice

Share this post


Link to post
Share on other sites
Madza91

One more solution :D

Btw.....3.2.15 :D

Where i can to download ?


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites
martin

How about this?

#include <GuiConstantsEx.au3>
$GUI = GUICreate("Test", 288, 100)
$hInput = GUICtrlCreateInput("Enter something here", 44,  20)
$hButton = GUICtrlCreateButton("Click or press enter", 95, 50)
Dim $hotkey[1][2] = [["{enter}", $hButton]]
GUISetAccelerators($hotkey)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hButton
            MsgBox(0, "Ok", "You typed "&GUICtrlRead($hInput))
    EndSwitch
WEnd

(Assuming you use Beta)

I forgot we had accelerators now.

I don't see how you can use them in the example I gave where you have 2 edit boxes and you want ENTER to give the result for whichever has the focus (but I would be interested if you have a way). In this situation n3nE's method is better provided you use OnEvents, otherwise it doesn't work so well and then I think the HotKeySet way is better.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Madza91

Martin's way (hotkeyset) is the best for my script because is simple and it isn't OnEvent :D

Edited by n3nE

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites
martin

(note, this will work only if the text in the input box has been changed)

Yes, that's the problem and it's why I think n3nE's suggestion is better because it works even if the text hasn't been changed.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
NELyon

I forgot we had accelerators now.

I don't see how you can use them in the example I gave where you have 2 edit boxes and you want ENTER to give the result for whichever has the focus (but I would be interested if you have a way). In this situation n3nE's method is better provided you use OnEvents, otherwise it doesn't work so well and then I think the HotKeySet way is better.

Took me a minute but...

#include <GuiConstantsEx.au3>
$GUI = GUICreate("Test", 288, 150)
$hInput = GUICtrlCreateInput("Enter something here", 44,  20)
$hInput2 = GUICtrlCreateInput("Or enter something here", 44, 100)
$hButton = GUICtrlCreateButton("Click or press enter", 95, 50)
Dim $hotkey[1][2] = [["{enter}", $hButton]]
GUISetAccelerators($hotkey)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hButton
            $hndl = ControlGetText("", "", ControlGetFocus($GUI))
            MsgBox(0, "Ok", "You typed "&$hndl)
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites
martin

Took me a minute but...

#include <GuiConstantsEx.au3>
$GUI = GUICreate("Test", 288, 150)
$hInput = GUICtrlCreateInput("Enter something here", 44,  20)
$hInput2 = GUICtrlCreateInput("Or enter something here", 44, 100)
$hButton = GUICtrlCreateButton("Click or press enter", 95, 50)
Dim $hotkey[1][2] = [["{enter}", $hButton]]
GUISetAccelerators($hotkey)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hButton
            $hndl = ControlGetText("", "", ControlGetFocus($GUI))
            MsgBox(0, "Ok", "You typed "&$hndl)
    EndSwitch
WEnd
Yup, that did it. Thanks.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
ResNullius

Yup, that did it. Thanks.

Also, see Zedna's work here http://www.autoitscript.com/forum/index.ph...st&p=493696

with associating accelerators to dummy controls.

I just finished a project where I made good use of the concept:

the GUI had only Input controls, no buttons, and the ENTER had to behave differently if the contents of the Input had changed or not.

Edited by ResNullius

Share this post


Link to post
Share on other sites
Madza91

Aha, i have it :D


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.