StrokesPlus.net Forum
»
General Discussion
»
Scripts
»
How to check if a window is active and has focus
Rank: Newbie
Groups: Approved
Joined: 11/22/2022(UTC) Posts: 5 ![United Kingdom United Kingdom](/Content/images/flags/GB.png) Location: London Thanks: 3 times
|
Hi, I am looping through all windows and trying to determine if they are active (foreground and have focus) or not. sp.ForegroundWindow() is not a solution, because I need to do different things for certain number windows, depending if they are active or not. Any help would be appreciated. I have tried to find something among SystemWindow methods/fields, but was unable to. Then, I tried comparing windows to sp.ForegroundWindow() but failed here as well. Here is a sample code below. It looks silly but it illustrates the point. In my case, one side of the comparison would be the window I am checking. Code:sp.ConsoleLog( sp.ForegroundWindow().HWnd, 'xx1' );
sp.ConsoleLog( sp.ForegroundWindow().HWnd, 'xx2' );
sp.ConsoleLog( sp.ForegroundWindow() === sp.ForegroundWindow(), 'xx3' );
sp.ConsoleLog( sp.ForegroundWindow() == sp.ForegroundWindow(), 'xx4' );
sp.ConsoleLog( sp.ForegroundWindow().HWnd === sp.ForegroundWindow().HWnd, 'xx5' );
sp.ConsoleLog( sp.ForegroundWindow().HWnd == sp.ForegroundWindow().HWnd, 'xx6' );
This is what I get when I run it: Code:2022-12-14 12:38:34.39: [xx1] 198312
2022-12-14 12:38:34.40: [xx2] 198312
2022-12-14 12:38:34.41: [xx3] False
2022-12-14 12:38:34.42: [xx4] False
2022-12-14 12:38:34.42: [xx5] False
2022-12-14 12:38:34.43: [xx6] False
Assuming that the foreground window does not change during the milliseconds this code is running, I would expect at least some of the above comparisons to return true. What am I doing wrong? Is there some ECMAscript peculiarity that I am not aware of?
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,382 ![United States United States](/Content/images/flags/US.png) Location: Tampa, FL Thanks: 28 times Was thanked: 430 time(s) in 363 post(s)
|
sp.ForegroundWindow() returns a new SystemWindow object - so you're trying to compare two different object instances. Even though they have the same property values, they're two different copies and are technically not equal. You'd want to use the Equals method instead: Code:// False
StrokesPlus.Console.Log(sp.ForegroundWindow() == sp.ForegroundWindow());
// True
StrokesPlus.Console.Log(sp.ForegroundWindow().Equals(sp.ForegroundWindow()));
The SystemWindow class overrides the default Equals method on the .NET Object class, basically just comparing the HWnd to determine equality: Code:
public override bool Equals(System.Object obj)
{
if (obj == null)
{
return false;
}
SystemWindow sw = obj as SystemWindow;
return Equals(sw);
}
public bool Equals(SystemWindow sw)
{
if ((object)sw == null)
{
return false;
}
return _hwnd == sw._hwnd;
}
|
![thanks](/Themes/BlueGrey/heart_small.png) 1 user thanked Rob for this useful post.
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,382 ![United States United States](/Content/images/flags/US.png) Location: Tampa, FL Thanks: 28 times Was thanked: 430 time(s) in 363 post(s)
|
These both return true for me: Code:sp.ConsoleLog( sp.ForegroundWindow().HWnd === sp.ForegroundWindow().HWnd, 'xx5' );
sp.ConsoleLog( sp.ForegroundWindow().HWnd == sp.ForegroundWindow().HWnd, 'xx6' );
Code:2022-12-14 08:59:50.93: [xx5] True
2022-12-14 08:59:50.94: [xx6] True
|
|
|
|
Rank: Newbie
Groups: Approved
Joined: 11/22/2022(UTC) Posts: 5 ![United Kingdom United Kingdom](/Content/images/flags/GB.png) Location: London Thanks: 3 times
|
Thanks, Equals is doing the trick for me. However, HWnd comparison is still returning false to me: Code:{
let w1 = sp.ForegroundWindow();
let w2 = sp.ForegroundWindow();
sp.ConsoleLog( w1.HWnd, 'yy0' );
sp.ConsoleLog( w2.HWnd, 'yy1' );
sp.ConsoleLog( "h1:" + w1.HWnd + " h2:" + w2.HWnd + " ==:" + (w1.HWnd == w2.HWnd) + " ===:" + (w1.HWnd === w2.HWnd), 'yy2' );
}
Produces: Code:2022-12-14 15:03:55.16: [yy0] 198312
2022-12-14 15:03:55.17: [yy1] 198312
2022-12-14 15:03:55.17: [yy2] h1:[object Object] h2:[object Object] ==:false ===:false
I have tried the code in both Console->Script tab and by creating a macro in S+ settings and replaying it. I use portable version of S+ 0.5.6.9
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,382 ![United States United States](/Content/images/flags/US.png) Location: Tampa, FL Thanks: 28 times Was thanked: 430 time(s) in 363 post(s)
|
HWnd are window handles, which are pointers - thus can be a bit weird when referencing as an address. It's better/safer to convert it to an integer. Code:let w1 = sp.ForegroundWindow();
let w2 = sp.ForegroundWindow();
sp.ConsoleLog( w1.HWnd.ToInt32(), 'yy0' );
sp.ConsoleLog( w2.HWnd.ToInt32(), 'yy1' );
sp.ConsoleLog( "h1:" + w1.HWnd.ToInt32() + " h2:" + w2.HWnd.ToInt32() + " ==:" + (w1.HWnd.ToInt32() == w2.HWnd.ToInt32()) + " ===:" + (w1.HWnd.ToInt32() === w2.HWnd.ToInt32()), 'yy2' );
|
![thanks](/Themes/BlueGrey/heart_small.png) 1 user thanked Rob for this useful post.
|
|
|
StrokesPlus.net Forum
»
General Discussion
»
Scripts
»
How to check if a window is active and has focus
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The StrokesPlus.net Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close