Make the script work only in a specific window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gerard
Posts: 72
Joined: 01 Apr 2020, 13:14
Location: Buenos Aires
Contact:

Make the script work only in a specific window

12 Apr 2020, 16:45

Hi. Nice to greet you.
Very recently, I started to study the AHK syntax a bit, mainly to make accessible some audio programs that are not well labeled for screen readers. I am in awe of the possibilities, and have already been able to do some simple scripts.
I want to see how to make keyboard shortcuts only work in the plug-in window, and not in another. To not trigger things by mistake outside that window. This is the code for something I have done to use a virtual instrument called Perfect Drum.
Thank you in advance for any help.
Cheers!

Code: Select all

SoundBeep, 440, 50
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 5
oSpVoice.Volume :=75
oSpVoice.Speak("Script iniciado. Pulse f 1 Para la lista de comandos.")

Menu, MyMenu, Add, Kit 1, 1
Menu, MyMenu, Add, kit 2, 2
Menu, MyMenu, Add, Kit 3, 3
Menu, MyMenu, Add, Kit 4, 4
Menu, MyMenu, Add, Kit 5, 5
Menu, MyMenu, Add, Kit 6, 6
Menu, MyMenu, Add, Kit 7, 7
Menu, MyMenu, Add, Kit 8, 8
Menu, MyMenu, Add, Kit 9, 9
Menu, MyMenu, Add, Kit 10, 10
Menu, MyMenu, Add, Kit 11, 11
Menu, MyMenu, Add, Kit 12, 12
Return

!k::
SoundPlay, C:\Windows\Media\Windows Recycle.wav
Menu, MyMenu, Show
Return

SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
SetTitleMatchMode 2
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1

1:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 73 Left, Down
Sleep, 10
Click, 449, 73 Left, Up
Sleep, 10
Click, 449, 73 Left, Down
Sleep, 10
Click, 449, 73 Left, Up
Return

2:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 85 Left, Down
Sleep, 10
Click, 449, 85 Left, Up
Sleep, 10
Click, 449, 85 Left, Down
Sleep, 10
Click, 449, 85 Left, Up
Return

3:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 97 Left, Down
Sleep, 10
Click, 449, 97 Left, Up
Sleep, 10
Click, 449, 97 Left, Down
Sleep, 10
Click, 449, 97 Left, Up
Sleep, 10
Return

4:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 109 Left, Down
Sleep, 10
Click, 449, 109 Left, Up
Sleep, 10
Click, 449, 109 Left, Down
Sleep, 10
Click, 449, 109 Left, Up
Return

5:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 121 Left, Down
Sleep, 10
Click, 449, 121 Left, Up
Sleep, 10
Click, 449, 121 Left, Down
Sleep, 10
Click, 449, 121 Left, Up
Return

6:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 133 Left, Down
Sleep, 10
Click, 449, 133 Left, Up
Sleep, 10
Click, 449, 133 Left, Down
Sleep, 10
Click, 449, 133 Left, Up
Return

7:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 145 Left, Down
Sleep, 10
Click, 449, 145 Left, Up
Sleep, 10
Click, 449, 145 Left, Down
Sleep, 10
Click, 449, 145 Left, Up
Return

8:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 157 Left, Down
Sleep, 10
Click, 449, 157 Left, Up
Sleep, 10
Click, 449, 157 Left, Down
Sleep, 10
Click, 449, 157 Left, Up
Return

9:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 169 Left, Down
Sleep, 10
Click, 449, 169 Left, Up
Sleep, 10
Click, 449, 169 Left, Down
Sleep, 10
Click, 449, 169 Left, Up
Return

10:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 472, 44 Right, Down
Sleep, 10
Click, 472, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 181 Left, Down
Sleep, 10
Click, 449, 181 Left, Up
Sleep, 10
Click, 449, 181 Left, Down
Sleep, 10
Click, 449, 181 Left, Up
Return

11:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 420, 44 Right, Down
Sleep, 10
Click, 420, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 193 Left, Down
Sleep, 10
Click, 449, 193 Left, Up
Sleep, 10
Click, 449, 193 Left, Down
Sleep, 10
Click, 449, 193 Left, Up
Return

12:
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 470, 44 Right, Down
Sleep, 10
Click, 470, 44 Right, Up
Sleep, 10
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 449, 205 Left, Down
Sleep, 10
Click, 449, 205 Left, Up
Sleep, 10
Click, 449, 205 Left, Down
Sleep, 10
Click, 449, 205 Left, Up
Return

^r::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=85
oSpVoice.Speak("Snare seleccionado")

WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 409, 339 Right, Down
Sleep, 10
Click, 409, 338 Right, Up
Return

^b::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=85
oSpVoice.Speak("Kick seleccionado")
WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 483, 342 Right, Down
Sleep, 10
Click, 482, 338 Right, Up
Return

^1::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=75
oSpVoice.Speak("1")

WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 97, 655 Left, Down
Sleep, 10
Click, 97, 655 Left, Up
Return

^2::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=75
oSpVoice.Speak("2")

WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 215, 623 Left, Down
Sleep, 10
Click, 215, 623 Left, Up
Return

^3::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=75
oSpVoice.Speak("3")

WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 404, 653 Left, Down
Sleep, 10
Click, 404, 653 Left, Up
Sleep, 10
Return

^4::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=75
oSpVoice.Speak("4")

WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 523, 637 Left, Down
Sleep, 10
Click, 523, 637 Left, Up
Return

^5::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=75
oSpVoice.Speak("5")

WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 702, 630 Left, Down
Sleep, 10
Click, 702, 630 Left, Up
Return

^6::
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 6
oSpVoice.Volume :=75
oSpVoice.Speak("6")

WinActivate, Perfect Drums (x64 bridged) ahk_class REAPERb32host
Click, 826, 645 Left, Down
Sleep, 10
Click, 826, 645 Left, Up
Return

f1::
MsgBox, 0, Comandos.`n`n, Alt k. Seleccionar kit.`nControl b. Seleccionar bombo.`nControl r. Seleccionar Redoblante.`nControl 1 al 6, seleccionar marca.`nControl q. Pausa y reanuda el Script.`nAlt q. Salir.
Return

Suspend, Toggle
Return

^q::
Suspend
If (A_IsSuspended)
{
oSpVoice.Speak("Inactivo")
}
else
{
oSpVoice.Speak("Activo")
}
Return

!q::
SoundBeep, 587, 50
oSpVoice.Speak("Script Finalizado.")
ExitApp
[Mod edit: [code]code goes here[/code] tags fixed.]
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Make the script work only in a specific window

12 Apr 2020, 16:51

Hi,

Example:

Code: Select all

#if WinActive("ahk_class Notepad++")
1::
MsgBox, hello
return

#if
The MessageBox will only appear if you press 1 AND if you are in Notepad++.
Replace Notepad++ by your actual application.

Right click on the running AHK script (Green icon in the taskbar), go to "Window Spy", click on your application and check the AHK class.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
gerard
Posts: 72
Joined: 01 Apr 2020, 13:14
Location: Buenos Aires
Contact:

Re: Make the script work only in a specific window

12 Apr 2020, 17:49

Thank you very much. It works perfect. Sorry for the untagged code.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Panaku, Rohwedder, roysubs and 315 guests