Page 1 of 1

Different hotkeys for different winactives in one script

Posted: 20 May 2022, 07:15
by flacito
How to use same hotkeys for different windows, but for each window they have their own function in one script?
Using identical hotkeys for different windows does work when I save the script as different files, but this is ineffective as I would have to save multiple files. I want one file, same hotkeys but different functions for each window.

The script I'm using now have been revised many times, and I have tried different methods but none work. The problem is that the script works just as it would without it. My latest version is below.
For instance, the hotkeys in sublime text also work in explorer.exe, while I only want it to work in sublime text.

Code: Select all

#If WinActive ("ahk_exe sublime_text.exe")
ins::{
home::}

#If WinActive("ahk_exe Explorer.EXE")
!1::send, {enter}
!2::SendInput, {f2}
!3::SendInput, !{left}
f1::send, {enter}

#If WinActive("ahk_exe dopus.exe")
!1::send, {enter}
!2::SendInput, !{left}
!3::SendInput, !{right}
f1::send, {enter}


#If WinActive("ahk_exe Everything.exe")
!1::send, {enter}
f1::send, {enter}
\:: Send, {down}
]::Send, {up}

#If

Re: Different hotkeys for different winactives in one script  Topic is solved

Posted: 20 May 2022, 08:21
by BoBo

Code: Select all

#If WinActive("ahk_exe sublime_text.exe")
ins::{
home::}
#If

#If WinActive("ahk_exe Explorer.EXE")
!1::send, {enter}
!2::SendInput, {f2}
!3::SendInput, !{left}
f1::send, {enter}
#If

#If WinActive("ahk_exe dopus.exe")
!1::send, {enter}
!2::SendInput, !{left}
!3::SendInput, !{right}
f1::send, {enter}
#If


#If WinActive("ahk_exe Everything.exe")
!1::send, {enter}
f1::send, {enter}
\:: Send, {down}
]::Send, {up}
#If
Will this do it?

Edit. Removed redundant space character from initial WinActive()-function.

Re: Different hotkeys for different winactives in one script

Posted: 20 May 2022, 09:36
by flacito
BoBo wrote:
20 May 2022, 08:21

Code: Select all

#If WinActive ("ahk_exe sublime_text.exe")
ins::{
home::}
#If

#If WinActive("ahk_exe Explorer.EXE")
!1::send, {enter}
!2::SendInput, {f2}
!3::SendInput, !{left}
f1::send, {enter}
#If

#If WinActive("ahk_exe dopus.exe")
!1::send, {enter}
!2::SendInput, !{left}
!3::SendInput, !{right}
f1::send, {enter}
#If


#If WinActive("ahk_exe Everything.exe")
!1::send, {enter}
f1::send, {enter}
\:: Send, {down}
]::Send, {up}
#If
Will this do it?
Thanks for helping! Unfortunately, that doesn't work. I tried adding #If after every #IfWinactive, but that didn't work. I tried it again right now just to be sure, and it doesn't help

Re: Different hotkeys for different winactives in one script

Posted: 20 May 2022, 09:42
by BoBo
There's a redundant space character in your first WinActiveSpace()-function call. Why? :think:

Re: Different hotkeys for different winactives in one script

Posted: 20 May 2022, 09:48
by flacito
BoBo wrote:
20 May 2022, 09:42
There's a redundant space character in your first WinActiveSpace()-function call. Why? :think:
(
:lol: I noticed that too, highly frustrating. It isn't there anymore in the real script

Re: Different hotkeys for different winactives in one script

Posted: 20 May 2022, 09:57
by flacito
BoBo wrote:
20 May 2022, 09:42
There's a redundant space character in your first WinActiveSpace()-function call. Why? :think:
Wait a second, it works now! How is that possible?
first I added #if myself. Then I copied paste your script instead.
Now it works.

What did you do differently apart from adding #if?

Re: Different hotkeys for different winactives in one script

Posted: 20 May 2022, 10:28
by BoBo
What did you do differently apart from adding #if?
I removed that space character that I've 'adopted' with your initial code snippet without noticing it at that time. Mission accomplished. :thumbup:

Btw, you shouldn't label the thread as solved with highlighting the posting that contains the error. JM2€ents. ;)

Re: Different hotkeys for different winactives in one script

Posted: 20 May 2022, 12:43
by flacito
I guess you're right BoBo. Thanks!