Jump to content

Which message triggered the event?


Recommended Posts

Hi All, I'm pretty new to AutoIT, and GUI programming in general, and have what I hope is an easy question. I'll try to be brief, but if more info is needed please let me know.

I have a text input control with OnEvent set to run one function, (when the user tabs out of the box), and a button with OnEvent set to run another function.

If the button is pressed while the input control still has focus, the input control loses focus, which preempts the button click event, and the input control's OnEvent function is executed instead of the button's.

Is there some way to trap the message that caused the lost focus event and evaluate it for whether the user tabbed out of the control or the button was pressed? If so, how?

Thanks for any assistance!

k3v

Link to comment
Share on other sites

Here's a little example that demonstrates the behavior I described above.

It's interesting that the lost focus event only occurs if some text has been typed into the input control before leaving the control.

#include <Process.au3>
#include <GuiConstants.au3>
Opt('GUIOnEventMode', 1)

GUICreate("Event Test",175,130,400,300)
$input1 = GUICtrlCreateInput("",15,15,145,25)
$input2 = GUICtrlCreateInput("",15,45,145,25)
$button = GUICtrlCreateButton("Run",15,75,70,30)
$exit = GUICtrlCreateButton("Exit",90,75,70,30)
GUICtrlSetOnEvent($input1,"input")
GUICtrlSetOnEvent($button,"button")
GUICtrlSetOnEvent($exit,"close")
GUISetOnEvent($GUI_EVENT_CLOSE,"close")
ControlFocus("Event Test","",$input1)
GUISetState(@SW_SHOW)

while 1
    sleep(1000)
WEnd

Func close()
    Exit
EndFunc

Func input()
    MsgBox(0,"","Input lost focus")
EndFunc

Func button()
    MsgBox(0,"","Button clicked")
EndFunc

Thanks again!

k3v

Link to comment
Share on other sites

... lost focus event only occurs if some text has been typed into the input control before leaving the control.

Your function "input" will work because it is triggered by a changing in inputbox content and not by a change of focus.

This type of function (GUICtrlSetOnEvent($input1,"input")) will work only when the content of the inputbox was changed - and that's why you don't get any reaction when the inputbox is empty.

It will work exactly the same if you remove ControlFocus("Event Test","",$input1)

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Thank you for your reply, eneiman.

If I understand what you're saying, then simply typing in the input control would execute the input() function, once for each keystroke, since changing the contents of the control would constitute an event. (have I misunderstood you?)

But in my testing, I can type all day long without executing the input() function; the function is only executed when the control loses focus. This is the desired effect, if focus is lost because the user tabbed out of the control, but not if the "Run" button is pressed.

THE REAL QUESTION is less about that, and more of a "cart before the horse" issue: My application's interface allows two ways to "leave" the input control - 1) by tabbing out of it, which should execute the input() function, or 2) by clicking the "Run" button, which I would like to execute the button() function...

The problem I'm running into is that by clicking the "Run" button, the input control's lostfocus event occurs before the button's click event, and runs the input() function instead of the button() function.

So even if you ignore the GUICtrlSetOnEvent($input1,"input") line, I guess my question still stands... maybe there is an alternate strategy for accomplishing the task, since I can't see any way around triggering the lostfocus event.

Any ideas there?

(sorry for the verbosity...)

Thanks again,

k3v

Edited by k3v
Link to comment
Share on other sites

Actually I've made a little mistake - the function will be triggered if the content was modified AND either Enter was pressed OR the focus was lost.

Maybe there is a solution for what you want to achieve but I can't find any at this moment. I hope some "guru" will be able to help you with this.

Good luck,

PS: if nobody will be able to give you a solution during the next days (if a proper and simple solution is not available) I might try to think "deeper" about this and - maybe - to find something ...

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Thank you for your thoughts. I'm experimenting with $GUI_EVENT_xxx now, but not having much luck... might be worth a new post.

Oh well... have a great day!

k3v

Link to comment
Share on other sites

After continuing this thread in the GUI postings, a suitable resolution was recommended which worked like a champ. For anyone interested, checking for _IsPressed(09) let me branch out of the input() function if the tab button was not used:

Func input()
     if _IsPressed(09) Then
          ...do stuff...
     endif
EndFunc

09 is the value for the Tab key... (keyboard values are all listed in the _IsPressed help documentation).

(GUI section thread is here --> http://www.autoitscript.com/forum/index.php?showtopic=72957)

Many thanks to MHz for this!

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...