Sign in to follow this  
Followers 0
yutt

Event on hitting Enter

8 posts in this topic

#1 ·  Posted (edited)

This should be simple to do, but I'm not having any luck finding a way. My script includes an input box (the GUICtrlCreateInput type). I want an event to trigger when the user hits the Enter key while the input box has focus.

It's for a chat client I am trying, so the user will type their message and hit Enter to send it.

Edited by yutt

It is a waste of energy to be angry with a man who behaves badly, just as it is to be angry with a car that won't go. - Bertrand Russell

Share this post


Link to post
Share on other sites



You can do this by creating a button that has the $BS_DEFPUSHBUTTON style. This will make it the default button on the window, and pressing ENTER anywhere will have the effect of clicking it. From the help,

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.

If you don't want the users to see the button, I guess you can either set it hidden or out of the window bounds.

hope that helps

ben

Share this post


Link to post
Share on other sites

This should be simple to do, but I'm not having any luck finding a way. My script includes an input box (the GUICtrlCreateInput type). I want an event to trigger when the user hit the Enter key while the input box has focus.

It's for a chat client I am trying, so the user will type their message and hit Enter to send it.

<{POST_SNAPBACK}>

few ways to do this... you could use msgloop mode and
if $msg = $input and _IsPressed(xx) (xx is the hex number code for enter) then ...
or you could:

HotKeySet("{ENTER}","Shenanigans")
while 1
;other code
sleep(100)
wend

Func Shenanigans()
$a = ControlGetFocus("Untitled - Notepad")
if $a = controlid of inputbox here then
    ;more code
EndIf
EndFunc

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Define the Button to Send with the Style: $BS_DEFPUSHBUTTON

If the User presses Enter the "Send" Button gets Triggered...


Say: "Chuchichäschtli"My UDFs:_PrintImage UDF_WinAnimate UDFGruess Raindancer

Share this post


Link to post
Share on other sites

That should work fine, though not exactly what I had in mind. Is there a way to check which element has focus?

Wow, this community is pretty amazing. :whistle:

Thanks everyone!


It is a waste of energy to be angry with a man who behaves badly, just as it is to be angry with a car that won't go. - Bertrand Russell

Share this post


Link to post
Share on other sites

here is an example using both ideas focus and default button

#include <GuiConstants.au3>
Dim $stop = 0, $go = 0, $time, $t, $move

GuiCreate("The  W A I T E R ", 365, 316,(@DesktopWidth-392)/2, (@DesktopHeight-316)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$input1 = GuiCtrlCreateInput("wait time", 130, 85, 55, 22)
GUICtrlSetState( -1, $GUI_FOCUS)
$label1 = GuiCtrlCreateLabel("Please type in a  *wait*  time for the function ( in seconds)", 20, 150, 340, 80)
$startbutton = GuiCtrlCreateButton("Start", 130, 115, 96, 23)
GUICtrlSetState( -1, $GUI_DEFBUTTON)
$stopbutton = GuiCtrlCreateButton("Stop", 249, 115, 96, 23)
$progress = GUICtrlCreateProgress( 20, 116, 96, 20)
GUISetState()

;Set on event-- does not use GUIGetMessage()
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode 

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlSetOnEvent($startbutton, "startit")
GUICtrlSetOnEvent($stopbutton, "stopit")


While 1
    
    If $go = 1 Then
        GUICtrlSetState( $input1, $GUI_DISABLE)
        $t = $t +1
        $move = $move + 10
        
        GUICtrlSetData($progress, $move)
        If $t = 11 then 
            $go = 0
            $t = 0
            $move = 0
            GUICtrlSetData($progress, 0)
            GUICtrlSetState( $input1, $GUI_ENABLE)
            GUICtrlSetData($label1, " The wait is over")
        EndIf
        
        Sleep($time)
        
    EndIf

    If $stop = 1 Then
        MsgBox(0, "Cancelled", "Wait was cancelled...", 2)
        GUICtrlSetData($progress, 0)
        GUICtrlSetState( $input1, $GUI_ENABLE)
        $stop = 0
        $go = 0
        $t = 0
        $move = 0
    EndIf
    
    If $t <> 0 Then
        GUICtrlSetData($label1, " The Program is still running,  " & $move & "%" )
    Else
        Sleep(300)
        GUICtrlSetData($label1, "Please type in a  *wait*  time for the function ( in seconds)")
    EndIf

;Sleep(5)
WEnd

Func stopit()
   $stop = 1
EndFunc

Func startit()
    $go = 1
    $inputread = GUICtrlRead($input1)
    $time = $inputread * 90
Endfunc

Func CLOSEClicked()
  MsgBox(0, "Close Program", "You clicked CLOSE!" & @CRLF & "Now Exiting...  The W A I T E R", 3)
  Exit
EndFunc

hope it helps

8)


NEWHeader1.png

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