Jump to content

Problem with a simple script, one change kills the script.


Go to solution Solved by TheLandYacht,

Recommended Posts

Noob question

So I've got a simple script I've created to (quickly) send a keypress to a background application, then switch back to the application I was already in...and seems to work just fine with some keypresses, but the script fails to function at all with others.

Specifically, in line 5, the script works fine with (F2) or +(F5), but not with (F1) or (F5).

A bit about these keypresses in the application, F2 is "options" & (shift)F5 is "Sync All".  F1 is "Connect", F5 is "Sync current window".  All four keys work when pressed directly (physical keyboard).

Help?

Sleep(3000)
   ;~This gets the window title of the currently active window
    $title=WinGetTitle("[active]")
   ;~This sends a keypress or whatever you want to MPE, even if it's minimized or not in-focus.
   ControlSend( "MyPhoneExplorer", "", "", "+{F5}")
   ;~I found you needed a short delay between telling MPE to do something & switching back to your window, half a second was enough for me.
    Sleep(500)
   ;~This brings back your window
    WinActivate($title)
   ;~This stuff's specific to my application, you can ignore it.
    If WinActive("Zoom Player") Then
      ControlSend( "Zoom Player", "", "", "!t")
    EndIf

All of this because I can't figure out how to send a keypress to a background application (background, not minimized) without bringing it to the front (focus)...which was my original intent.

Link to comment
Share on other sites

You must specify the controlID parameter in the ControlSend function.

 

Hmm...thought it'd be as easy as using AI Window Info to get that (summary window has a spot for ID: under >>>Control<<<

but that field remains empty.

Class, ClassNameNN, etc come up with values..."ID:" is empty tho.

Also, the first two (F2 & shiftF5) work fine in the script...while the second two (F1 & F5) don't.  If controlID were required, wouldn't it crash & burn no matter what was keypress I used?

Is there another way to send a keypress to a window in the background other than ControlSend?

Link to comment
Share on other sites

FireFox : OK, thanks. I thought it was a setting that was necessarily set. But on which control dos ControlSend will operate in this case ?

 

In this case, I'm not trying to send it to a control, but as a method of sending a keypress to the window itself...a window that is NOT currently "up front" so-to-speak.

I could have done it with 2 steps, using WinActivate followed by Send (keypress)...but that seemed inelegant...especially when the initial goal was to send a keypress WITHOUT bringing that application "up front".

 

BTW...My initial (secondary) question still stands.  Is there a way to send a keypress to an application WITHOUT bringing it "up front"?

Link to comment
Share on other sites

So...it looks like the whole problem is...the application (MyPhoneExplorer) likes certain keys (sent from AutoIt) just fine, but others it won't accept unless they're physically pressed.  I thought keyboard emulation was supposed to be "as good as the real thing"...or what's the point?

Any ideas?

Link to comment
Share on other sites

  • Moderators

This works just fine for me on WIN7 x64, MyPhoneExplorer 1.8.5:

Local $hWnd = WinWait("MyPhoneExplorer","")
Sleep(1000)

ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F1}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F2}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F5}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "+{F5}")

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Solution

 

This works just fine for me on WIN7 x64, MyPhoneExplorer 1.8.5:

Local $hWnd = WinWait("MyPhoneExplorer","")
Sleep(1000)

ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F1}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F2}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F5}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "+{F5}")

 

What, exactly, does $hWnd, do?

Link to comment
Share on other sites

  • Moderators

$hWnd is a variable, which is set to the Window's handle (as FireFox tried to point out to you in post #9).

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

So...tried your function as a stand-alone script & it works as advertised. 

BUT...plugging your function (used the {F5} line) into my script in place of line five broke it.  

Removing the first couple of lines of your function and the mention of $hWnd seemed to work.  So it looks like the first response I got in the thread was (sort of) right.  It did need to be pointed at a specific ControlID?

And yes, the infinite loop is deliberate...I want this to run as long as the computer's running.

Ended up with...

While 1=1
    Sleep(2000)
    ;~This gets the window title of the currently active window
    $title=WinGetTitle("[active]")
    ;~This sends a keypress or whatever you want to MPE, even if it's minimized or not in-focus.
    ControlSend("MyPhoneExplorer", "", "[CLASS:ToolbarWindow32]", "{F5}")
    ;~I found you needed a short delay between telling MPE to do something & switching back to your window, half a second was enough for me.
    Sleep(500)
    ;~This brings back your window
    WinActivate($title)
    ;~This stuff's specific to my application, you can ignore it.
;~     If WinActive("Zoom Player") Then
;~      ControlSend( "Zoom Player", "", "", "!t")
;~     EndIf
    If WinActive("Interface") Then
        WinActivate("Zoom Player")
    EndIf
    ;~Sleep for 10 minutes
    Sleep(600000)
WEnd

 

This works just fine for me on WIN7 x64, MyPhoneExplorer 1.8.5:

Local $hWnd = WinWait("MyPhoneExplorer","")
Sleep(1000)

ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F1}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F2}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F5}")
;Or
ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "+{F5}")
Link to comment
Share on other sites

$hWnd is a variable, which is set to the Window's handle (as FireFox tried to point out to you in post #9).

 

Looks like both I and FireFox were answering a question by jguinch (my response is is #6, I just didn't have the language to put it into technical terms).

As to variables, I guess I'll have to do a bit of research on how to use them effectively in the future.

THANK YOU for your help with this!

Link to comment
Share on other sites

  • Moderators

Once again "broke it" does not qualify as an explanation. What broke: did you get an error, did it simply not send the commands you expected, did it send the wrong command? Help us help you by providing an adequate amount of information ;)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

Yes, as much as you (and we have all been there) want to start coding now, if you do not understand what a variable is, and how to employ them, you need to take a step back and read through the help file. Reading the first few sections of the help file, as well as looking at the Examples in each section and the Tutorials on the Wiki, is the best thing you can do for yourself at this point.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Once again "broke it" does not qualify as an explanation. What broke: did you get an error, did it simply not send the commands you expected, did it send the wrong command? Help us help you by providing an adequate amount of information ;)

 

Oops, to clarify...it seems not to proceed through that step.  I couldn't tell you more without some idea of how I would go about debugging it to figure out where/how it's broken.

What I do know is...while the one I pasted above works...this one (below) doesn't...for whatever reason.  How would I go about figuring out where/how it's broken?

While 1=1
    Sleep(2000)
    ;~This gets the window title of the currently active window
    $title=WinGetTitle("[active]")
    ;~This sends a keypress or whatever you want to MPE, even if it's minimized or not in-focus.
Local $hWnd = WinWait("MyPhoneExplorer","")
Sleep(1000)

ControlSend($hWnd, "", "[CLASS:ToolbarWindow32]", "{F5}")
    ;~I found you needed a short delay between telling MPE to do something & switching back to your window, half a second was enough for me.
    Sleep(500)
    ;~This brings back your window
    WinActivate($title)
    ;~This stuff's specific to my application, you can ignore it.
;~     If WinActive("Zoom Player") Then
;~      ControlSend( "Zoom Player", "", "", "!t")
;~     EndIf
    If WinActive("Interface") Then
        WinActivate("Zoom Player")
    EndIf
    ;~Sleep for 10 minutes
    Sleep(600000)
WEnd
Link to comment
Share on other sites

Yes, as much as you (and we have all been there) want to start coding now, if you do not understand what a variable is, and how to employ them, you need to take a step back and read through the help file. Reading the first few sections of the help file, as well as looking at the Examples in each section and the Tutorials on the Wiki, is the best thing you can do for yourself at this point.

 

Believe me, I feel where you're coming from.  I didn't do this because I wanted to code.  I had something I needed to automate...and turned to this as the easiest method.

I knew in advance what I came up with would probably be dirty & inelegant.  But all I needed was something that would work consistently on my one (actually 2 identical) machine(s).

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