Jump to content
Macro Express Forums

Changing window focus


Recommended Posts

I can't  email support because my product purchase is older than two years. If anyone could help me, I would be appreciative.

 

I created a macro which I execute by clicking the left mouse button. The program which I'm using it in has several windows open on the screen. For the macro to execute in the desired window, I need to first click the left mouse button over the window I want to employ the macro in. This moves the focus to that window. I would like the window which I'm hovering over to automatically gain the focus so that I don't have to execute the extra mouse click. 
I thought that by prefacing my macro code with "Mouse Left Click", I could accomplish that, but it doesn't work. I've also tried most of the other left mouse commands without any avail.

Link to comment
Share on other sites

AFAIK ISS support does not end at 2 years. 

 

If you have a traditional program with multiple 'windows' that's called MDI. Multiple Document Interface. Unfortunately the window focusing commands do not work on on windows that are the child like that. If you can make them external and independent, then they should work. In some applications this is an option. 

 

You would need to use whatever keyboard shortcuts there are in the program to switch child windows. Often there are key chords, often with a tab, that can do that. But even then it's difficult because the titles are not readable. And those would have no idea where or which you mouse clicked in. 

 

If you gave us more information like what the program was, screenshots, etcetera, we might be able to help. Otherwise the best we can do is guess. 

Link to comment
Share on other sites

Thanks, Cory. I filled out the email form for support, and I got a popup that since the latest version was released more that two years ago, I couldn't get help. I'm using Macro Express Pro Version 4.9.0.1

 

You've explained it very clearly. Unfortunately, there are no keyboard shortcuts in the program to switch windows.

Each child window does have a name, but there are many windows.

 

I'm just wondering if it weren't possible to get the mouse position and, whatever position that is, send a click there, so that the focus would then go to that window?

Link to comment
Share on other sites

Quote

Unfortunately, there are no keyboard shortcuts in the program to switch windows.

 

In many applications, there are hotkeys for switching between different windows, but the hotkeys may not be well documented. Here are a few key combinations to experiment with:

 

F6

Alt + F6

Ctrl + F6

Ctrl + Tab

Ctrl + Page Up

Ctrl + Page Down

 

I've seen applications that include window switching commands in its menus. For example, in Microsoft Word 2019, the key sequence for getting close to the window switching command is:

 

Alt  (or F10)

W   (View)

W  (Switch Window)

Up and down arrow keys (to select a window)

Enter

 

Even if key sequences are impossible like this one, automating them via macros is fairly straightforward.

 

Link to comment
Share on other sites

Thanks, Acantor. I suppose that I should have explained the use case.

My program has six windows open over three screens. Each window is a chart of a different commodity which I trade.

I have two macro buttons on my mouse (in addition to the left and right buttons) which I use for trading--one button executes a "buy" macro (ctrl B), and one is a "sell macro (ctrl s). 

Before executing a trade, I need to press the left mouse button to switch the focus to the window I want to trade from. I always hover over the correct window before trading, but occasionally I forget to mouse click to change focus, and the trade is executed in the window in which I last traded.

I would like not to have to execute the extra click to change focus. Ideally, the macro would read the current mouse position, execute one left mouse button click to change focus and then execute my buy or sell macro.

In sum, I start with one click to change focus and then one click to execute my trade. Having to do anything more wouldn't help me as I'm only trying to save the one click.

Link to comment
Share on other sites

That doesn't sound too hard to do with Macro Express. Do I understand this correctly? You click a mouse button, which causes the mouse to output Ctrl + S or Ctrl + B? If so, the macro might look like this:

 

Mouse Left Click
Text Type (Simulate Keystrokes): <CONTROL>b

 

Link to comment
Share on other sites

Thanks, acantor. That part works great. My problem is that before I run this, I need the macro to output a left mouse button click in the window it's hovering over, so that the window gains focus. Without that, the macro is executed over the last active window. I currently manually click the left mouse button over the window, but I was hoping that a macro could handle that.

Link to comment
Share on other sites

That's what my admittedly untested script is supposed to do. It clicks on whatever object the mouse pointer is hovering over.

 

If the script fails because the object isn't gaining focus after it's clicked on, the first modification I would try is insert a delay between the two statements. Start with 1 second or 0.5 seconds. Through trial and error experimentation, reduce the delay until you've figured out how short it needs to be.

Link to comment
Share on other sites

Thanks, acantor.

Please forgive me for not reading your script carefully enough.

I had tried prefacing my buy command with a mouse left click command, but I hadn't thought of entering a delay.

I just tried it at various delays, up to two seconds. It still didn't move focus. If it had worked, I would have needed it at .5 seconds or less, so I didn't try anything longer than two seconds.

I just don't understand why a macro action can't emulate the manual left click that I perform with my finger.

 

Link to comment
Share on other sites

Then try this, which may or may not need a delay between steps:

 

Capture Control from Beneath the Mouse into %ControlUnderMouse%
Set Focus to %ControlUnderMouse%

<CAPTURE CONTROL Option="\x00" Control="%ControlUnderMouse%" UseText="FALSE"/>
<SET FOCUS Control="%ControlUnderMouse%"/>

 

Link to comment
Share on other sites

This is the way that the code looks (I used the direct editor):

<TEXT TYPE Action="0" Text="Capture Control from Beneath the Mouse into %ControlUnderMouse%\r\nSet Focus to %ControlUnderMouse%\r\n"/>
<CAPTURE CONTROL Option="\x00" Control="%ControlUnderMouse%" UseText="FALSE"/>
<SET FOCUS Control="%ControlUnderMouse%"/>
<TEXT TYPE Action="0" Text="<CTRLD>2<CTRLU>"/>

 

When I went to save it, it asked me about creating a variable, which I agreed to.

 

The buy action still works, but the macro sill doesn't move the focus to the window under the mouse.

Link to comment
Share on other sites

At this point, my only suggestion is put your energies into forcing the macro to reliably give the window focus.

 

Start afresh. Create a new macro, and keep messing with it until you find the steps to give focus to the window under the mouse pointer. Experiment with code excerpts such as these:

 

Mouse Left Click


Mouse Left Click
Delay: 100 milliseconds
Mouse Left Click


Mouse Left Button Down
  Delay: 100 milliseconds
Mouse Left Button Up


Capture Control from Beneath the Mouse into %ControlUnderMouse%
Delay: 100 milliseconds
Mouse Click on Control %ControlUnderMouse%



<MOUSE LEFT CLICK/>


<MOUSE LEFT CLICK/>
<DELAY Flags="\x02" Time="100"/>
<MOUSE LEFT CLICK/>


<MOUSE LEFT BUTTON DOWN/>
<DELAY Flags="\x02" Time="100"/>
<MOUSE LEFT BUTTON UP/>


<CAPTURE CONTROL Option="\x00" Control="%ControlUnderMouse%" UseText="FALSE"/>
<DELAY Flags="\x02" Time="100"/>
<MOUSE CLICK ON CONTROL Clicks="\x01" Control="%ControlUnderMouse%" Button="\x00" Center="TRUE"/>

 

Link to comment
Share on other sites

I just wanted to thank you, acantor, for taking the time to send me the code snippets. 

 

I tried them all. Unfortunately, none of them moved the focus, and I'm not knowledgeable enough to conjure additional ones.

 

It's passing strange to me that a simple macro can't be created to duplicate a manual click on a screen coordinate, something the least knowledgeable computer user does many times a day., but it is what it is.

 

Thanks again for your efforts.

 

Best,

Stephen

Link to comment
Share on other sites

“It's passing strange to me that a simple macro can't be created to duplicate a manual click on a screen coordinate”
 

 

I’m lost! Of course a macro can do that. For example, the mouse location can be captured as coordinates (MouseX,MouseY) and an IF Variable used to determine whether that falls within the required area.

 

But as you have to first activate the macro, why not just do so with a Mouse Event specifying a position chosen visually? That could simultaneously give that area the focus, yes?

 

I clearly must be missing something here. It would be helpful to see a non-confidential screenshot or two, plus your full macro.

 

And, as this is your first post could you indicate your level of experience with MX Pro macros. For instance, are you familiar with the full command set? Including Window Activate etc? And when specifying a window in the Script Editot have you tried showing Hidden windows.

 

Link to comment
Share on other sites

Thanks, terrypin.

 

I can write very simple macros, and I can't write any that involve real programming, e.g., if, then, else statements.

I was able to copy each of the macros that acantor suggested and each ran without error. The macros all placed my Buy/Sell orders, but without moving the known coordinates of the mouse to the window that I was hovering over--the desired initial action.

 

I have three monitors. Each is divided into two windows, and each of those windows displays a different commodity chart. These windows are each children of the parent program, NinjaTrader. I place my orders by clicking on one of two macro buttons on the side of the mouse, one I've programmed for "buy" and one for "sell."

To make certain that I'm in the active window, I need to left click the mouse button before clicking one of the two macro buttons; if I don't, I will execute a buy or a sell trade in whichever window I previously executed one in. This has happened to me and caused me to make unwanted trades.

 

When trading, one needs to act quickly, so I would like not to have to take the additional step of first clicking on the left mouse button while hovering over the desired window. I would like that left mouse click I make while hovering to be part of the macro, but I don't know how do it.

 

Any help you could give me would be appreciated.

Link to comment
Share on other sites

 

There are several aspects of your problem definition that I’m not clear about. I suppose I still don’t understand exactly what you are doing and what you want to happen.

 

“I created a macro which I execute by clicking the left mouse button.”
Yet as I understand it you also want your macro to left click when the mouse cursor is hovering over a certain window directly before you make a trade with your ‘macro’ buttons. Seems like potential conflict, as that would restart your macro. Did you consider the command Window Activate I asked about?

 

As usual the devil is in the details. What exactly do you want to happen, not only when you move your mouse into a specific window but also afterwards? Specifically, am I right that as soon as the mouse cursor moves into a ‘target window’ you want it to get the focus abd keep it until the cursor enters another window? If so, what are the time constraints, if any? For example, if you were trading briskly and moved from window #1 to window #2 in less than say 0.5s, I assume you would want both the MX macro and your macro buttons to respond correctly?

 

I’m not familiar with your application or with online trading. Nor do I have any ‘macro buttons’ on my mouse. And I use only a single large monitor, albeit usually with many windows open. These are obstacles to helping you, so we need a shared basis for discussion. 
 

If you confirm that I’m on the right lines so far then I suggest we continue the discussion as follows:  using two windows, each occupying a fixed section of the screen, say Notepad and Paint, both universally accessible on MS Windows based PCs.. I would then draft a simple demo macro which you could hopefully adapt to your Ninja Trader scenario.

 

Terry


 

Link to comment
Share on other sites

The core of Terry's many excellent suggestions, I would say, is to simplify what you are trying to do.

 

To that end, temporarily take the mouse buttons out of the equation. Instead, make sure your Macro Express script works if you activate it via hotkey.

 

Add a line of code to the start of your script to make it easier to tell that your script is getting activated. (This is for testing purposes only. You will be able to get rid of it soon.)

 


Text Box Display: Yes, the macro has been activated!


<TEXT BOX DISPLAY Title="Yes, the macro has been activated!" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs14 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

 

 

Once you've saved the macro, move the mouse pointer to the desired location. But instead of pressing a mouse button, press the hotkey. Is the macro activating?

 

It would help if you were to post your script in the forum.

Link to comment
Share on other sites

I'll shortly be leaving my PC for most of the day. So I made the assumption that your answer to my key question would be Yes. I've therefore gone ahead and written the heavily commented demo macro below. Together with Alan's suggestions I hope it will help you get the problem sorted. Come back if not.

 

// The MX Pro macro is now running, having been triggered in an appropriate way.
 
// The next command starts a loop which will repeat until othewise ended. For this basic demo that will be by using the hotkey assigned in Options > Preferences > Playback > Miscellaneous > 'Hotkey to abort macros'.
     
// One way to repeat indefinitely, until otherwise stopped.
Repeat Until %N[99]% Does not Equal "%N[99]%"
  Get Mouse Position into (MouseX, MouseY) Relative to Screen
  // Test if mouse is inside Notepad
  If Variable %MouseX% Is Less Than "777"
    AND
  If Variable %MouseY% Is Less Than "566"
  // So the next command should switch focus to that window if it does not already have it.
    Window Activate:  - Notepad
    // In addition, for this demo, let's briefly show a message.
    Text Box Display: Message
  End If
   
  // Test if mouse is inside Paint
  If Variable %MouseX% Is Greater Than "257"
    AND
  If Variable %MouseX% Is Less Than "970"
    AND
  If Variable %MouseY% Is Greater Than "578"
    AND
  If Variable %MouseY% Is Less Than "1160"
  // So the next command should switch focus to that window if it does not already have it.
    Window Activate: - Paint
    // In addition, for this demo, let's briefly show a message.
    Text Box Display: Message
  End If
End Repeat
// The window will be repeatedly toggled until the mouse leaves the area. But that makes no practical difference, and the temporary messages can be deleted after testing.
 
// Only experiment will determine whether the macro is fast enough to respond to brisk cursor movement

 

<COMMENT Value="The MX Pro macro is now running, having been triggered in an appropriate way."/>
<COMMENT/>
<COMMENT Value="The next command starts a loop which will repeat until othewise ended. For this basic demo that will be by using the hotkey assigned in Options > Preferences > Playback > Miscellaneous > 'Hotkey to abort macros'.\r\n"/>
<COMMENT Value="One way to repeat indefinitely, until otherwise stopped."/>
<REPEAT UNTIL Variable="%N[99]%" Condition="\x01" Value="%N[99]%"/>
<GET MOUSE POSITION Option="\x00" X="MouseX" Y="MouseY"/>
<COMMENT Value="Test if mouse is inside Notepad"/>
<IF VARIABLE Variable="%MouseX%" Condition="\x02" Value="777" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%MouseY%" Condition="\x02" Value="566" IgnoreCase="FALSE"/>
<COMMENT Value="So the next command should switch focus to that window if it does not already have it."/>
<WINDOW ACTIVATE Title=" - Notepad" Exact_Match="FALSE" Wildcards="FALSE" _IGNORE="0x0006"/>
<COMMENT Value="In addition, for this demo, let's briefly show a message."/>
<TEXT BOX DISPLAY Title="Message" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n{\\colortbl ;\\red255\\green0\\blue0;}\r\n\\viewkind4\\uc1\\pard\\qc\\b\\f0\\fs20 The mouse cursor has entered the target window called Notepad.\r\n\\par \r\n\\par \\cf1 Use the macro button to trade!\\b0\\f1 \r\n\\par }\r\n" Left="824" Top="620" Width="352" Height="122" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x01" Delay="1"/>
<END IF/>
<COMMENT/>
<COMMENT Value="Test if mouse is inside Paint"/>
<IF VARIABLE Variable="%MouseX%" Condition="\x03" Value="257" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%MouseX%" Condition="\x02" Value="970" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%MouseY%" Condition="\x03" Value="578" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%MouseY%" Condition="\x02" Value="1160" IgnoreCase="FALSE"/>
<COMMENT Value="So the next command should switch focus to that window if it does not already have it."/>
<WINDOW ACTIVATE Title="- Paint" Exact_Match="FALSE" Wildcards="FALSE" _IGNORE="0x0006"/>
<COMMENT Value="In addition, for this demo, let's briefly show a message."/>
<TEXT BOX DISPLAY Title="Message" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n{\\colortbl ;\\red255\\green0\\blue0;}\r\n\\viewkind4\\uc1\\pard\\qc\\b\\f0\\fs20 The mouse cursor has entered the target window called Paint.\r\n\\par \r\n\\par \\cf1 Use the macro button to trade!\\b0\\f1 \r\n\\par }\r\n" Left="824" Top="620" Width="352" Height="122" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x01" Delay="1"/>
<END IF/>
<END REPEAT/>
<COMMENT Value="The window will be repeatedly toggled until the mouse leaves the area. But that makes no practical difference, and the temporary messages can be deleted after testing."/>
<COMMENT/>
<COMMENT Value="Only experiment will determine whether the macro is fast enough to respond to brisk cursor movement"/>

 

Terry

 

MousePositionDemoScenario.jpg

Link to comment
Share on other sites

Terrypin, I feel badly that you've gone so out of your way to give me so much help and that I'm not going to carry it forward.

I'm not a programmer, and this is all way over my head. I don't even understand how to set up the Paint windows and execute the code you've written, much less how to transpose the code for NinjaTrader. 

My hope was to get one or two lines of code that would give me a single mouse click at the coordinates on the screen that I was hovering over and then execute in place my "Mouse Left Click Text Type (Simulate Keystrokes): <CONTROL>b"  code.

That clearly won't be the case.

 

Thanks again,

Stephen

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...