WinActive - If question

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
homezonebenny
Posts: 20
Joined: 23 Apr 2016, 08:36

WinActive - If question

20 May 2017, 19:43

Hi,
I have a short question about a IF with WinActive.

I use a simple script like that:
SetTimer,IsActive,300
return ;Halts script on first run to prevent misfire.

; CTRL + ALT + (num) 0
IsActive:
if(WinActive(StarCraft II ahk_class StarCraft II))
{
if(SentKey0 = "") ;Verify key wasn't already fired.
Send ^!{Numpad0} ;Send hotkey if it wasn't already fired.
return
} else {
if(SentKey1 = "")
Send ^!{Numpad1}
return
}
I don't post the complete script because it would be to long.
Basically it press a hotkey when SC2 is focused, and when I leave SC2 it sends another hotkey.
I have variables for it.
SentKey0 := ""
SentKey1 := ""
And after the hotkey got pressed it will be:
SentKey0 := !""
so that the button will not be pressed every 300ms.


Now to my questions:

This is working:

Code: Select all

if(WinActive("Untitled - Notepad"))
but sadly this is not working:

Code: Select all

if(WinActive(StarCraft II ahk_class StarCraft II))
I want to ask for the title of the window, but to be 100% sure it's right the ahk_class too.
It's working with:

Code: Select all

if(WinActive("StarCraft II"))
but yeah, a bit more detailed would be better.


So theoretically I should be able to use it that way:

Code: Select all

if(WinActive(StarCraft II ahk_class StarCraft II) || WinActive(World of Warcraft ahk_class GxWindowClass))

I put an AFK check around my WinActive check:

Code: Select all

; CTRL + ALT + (num) 0
IsActive:
if(AFK = "") {
  if(WinActive(StarCraft II ahk_class StarCraft II) || WinActive(World of Warcraft ahk_class GxWindowClass))
  {
    if(SentKey0 = "") ;Verify key wasn't already fired.
      Send ^!{Numpad0} ;Send hotkey if it wasn't already fired.
    return
  } else {
    if(SentKey1 = "")
      Send ^!{Numpad1}
    return
  }
}
So when I press the hotkey CTRL + ALT + NumPadAdd (the + on numpad) it sets the variable to:

Code: Select all

  if(AFK = !"") {
    AFK := ""
  } else {
    AFK := !""
  }
So I can pause the WinActive check, and unpause it again. Works great so far.

Now I have a question to that:
- Do I need a IsActive: for all new blocks?


At the moment I only have the "CTRL + ALT + NumPad 0" hotkey pressed when the game (any game in the list, the list will be pretty long...) is Active.
But when I want a check for others things?! Like when the browser or just notepad is Active and I want the hotkey "CTRL + ALT + NumPad 2" get pressed for example.
Do I need it that way:

Code: Select all

; CTRL + ALT + (num) 0
IsActive:
if(AFK = "") {
  if(WinActive(StarCraft II ahk_class StarCraft II) || WinActive(World of Warcraft ahk_class GxWindowClass))
  {
    if(SentKey0 = "") ;Verify key wasn't already fired.
      Send ^!{Numpad0} ;Send hotkey if it wasn't already fired.
    return
  } else {
    if(SentKey1 = "")
      Send ^!{Numpad1}
    return
  }
}


; CTRL + ALT + (num) 2
IsActive:
if(AFK = "") {
  if(WinActive(Untitled - Notepad))
  {
    if(SentKey2 = "") ;Verify key wasn't already fired.
      Send ^!{Numpad2} ;Send hotkey if it wasn't already fired.
    return
  } else {
    if(SentKey1 = "")
      Send ^!{Numpad1}
    return
  }
}
So that I use 2x IsActive: ? Or can I delete the "IsActive:" from the CTRL + ALT + NumPad2 ?
I'm not 100% sure how the syntax of AHK works. But I guess the
IsActive:
means that the next IF is connected to this IsActive.
So that I need a 2nd IsActive for the notepad check.


This post in short:

1. Question:
How can I ask WinActive detailed, like the Window Title + ahk_class within the IF. I know there's a #IfWinActive thing.
But when I would use this, I would need an ELSE block seperated. Like #IfNotWinActive and then I have to list the whole games again...
So this would be reduntant. And since I like (and know) the normal if statements this works for me. Just need to know how to ask for title + ahk_class.

2. Question:
Do I need an extra IsActive: for every new "block" I create?
You saw my code examples. I have a IsActive: with IF for the games. If the game is Active it press CTRL + ALT + Num0.
When not it press: CTRL + ALT + Num1
Now when I would want to check for something else, so that CTRL + ALT + Num2 get pressed do I need a 2nd IsActive: BLOCK?
I have to put all the numblock numbers as automatically pressed hotkeys. 0 is main. 1 is default if a window is NOT in the list.
2 could be browser. 3 could be skype and so on :) That's why I need more "blocks" for it.


Thanks for reading all of this.
I know I made this post much longer as maybe needed. Since it's only about 2 simple short questions.


EDIT:
I have another small question.
Why is this not working:

Code: Select all

Send {ctrl down}{alt down}{Numpad9}{alt up}{ctrl up}
What I try is, when I press a hotkey. Let's say:
CTRL + 1
it send the code above. So basically it would to CTRL + ALT + Num9

What I try is, I set 1 hotkey, which will press 3 different hotkeys after another.
Something like that:

Code: Select all

#o::
Send {ctrl down}2{ctrl up}
sleep 10
Send {ctrl down}{alt down}{Numpad9}{alt up}{ctrl up}
sleep 10
Send {ctrl down}1{ctrl up}
return
Basically it switch from PC1 to PC2 with "CTRL + ALT + 2 (no numblock)" (network program do it) then it will press the needed hotkey "CTRL + ALT + Num9" and then it jumps back from PC2 to PC1 with "CTRL + 1 (no numblock)".
I would add a little delay (sleep) in it. So that the switch from PC1 to PC2 have time, before the "CTRL + ALT + Num9" hotkey get pressed.
But this:

Code: Select all

Send {ctrl down}{alt down}{Numpad9}{alt up}{ctrl up}
seems just not to be working. Maybe it's the
{Numpad9}
part, since I tried ctrl down and ctrl up already, and it was working in another script.

Okay, I tested a lot now. This:

Code: Select all

#o::
Send {ctrl down}{alt down}{Numpad3}{ctrl up}{alt up}
return
Seems working. When I change it from {Numpad3} to just 3 it's giving me a ³ in a textfile when I press Windows + O
Buuut the program itself does not get this pressed hotkey?!?!?!? Q__Q
I'm talking about OBS (http://www.obsproject.com).
I want to switch a scene with a hotkey being pressed from AHK.
When I press the hotkey manually on my keyboard:
CTRL + ALT + Numpad3
it's working. It switch the scene. But when AHK is doing it, it wont work.
Anyone any idea?!

I tried:

Code: Select all

#o::
controlSend,, ^!{Numpad3},ahk_class Qt5QWindowIcon
return
But even with Qt5QWindowIcon it only works when I spam the Windows + O hotkey, or HOLD both for 2-3 seconds (which is spamming aswell).


EDIT 2:
What the hell, this is working:

Code: Select all

^!6::
{
  controlSend,,^!{Numpad3}, ahk_class Qt5QWindowIcon
  return
}
When I press CTRL + ALT + 6 (even with the NumBlock6) it's working...
When I use simple things like CTRL + 2 it's NOT working O.o ........
This is just, more than strange... But in the automatic script it's only working like 70% of the time.
Sometimes it's not switching the scene. Maybe I have to put a WAIT in it or something :-\ ......... I'm totally frustrated about this scene switching not working with autohotkey.
Somehow Autohotkey use a strange hotkey send method :\

THIS btw seems working all the time:

Code: Select all

#o::
{
controlSend,,^!{Numpad3}, ahk_class Qt5QWindowIcon
controlSend,,^!{Numpad3}, ahk_class Qt5QWindowIcon
controlSend,,^!{Numpad3}, ahk_class Qt5QWindowIcon
return
}
But this feels just so wrong...
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: WinActive - If question

21 May 2017, 01:38

Hallo,
1. Question:

Code: Select all

if(WinActive(StarCraft II ahk_class StarCraft II))
is wrong!
either

Code: Select all

if(WinActive("StarCraft II ahk_class StarCraft II"))
or

Code: Select all

ifWinActive, StarCraft II ahk_class StarCraft II
also possible

Code: Select all

StarCraft_II = StarCraft II ahk_class StarCraft II
if(WinActive(StarCraft_II))
or

Code: Select all

StarCraft_II = StarCraft II ahk_class StarCraft II
ifWinActive, %StarCraft_II%
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: WinActive - If question

21 May 2017, 02:21

Hallo,
2. Question (untested):

Code: Select all

; "CTRL + ALT + (num) 0" And "CTRL + ALT + (num) 2"
IsActive:
if AFK
	Return
if !WinActive("StarCraft II ahk_class StarCraft II") or !WinActive("World of Warcraft ahk_class GxWindowClass")
	Return
if(!SentKey0 And GetKeyState("NumPad0","P")) ;Verify key wasn't already fired.
	Send ^!{Numpad0} ;Send hotkey if it wasn't already fired.
else if(!SentKey2 And GetKeyState("NumPad2","P")) ;Verify key wasn't already fired.
	Send ^!{Numpad2} ;Send hotkey if it wasn't already fired.
else if !SentKey1
	Send ^!{Numpad1}
Return
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: WinActive - If question

21 May 2017, 02:54

Hallo,
3. Question:
normally you don't need:

Code: Select all

Send {ctrl down}{alt down}{Numpad9}{alt up}{ctrl up}
try:

Code: Select all

Send ^!{Numpad9}
sleep 10 is a very little delay, only 10 ms!
first try with sleep 1000, later you reduce it.
But better would be WinWaitActive.

If Send not work, try SendInput

Code: Select all

SendInput ^!{Numpad9}
or SetKeyDelay and Send

Code: Select all

SetKeyDelay, 333,50
Send {ctrl down}{alt down}{Numpad9}{alt up}{ctrl up}
homezonebenny
Posts: 20
Joined: 23 Apr 2016, 08:36

Re: WinActive - If question

22 May 2017, 04:13

@Rohwedder
Thanks for your posts.


For the question 3.
It's not working since OBS Studio is Multiplatform.
See the post my posts here:
https://autohotkey.com/boards/viewtopic ... 0&p=149558


For question 1.
I knew that the syntax is wrong. That's why I asked for a method to ask for ahk_class aswell. Not only for the windows title.
Thanks I'll test it.


For question 2.
I'll test it.
But to be honest I wouldn't mind using a block with is active for each scene.
At the moment I only need one scene to be "auto checked" so it will stay on this one.
In the link above, I linked all my other posts to the "same topic".
I have to use 3x

Code: Select all

controlSend,, ^!{Numpad3},ahk_class Qt5QWindowIcon
controlSend,, ^!{Numpad3},ahk_class Qt5QWindowIcon
controlSend,, ^!{Numpad3},ahk_class Qt5QWindowIcon
to make it work 100% of time. 1x = 30% 2x = 95% and 3x = 100% working...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Kintaro-OEx, Marium0505, Trunks298 and 179 guests