Auto use of a hotkey?!

Ask gaming related questions (AHK v1.1 and older)
homezonebenny
Posts: 20
Joined: 23 Apr 2016, 08:36

Auto use of a hotkey?!

10 May 2017, 04:39

Hi,
is there any chance that I can do the following (automated):

1. When I enter game X autohotkey press a defined hotkey
2. When I leave the game X autohotkey press a defined hotkey
3. When I click on browser (focus) or on Desktop, autohotkey press a defined hotkey

Why I'm asking?
Well. I have defined hotkeys for OBS (Open Broadcaster Software) to switch the stream scenes manually.

What I try to do is I want a tool which press that hotkey automatically to switch that scene in OBS.
For example when I enter (go into) the game StarCraft 2, it press:
CTRL + ALT + 0 (numblock)
When I enter: Desktop / Browser or anything else (not StarCraft 2) it press:
CTRL + ALT + 1 (numblock)



But that's only the first step.
When I have a tool which press this hotkey for me automatically I can create a autohotkey script which do more things with that hotkey.

That means, when CTRL + ALT + 0
got pressed, my autohotkey will use something like:
send CTRL ALT 2 (normal 2)
send CTRL ALT 0 (numblock)
send CTRL ALT 1 (normal 1)

Why I want autohotkey do that?
I use a program which switch from PC1 to PC2 with a hotkey:
CTRL ALT 1 (normal) = PC1
CTRL ALT 2 (normal) = PC2
So I can jump from PC to PC. I can control 2 pcs with only one mouse / keyboard connected to my PC1 USB.
I don't want to go MANUALLY from PC1 to PC2, press the needed hotkey... and switch back to PC1.


For short:
1. I need a tool (maybe Autohotkey can do it too?!) which press a hotkey when I have a program on "focus" like a game, browser or whatever
2. I'll configurate Autohotkey to receive the pressed hotkey and do things with it (like described above)



EDIT:
I know there is a "Scene switcher" for OBS.
But it's not what I searching for. Since I need to control it "remotely" on another PC.
OBS is not running on the same computer like the game. That's why I have to switch, press hotkey, switch back.
And when I do that manually it takes a while :-\
User avatar
theartofx
Posts: 15
Joined: 07 May 2017, 15:50

Re: Auto use of a hotkey?!

10 May 2017, 11:37

This is definitely possible in AHK. You'd use a timer (SetTimer and a label) to check the if the window is active (IfWinActive command / WinActive function) every so often, say 300ms - 500ms, and if so to send a hotkey (Send ^!Numpad0 or Send ^!Numpad1) based on the status. You'd need to set a couple of check variables to see if the hotkey was already sent for the windows current status. At the bottom of the script, you'd specify what they hotkeys do. I'm at work, so can't provide an example right at this time, but will check back after and provide one later if no one else has already if you'd like.
User avatar
theartofx
Posts: 15
Joined: 07 May 2017, 15:50

Re: Auto use of a hotkey?!

10 May 2017, 12:31

Ok, so I made this as an example. It checks to see if an untitled notepad document window is active ever 300ms, and sends a hotkey (Ctrl+Alt+Numpad0) only once if it is. If it's not active, it sends a different hotkey (Ctrl+Alt+Numpad1) once. Hotkeys are defined below the script. Currently the hotkeys do nothing, but I tested it and it seems to work in the way you described, I think. Ctrl+Alt+Numpad3 was set to close the script during testing, this can be changed or removed as you please. Hope this helps.

Code: Select all

#NoEnv
#Persistent
#SingleInstance Force

;Force check variables to blank on first run.
SentKey1 := ""
Sentkey2 := ""

;SetTimer to Check WinStatus
SetTimer,IsActive,300
return ;Halts script on first run to prevent misfire.

;IsActive label is a subroutine to check if Window is active, and if so, to do stuff.
IsActive:
If (WinActive("Untitled - Notepad"))  ;If Window is active.
{
  	If (SentKey0 = "")   ;Verify key wasn't already fired.
	   Send ^!{Numpad0}  ;Send hotkey if it wasn't already fired.
 return
}
else  ;If Window isn't active.
{
	If (SentKey1 = "")  ;Verify key wasn't already fired.
	  Send ^!{Numpad1} ;Send hotkey if it wasn't already fired.
	return
}

;Hotkey Section (Intentionally placed below script)
^!NumPad0::  
{
  SentKey0 	:= !""
    ;Stuff to do, such as sending other hotkeys below, should  go here.
  SentKey1 := ""
  return
}
^!NumPad1::
{
	SentKey1 := !""
	  ;Stuff to do, such as sending other hotkeys below, should  go here.
	SentKey0 := ""
	return
}
^!NumPad3::ExitApp
;^!1:: Msgbox Hello ; This should do something.
;^!2:: MsgBox World ; This should do something.
homezonebenny
Posts: 20
Joined: 23 Apr 2016, 08:36

Re: Auto use of a hotkey?!

14 May 2017, 07:34

Wow, first I really have to thank you for the help.
I did not look into this post for a while now.
I really, really have to thank you.

Now the thing is. Will this AHK script take less power than a little program which checks for active windows -> press hotkey?!
I'm pretty sure there are some small tools out there. Or maybe I'll write my own in c++.
Since I'm a fan of "low resources" tools, I love autohotkey.
But I never tested how much such a timer will use running all the time in the background.

I'll test it and give you a feedback.


EDIT:
I'm pretty new to AHK.
But I'm confused by some things.

When I look into the script. I would need to make like 10x IsActive blocks.
For "StarCraft 2" for "World of Warcraft" for "Heathsthone" and for all other games I play.
This would make the script pretty long though.

I correct the script a little bit. Since you had a low K as "SentKey" and you used SentKey2 instead of 0 and 1.
This script should be still correct right?

Code: Select all

#NoEnv
#Persistent
#SingleInstance For 



;------------------------------------------------------------------
; Create default variables (empty)
;------------------------------------------------------------------

SentKey0 := ""
SentKey1 := ""
SentKey2 := ""
SentKey3 := ""
SentKey4 := ""
SentKey5 := ""
SentKey6 := ""
SentKey7 := ""
SentKey8 := ""
SentKey9 := ""




;------------------------------------------------------------------
; SetTimer to Check WinStatus
;------------------------------------------------------------------

SetTimer,IsActive,300
return ;Halts script on first run to prevent misfire.




;------------------------------------------------------------------
; Check Windows Actives
;------------------------------------------------------------------

; CTRL + ALT + (num) 0
IsActive:
if(WinActive("Untitled - Notepad"))
{
  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
; another CODE BLOCK here


; CTRL + ALT + (num) 3
; another CODE BLOCK here




;------------------------------------------------------------------
; Press Hotkeys
;------------------------------------------------------------------

^!NumPad0::  
{
  SentKey0 := !""
  ;Stuff to do, such as sending other hotkeys below, should  go here.
  SentKey1 := ""
  SentKey2 := ""
  SentKey3 := ""
  SentKey4 := ""
  SentKey5 := ""
  SentKey6 := ""
  SentKey7 := ""
  SentKey8 := ""
  SentKey9 := ""
  return
}

^!NumPad1::
{
  SentKey1 := !""
  ;Stuff to do, such as sending other hotkeys below, should  go here.
  SentKey0 := ""
  SentKey2 := ""
  SentKey3 := ""
  SentKey4 := ""
  SentKey5 := ""
  SentKey6 := ""
  SentKey7 := ""
  SentKey8 := ""
  SentKey9 := ""
  return
}

^!NumPad2::
{
  SentKey2 := !""
  ;Stuff to do, such as sending other hotkeys below, should  go here.
  SentKey0 := ""
  SentKey1 := ""
  SentKey3 := ""
  SentKey4 := ""
  SentKey5 := ""
  SentKey6 := ""
  SentKey7 := ""
  SentKey8 := ""
  SentKey9 := ""
  return
}

^!NumPad3::  
{
  SentKey3 := !""
  ;Stuff to do, such as sending other hotkeys below, should  go here.
  SentKey0 := ""
  SentKey1 := ""
  SentKey2 := ""
  SentKey4 := ""
  SentKey5 := ""
  SentKey6 := ""
  SentKey7 := ""
  SentKey8 := ""
  SentKey9 := ""
  return
}




Just download the script to test.


Right now, this script should check if I'm in Notepad.
When I enter Notepad it press: CTRL + ALT + Num 0 (only 1 time)
When I leave Notepad, it press: CTRL + ALT + Num 1 (only 1 time)

So far so good. But the "bad thing" right now is that I would need to copy paste the whole "IsActive" block for every different game.
I'm not sure about the Syntax of AHK. But I guess maybe it's better to make an array or something with all the "games" which should be checked by the "IsActive".
And maybe just check for the Window Title isn't enought?! Like if I would need to check for the Browser Chrome. It's always a different Window Title. Like:
Chrome + Website title
So every website would be a different Window Title.

So maybe it's better to check something like:

Code: Select all

IfWinActive World of Warcraft ahk_class GxWindowClass
Or like:

Code: Select all

Netflix ahk_class ApplicationFrameWindow
So I guess I have to take the script above (if it's working?!) and try to change it that it checks a list of "Games".
So what I have to do is to make 1 IsActive - Block for every OBS (Open Broadcaster) Scene.

1. IsActive - Block:
Game Overlay Scene (CTRL + ALT + 0) for every game. So I'll need a game list string / array.
when tabbing out of the GAME the else block comes: CTRL + ALT + 1 (which is basically an Overlay with Webcam + Chat).

2. IsActive - Block:
Show Browser Scene (CTRL + ALT + 7) for the browser, when I want to show a YouTube Video on stream.
when tabbing out of the BROWSER the else block comes: CTRL + ALT + 1 (which is basically an Overlay with Webcam + Chat).

And so on. So for every Scene I can create a "IsActive" block. That makes sense.
But for the 1. Block (and every other block) it would make more sense to have the option to give it a list.

Like:

Code: Select all

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


When I use all the Numblock Numbers from 0 to 9, I have to increase the script like I did above.
All variables from 0 to 9. Is this correct?!
So when CTRL + ALT + 0 is active, all others 1 to 9 get set to "empty".
So when CTRL + ALT + 1 is active, all others 2 to 9 and the 0 variable, get set to "empty".
So I would to that with all the the hotkeys in the "Press Hotkeys" section.


This post in SHORT:
1. Is my little re-write of the script still correct, can you test it on your end?
2. How do I ask for a LIST of active Windows? And much better maybe with AHK_Class. Or would the Windows Title be enough (think about chrome browser title for example)?!
3. I guess I need the possibility to PAUSE the script. When I press CTRL + ALT + (num) 3 it's my AFK-Scene.


I have to explain Point 3 too :-\
When I press CTRL + ALT + (num)3 my scene switch to a "full afk overlay". You don't see my game or anything else anymore.
But think about I press this hotkey, the programm goes to AFK-Scene and when I switch to Desktop / Game it would switch back to that scene.
Since the AHK-Script tells it... Sometimes you go AFK (CTRL + ALT + num3) and you work on something. Like new graphic settings in the game or searching something on Desktop.
So when switching out of the game, it would switch my AFK-Scene away. So basically I should put an IF arround the complete "Check Windows Actives" section, right?!

Like:

Code: Select all

IF(AFK = "")
{
IsActive:
 ....

IsActive:
 ....

IsActive:
 ....
}
So when I press CTRL + ALT + 3
it will change the scene, and set the AFK variable to:
AFK := !""
So that the "auto-switcher" is paused. Right?!
And when I press a hotkey manually again. Like CTRL + ALT + 0
I set the AFK variable back to empty:
AFK := ""

Since I don't know the Syntax of AHK completely. I'm allowed to just put a simple if around those IsActive blocks?!


Ok, thanks for reading this wholse stuff.
Take your time to response, if you like to response. Again, you really helped me out.
It's just a "list" for IsActive and the pause around the IsActive I guess. Then it would work just fine for me though.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: eazsy and 45 guests