Sign in to follow this  
Followers 0
qwert

Need a hybrid "Wait For" function

7 posts in this topic

I have a script that displays intermediate results to the user and awaits their response to either Continue or to Go Back one step in the procedure that's in progress. My script calls a custom Wait function that loops until the user responds. So far, I've only been able to set up my Wait function for either the mouse clicks (using Do Until GUIGetMsg) or the Left and Right Arrows (using HotKeySet). What I haven't figured out is how to have the same Wait function cover both types of user actions. IOW, how can the function be constructed to pause the script until one of four possible user actions are entered:

Right Mouse Click = Go Back

Left Mouse Click = Continue

Right Arrow Key = Continue

Left Arrow Key = Go Back

All other keys and actions should be ignored except for ESC, which terminates the script. (The Wait function returns a "Back" or "Continue" indicator, of course.)

I've looked at dozens of examples on these forums, but all the ones I've found use either the keystroke method or the mouse method, but not both.

Any suggestions will be appreciated.

Share this post


Link to post
Share on other sites



It's easier to handle the GUI events the same as the hot keys:

#include <GuiConstantsEx.au3>

Opt("GuiOnEventMode", 1)

HotKeySet("{RIGHT}", "_Continue")
HotKeySet("{LEFT}", "_GoBack")
HotKeySet("{ESC}", "_Quit")

Global $iMsg = 0
Global $hGUI = GUICreate("Test")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_Continue")
GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "_GoBack")
GUISetState()

While 1
    Sleep(10)
WEnd

Func _Continue()
    $iMsg += 1
    ToolTip($iMsg & ": Continuing...")
EndFunc   ;==>_Continue

Func _GoBack()
    $iMsg += 1
    ToolTip($iMsg & ": Going back...")
EndFunc   ;==>_GoBack

Func _Quit()
    Exit
EndFunc   ;==>_Quit

:graduated:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#Include <Misc.au3>


while 1
If _ispressed("02") or _ispressed("25") Then
    msgbox (0, '' , "Go Back", 5)
    Endif

If _ispressed("01") or _ispressed("27") Then
    msgbox (0, '' , "Continue", 5)
    Endif

    wend


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

The problem is that _IsPressed() and HotKeySet() will both react when you are not on the target GUI. This is even better, as all the action is restricted to the appropriate GUI:

#include <GuiConstantsEx.au3>

Opt("GuiOnEventMode", 1)

Global $iMsg = 0, $aAccel[2][2] = [["{LEFT}", 0],["{RIGHT}", 0]]
Global $hGUI = GUICreate("Test", 300, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_Continue")
GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "_GoBack")
$aAccel[1][1] = GUICtrlCreateButton("CONTINUE", 40, 250, 90, 30)
GUICtrlSetOnEvent(-1, "_Continue")
$aAccel[0][1] = GUICtrlCreateButton("GO BACK", 170, 250, 90, 30)
GUICtrlSetOnEvent(-1, "_GoBack")
GUISetAccelerators($aAccel, $hGUI)
GUISetState()

While 1
    Sleep(10)
WEnd

Func _Continue()
    $iMsg += 1
    ToolTip($iMsg & ": Continuing...")
EndFunc   ;==>_Continue

Func _GoBack()
    $iMsg += 1
    ToolTip($iMsg & ": Going back...")
EndFunc   ;==>_GoBack

Func _Quit()
    Exit
EndFunc   ;==>_Quit

:graduated:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks very much. Either method will work for my specific application because it runs as the topmost window. But the accelerator table method looks very appropriate for other apps. I haven't had a reason to investigate it before. In fact, I'm not sure I fully understand the concept. I found this short explanation in a book on cross-platform widgets:

An accelerator implements a keyboard shortcut for a menu command, enabling the user to execute the command quickly. These shortcuts take precedence over other keyboard processing.

Does that do it justice? Other than restricting to the specific GUI, are there other advantages? I couldn't find much in the AutoIt Help, so I'm thinking it's not used often.

Share this post


Link to post
Share on other sites

The accelerator is a GUI-specific hot key. When you hit that hot key it is seen by the GUI as though you clicked an associated control. In the demo I posted the {LEFT} and {RIGHT} hot keys were associated with the GoBack and Continue button control IDs. Hitting the hot keys is seen by the GUI as the same thing as clicking the button.

The only thing remotely tricky about it is the need to use a 2D array to set up the accelerators.

:graduated:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

That's good to know. Just for reference, here's the resulting function ... using the simpler _IsPressed method:

#Include <Misc.au3>

Func Wait()
  While 1

  If _ispressed("02") or _ispressed("25") Then
    Return "Go Back"
  Endif

  If _ispressed("01") or _ispressed("27") Then
    Return "Continue"
  Endif

  Wend
EndFunc

Thanks for your help.

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