Keyboard Profiles Activate/Deactivate Topic is solved

Ask gaming related questions
User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Keyboard Profiles Activate/Deactivate

Post by WizardM » 19 Mar 2023, 00:36

Greetings,

Apologies for maybe a totally dumb request. Im a brand new totally confused user (installed AutoHotkey only a few hours ago). Last coding i ever did was more than 35 years ago and now have no idea how to do what I need. Tried searching through help and the forum but got even more confused. Appreciate any help...

I have a SteelSeries keyboard and the GG software is bloatware and a pain in the rear end. After the last update keyboard auto profile switching stopped working. I want to replace it with something reliable. In comes AutoHotkey...

I need to create a couple keyboard profiles to replace what SteelSeries GG software was doing. My main question is how to activate/deactivate a keyboard profile as a specific relevant application window comes/goes in and out of focus.

Im on Windows 10 and this is all what I figured out by myself so far. It works but lacks any activation/deactivation other than manual.

Code: Select all

t::1
g::2
d::3
e::4
In addition to the above I would like ask how to make a single press of a key send a series of keystrokes. A macro maybe, that for example runs when I press the "f" key and it sends the following sequence of keystrokes f, f, f, r, r, r, with XXms pause between them. How can I also do this?

Thanks to all for any help offered.

Rohwedder
Posts: 7677
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Keyboard Profiles Activate/Deactivate

Post by Rohwedder » 19 Mar 2023, 04:13

Hallo,
try:

Code: Select all

#Requires AutoHotkey v2.0
Profil := 0
!0::Global Profil := 0 ;Alt + 0: no Profil
!1::Global Profil := 1 ;Alt + 1: Profil 1
!2::Global Profil := 2 ;Alt + 2: Profil 2
#HotIf (Profil = 1)
t::1
g::2
d::3
e::4
#HotIf (Profil = 2)
t::4
g::3
d::2
e::1
#HotIf
$f::Send("{f 3}"), Sleep(500), Send("{r 3}") ; macro

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 19 Mar 2023, 05:51

Great thanks, that does it for manual (hotkey) profile switching. I got it working like this.

Code: Select all

Profil := 0
!0::Global Profil := 0 ;Alt + 0: no Profil
!1::Global Profil := 1 ;Alt + 1: Profil 1
!2::Global Profil := 2 ;Alt + 2: Profil 2
#HotIf (Profil = 1)
t::1
g::2
d::3
e::4
$f::Send("{f}"), Sleep(25), Send("{f}"), Sleep(25), Send("{f}"), Sleep(25), Send("{r}"), Sleep(25), Send("{r}"), Sleep(25), Send("{r}") ; macro
#HotIf (Profil = 2)
$f::Send("{W}"), Sleep(25), Send("{i}"), Sleep(25), Send("{z}"), Sleep(25), Send("{a}"), Sleep(25), Send("{r}"), Sleep(25), Send("{d}"), Sleep(25), Send("{M}") ; macro
I am greatefull as it gets me going but my primary question was about the solution by which the scrip recognizes the application window in focus and automatically activates a profile specific for that application without me needing to change profiles with a hotkey. For example:
1. I start a game executable future_war-W10.exe and when that game comes into focus the script automaticaly activates Profile 1 (Future War Profile)
2. I start Excel and when an Excel window comes into focus the script automaticaly activates Profile 2 (Excel Profile)
3. I start Discord and when a Discord window comes into focus the script automaticaly activates Profile 3 (Discord Profile)
4. When none of the specified applications are in focus the script automaticaly reverts to Profile 0 (NO Profile)

Rohwedder
Posts: 7677
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Keyboard Profiles Activate/Deactivate

Post by Rohwedder » 19 Mar 2023, 07:18

See: https://www.autohotkey.com/docs/v2/lib/_HotIf.htm#howto
E.g.:

Code: Select all

#HotIf WinActive("ahk_exe future_war-W10.exe")

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 19 Mar 2023, 17:52

thnx a lot, first attempts failed but will play around with it to see if I can get it to work.

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 19 Mar 2023, 19:00

I revert to one problem at a time. I again tried the manual profile switching to ensure there is nothing wrong there and discovered an issue with the macros so hope to get some direction on how to fix that and then go back to automation later.

The whole script works perfect in a text editor but neither of the below macros wants to work in the game for which this profile is intended. The "f" key locks up and doesnt do anything. Doesnt send even a single "f".

Code: Select all

$f::Send("{f 3}"), Sleep(200), Send("{r 3}") ; macro
$f::Send("{f}"), Sleep(25), Send("{f}"), Sleep(25), Send("{f}"), Sleep(25), Send("{r}"), Sleep(25), Send("{r}"), Sleep(25), Send("{r}") ; macro
I expect it has something to do with the sending mode but couldnt figure out how to try to send key press and key release separately. I easily made this macro work in the SteelSeries GG software as it right away offered sending a key press and a key release with delay between evey action. Something like this:

Code: Select all

KeyDown("{f}"), Sleep(25), KeyUp("{f}"), Sleep(25), KeyDown("{f}"), Sleep(25), KeyUp("{f}"), Sleep(25), KeyDown("{f}"), Sleep(25), KeyUp("{f}"), Sleep(25), KeyDown("{r}"), Sleep(25), KeyUp("{r}"), Sleep(25), KeyDown("{r}"), Sleep(25), KeyUp("{r}"), Sleep(25), KeyDown("{r}"), Sleep(25), KeyUp("{r}")
Is there a way to try something like this or is there some other way to make the macro work in AutoHotkey?

User avatar
boiler
Posts: 17057
Joined: 21 Dec 2014, 02:44

Re: Keyboard Profiles Activate/Deactivate

Post by boiler » 19 Mar 2023, 20:54

Code: Select all

Send("{f down}")
Same for up. See Repeating or Holding Down a Key.

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 19 Mar 2023, 21:17

:) thnx

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 19 Mar 2023, 23:26

Runing an auto detection test for the active window with this script. Notepad no issue with it being detected and and all the mapings and macros work flawlessly. This is also true if I activate the game keyboard profile/mappings manually with a hotkey.

With Winactive method for my game its as if though the line #HotIf WinActive("ahk_exe FutureWar_W10.exe") fails to detect the game window. The game is from Microsoft store and the executable FutureWar_W10.exe is In the C:\Program Files\WindowsApps\A278AB0D.ModernCombatFutureWar_66.0.7.0_x64__h6adky7gbf63m folder. The game window title bar displays "Sniper Fury". Might be worth noting that I cant run it by clicking the executable in its folder, only by the game installed icon. I added a couple shots of the game in task manager, showing the titles and PID. To me it all seems to be correct.

What am I doing wrong? What are the possible solutions to make this #HotIf work?

Code: Select all

#Requires AutoHotkey v2.0
#HotIf WinActive("ahk_exe FutureWar_W10.exe")
t::1
g::2
d::3
e::4
$f::Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20) ; jump and reload macro

#HotIf WinActive("ahk_class Notepad")
$F1::SendText("Hi, this is a F1 SendText")
$F2::SendText("Hi, this is a F2 SendText")
$F3::SendText("Hi, this is a F3 SendText")
t::1
g::2
d::3
e::4
$f::Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20) ; macro
[Mod edit: [code][/code] tags added. Please use them yourself when posting code.]

https://photos.app.goo.gl/f76XZCjWnZKsykxV6
https://photos.app.goo.gl/MnzPeTLz8gRnQ2CWA
[Mod edit: Removed img-tags. You can only use them with direct links.]

User avatar
boiler
Posts: 17057
Joined: 21 Dec 2014, 02:44

Re: Keyboard Profiles Activate/Deactivate

Post by boiler » 19 Mar 2023, 23:48

You might need to run the script as administrator or use one of the other approaches described here.

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 20 Mar 2023, 02:37

I would love to say that it worked but it didnt. I tried both of the below. Quoted from the link U sent.

Recommended: Run with UI access. This requires AutoHotkey to be installed under Program Files. There are several ways to do this, including:
  • Right-click the script in Explorer and select Run with UI access.
  • Set scripts to run with UI access by default, by checking the appropriate box in the Launch Settings GUI.

User avatar
boiler
Posts: 17057
Joined: 21 Dec 2014, 02:44

Re: Keyboard Profiles Activate/Deactivate

Post by boiler » 20 Mar 2023, 05:20

Are you saying that the very first scripts you posted work with your game so that we know it accepts virtual keypresses?

Try the following script that will help tell us whether the window is being identified.

Code: Select all

F1::MsgBox "FutureWar " . (WinExist("ahk_exe FutureWar_W10.exe") ? "exists" : "doesn’t exist")

#HotIf WinActive("ahk_exe FutureWar_W10.exe")
F2::MsgBox "FutureWar recognized as the active window"

Also, what does the top section of the Window Spy tool show for this window?

By the way, I am moving this topic to the “Gaming” section since the application in question is a game.

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 20 Mar 2023, 07:12

This script is a stepping stone but works with the game without any issues

Code: Select all

#Requires AutoHotkey v2.0
Profil := 1
!1::Global Profil := 1 ;Alt + 1: No Profil
!2::Global Profil := 2 ;Alt + 2: Profil 2
#HotIf (Profil = 2)
t::1
g::2
d::3
e::4
$f::Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{f down}"), Sleep(20), Send("{f up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20), Send("{r down}"), Sleep(20), Send("{r up}"), Sleep(20) ; macro
Runing your last message box script does nothing.

This is the window spy shot.
https://photos.app.goo.gl/6z8y1Vx2wC8BEhU96

Im setting this up as profiles for a range of apps, not just the game but sure we are now working on a game profile so yea move it.

User avatar
boiler
Posts: 17057
Joined: 21 Dec 2014, 02:44

Re: Keyboard Profiles Activate/Deactivate

Post by boiler » 20 Mar 2023, 07:18

WizardM wrote: Runing your last message box script does nothing.
How can that be? Did you press F1? What did it show?

WizardM wrote: This is the window spy shot.
That shows the problem. You’re not using the right process name. You need to use ApplicationFrameHost.exe instead of FutureWar_W10.exe.

User avatar
WizardM
Posts: 43
Joined: 18 Mar 2023, 23:08

Re: Keyboard Profiles Activate/Deactivate

Post by WizardM » 20 Mar 2023, 07:50

ahhhh, was pressing F2.... works with F1. Doesnt exist.

and yes problem solved when using the process from the Window Spy. I have X-Mouse Button Control installed and it uses futurewar_w10.exe so I falsly assumed AutoHotkey would also.

Thanx very much, your help is very much appreciated.

As of this moment I have 2 working profiles and intend to add 2-3 more. Now, after all the help I received, it shouldnt be a problem any more to do that by myself but I do have a question on how to customize the try icon or add some short text to the taskbar to indicate which #HotIf / keyboard profile is active at the moment.

User avatar
boiler
Posts: 17057
Joined: 21 Dec 2014, 02:44

Re: Keyboard Profiles Activate/Deactivate  Topic is solved

Post by boiler » 20 Mar 2023, 08:31

WizardM wrote: Thanx very much, your help is very much appreciated.
Glad to help.

WizardM wrote: I do have a question on how to customize the try icon or add some short text to the taskbar to indicate which #HotIf / keyboard profile is active at the moment.
See TraySetIcon. You would probably have to use SetTimer to be checking which window is active and change the icon accordingly, unless you want to use one of the scripts that are out there to register window events to trigger when the icon would be changed.

Post Reply

Return to “Gaming”