Write to registry before exiting (reversing changes)? Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Fabulist
Posts: 7
Joined: 21 Jan 2016, 02:43

Write to registry before exiting (reversing changes)?

Post by Fabulist » 24 May 2022, 22:05

Hello everyone,

I have this script:

Code: Select all

RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Hotkey, 3
RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Language Hotkey, 3

#IfWinActive, ahk_exe game.exe

SetTimer process_watcher, 100
	process_watcher:
		Process Exist, game.exe
		If ErrorLevel = 0
				Exitapp
Return
That:
a) Writes (changes) existing registry keys that disable the Alt+Shift language toggle hotkey of MS Windows.
b) Checks if a certain program is running, and if not, it exits the script.

How can I restore the values, just before exiting, and only if the automatic exit is triggered?

Code: Select all

RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Hotkey, 1
RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Language Hotkey, 1
Thank you for reading.

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

Re: Write to registry before exiting (reversing changes)?

Post by boiler » 24 May 2022, 22:26

Just put those lines after the if statement and before the ExitApp, using { } to define the code block after the if.

By the way, the following line does nothing in your script because it only affects hotkeys, hotstrings, and key remaps:

Code: Select all

#IfWinActive, ahk_exe game.exe

Fabulist
Posts: 7
Joined: 21 Jan 2016, 02:43

Re: Write to registry before exiting (reversing changes)?

Post by Fabulist » 25 May 2022, 04:46

Hello, thank you for your reply.

Do you mean like this:

Code: Select all

SetTimer process_watcher, 100
	process_watcher:
		Process Exist, game.exe
		If ErrorLevel = 0
			{
			RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Hotkey, 1
			RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Language Hotkey, 1
			}
				Exitapp
Return
The above exits the script even if the application is running and does not change the values in the registry.

As for the:

Code: Select all

#IfWinActive, ahk_exe game.exe
You are right! The script does have some key remaps, that I want disabled when using Alt+Tab. Ideally, something similar would be done for the registry edits with RegWrite (besides exiting), also writing in the registry when the game is in focus, and reversing it when not in focus — but that is way beyond my understanding. My mistake leaving that line on my initial post.

Edit: Added "Return" at the end of code.

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

Re: Write to registry before exiting (reversing changes)?  Topic is solved

Post by boiler » 25 May 2022, 05:46

No, you have to include the ExitApp line within the code block you created or else your script will immediately exit no matter what the result of theif condition is. It should be clear why that would be the case.

Fabulist
Posts: 7
Joined: 21 Jan 2016, 02:43

Re: Write to registry before exiting (reversing changes)?

Post by Fabulist » 25 May 2022, 06:02

You are right, thank you so much!

The following code will:
a) Disable the Alt+Shift language toggle hotkey of MS Windows.
b) Check if a certain program is running ("game.exe"), and if not, it exits the script after reversing the Alt+Shift language toggle hotkey of MS Windows to its default state.

Code: Select all

RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Hotkey, 3
RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Language Hotkey, 3

SetTimer process_watcher, 100
	process_watcher:
		Process Exist, game.exe
		If ErrorLevel = 0
			{
			RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Hotkey, 1
			RegWrite, REG_SZ, HKEY_CURRENT_USER\Keyboard Layout\Toggle, Language Hotkey, 1
			ExitApp
			}
Return
Do you think it is possible to have the RegWrite initiate when the game.exe is in focus and reverse when it is not?

Post Reply

Return to “Gaming Help (v1)”