Sign in to follow this  
Followers 0
boaty

Creating an 'Action Listener' for the Edit Control

12 posts in this topic

Hey everyone. I have a hopefully quick issue...

I'm trying to create an 'Action Listener' (Java words). Basically, I want to be focused on an Edit control, hit Enter, and have the text I've typed sent. I'm making a rudimentary IM program, and this is the one little thing I'm stuck on.

This is my current (GUI only) code:

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>

$GUI = GUICreate("Server (IP: )", 300, 200)
$EditRecv = GUICtrlCreateEdit("", 0, 0, 300, 100, $ES_READONLY, $ES_MULTILINE)
$EditSend = GUICtrlCreateEdit("", 0, 100, 300, 100, $ES_AUTOVSCROLL)
GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
    $GuiMsg = GUIGetMsg()
    
WEnd

Any help would be awesome.

Share this post


Link to post
Share on other sites



You can do it more ways than this I'm sure, but here's a few you can look at/for:

1. Use WM_NOTIFY to capture EN_CHANGE, read the control value with GUICtrlRead or ControlGetText(). If the last character(s) are vertical spaces, then take an action.

2. Use GUISetAccelerators() check if the control has focus (ie, ControlGetFocus ).

3. Use HotKeySet() with {ENTER}, check if the window is active, then check if the control has focus (ie, ControlGetFocus )


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Smoke_n gave a lot of ideas.. but missed one.. and i use this one alot..

if _isPressed ('0D') and controlgetfocus ($hWnd,'') = 'Edit2' then ;editN depending on what order your chat edits are made in

Endif

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks for the responses. Going off of Cody's, I have a problem. My compiler is returning an error with the _IsPressed function. It says in undefined, although my editor (the default Scite) brings it up as an autocomplete when I type it. Any ideas?

Ah, I think I have it. Turns out the function is in the AutoIt help, not the AutoIt3 help. I need to include Misc.au3.

Edited by boaty

Share this post


Link to post
Share on other sites

Okay...new problem. What is $hWnd? I've used them before in C++, but not in AutoIt. How do I get the $hWnd value? Right now I'm using $hWnd = GUICtrlGetHandle($EditSend).

My Current GUI Code:

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Misc.au3>

$GUI = GUICreate("Server (IP: )", 300, 200)
$EditRecv = GUICtrlCreateEdit("", 0, 0, 300, 100, $ES_READONLY, $ES_MULTILINE)
$EditSend = GUICtrlCreateEdit("", 0, 100, 300, 100, $ES_AUTOVSCROLL)
GUISetState(@SW_SHOW)

$hWnd = GUICtrlGetHandle($EditSend)

; Begin the loop to keep the GUI & program running
While GUIGetMsg() <> $GUI_EVENT_CLOSE
    
    If _IsPressed("0D") And (ControlGetFocus($hWnd, '') = 'Edit1') Then
        MsgBox(0, "Working", "If you see this, the Send worked")
    EndIf
WEnd

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

for Control () functions... hWnd, CLassNameNN, AND CtrlID's are all interchangable... BUT!!! for GUICtrl... () functions you either need to have the hWnd or GuiCtrlID... if it states it in the helpfile.. that is what is needed.. most of the _GuiCtrlEdit_... () UDFs use hWND's.. where.. most or all of the Stock GuiCtrl... () functions use the CtrlID

with the code i supplied above..

ControlGetFocus () uses the WINDOW hWnd and returns the ClassNameNN so...

your code might be like this instead

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Misc.au3>
$GUI = GUICreate("Server (IP: )", 300, 200)
$EditRecv = GUICtrlCreateEdit("", 0, 0, 300, 100, $ES_READONLY, $ES_MULTILINE)
$EditSend = GUICtrlCreateEdit("", 0, 100, 300, 100, $ES_AUTOVSCROLL)
GUISetState(@SW_SHOW)
; Begin the loop to keep the GUI & program running
While GUIGetMsg() <> $GUI_EVENT_CLOSE
    If _IsPressed("0D") And (ControlGetFocus($GUI, '') = 'Edit2') Then
        MsgBox(0, "Working", "If you see this, the Send worked")
    EndIf
WEnd

EDIT

not tested By the way

Edited by CodyBarrett

Share this post


Link to post
Share on other sites

Ahh, I see. Thank you very much. This GUI stuff for AutoIt is brand new to me, so I'm definitely working to understand how it all works with AutoIt instead of something like Java, lol...

One problem...When I hit enter...the message box comes up...however...when I hit enter to close it, it comes up again. I see that the enter command is carrying over somehow...Ah! Because the EditSend is still focused...So if I put in code to unfocus it...Should that work...Time to find out....Thanks for reading this odd monologue...

Rather...any other suggestions that I could do to resolve the problem more efficiently?

Share this post


Link to post
Share on other sites

hmm.. check

While GUIGetMsg() <> $GUI_EVENT_CLOSE
    If _IsPressed("0D") And (ControlGetFocus($GUI, '') = 'Edit2') and winactive ($GUI,'') Then
        MsgBox(0, "Working", "If you see this, the Send worked")
    EndIf
WEnd

again.. not tested

Share this post


Link to post
Share on other sites

Ahh, I looked at the code and thought it would work. Unfortunately, the problem still persists...sometimes. There are times when it comes up, and times when it doesn't reappear. The same thing happened when I tried my solution (although yours is much more elegant):

While GUIGetMsg() <> $GUI_EVENT_CLOSE
    
    If(_IsPressed("0D") And (ControlGetFocus($GUI, '') = 'Edit2')) Then
        ControlFocus('', '', $EditRecv)
        MsgBox(0, "Working", "If you see this, the Send worked")
        ControlFocus('', '', $EditSend)
    EndIf
WEnd

So neither are really working...hmm...any other ideas...?

Share this post


Link to post
Share on other sites

While GUIGetMsg() <> $GUI_EVENT_CLOSE
    
    If(_IsPressed("0D") And (ControlGetFocus($GUI, '') = 'Edit2')) and not Winexists ('Working','') Then
        MsgBox(0, "Working", "If you see this, the Send worked")
    EndIf
WEnd

try that?

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Just FYI, _IsPressed is not a safe way to do this with a GUI. There's a 250 ms delay with GUIGetMsg(), as well as you wanting to have your GUI do other things that take up time, you most certainly will miss the key your looking for a few times.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

good point.. it never bothers me.. but then again i preferably use OnEvent instead...

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