Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: restrict AutoHotkey and turn off them for other applications

Hello experts,

I was wondering how to restrict specific AutoHotkey for application and turn off them for other applications.
Example:
I would like to use the following AutoHotkey exclusively when I used Excel:
;====================================
;Excel paste special
;====================================
;$if the send contains same letters of hotstring
$^#v::
If (WinActive("ahk_exe excel.exe")) 
{
  Send, !esvt
  Sleep, 100
  Send, {Enter}
  Return
}
Else
{
  MsgBox,4144,Error,Active windows is not an excel windows
  Return
}

Open in new window


The benefit of If approach condition is that I restrict the AutoHotkey for Excel however the drawback is that I cannot turn off for other applications.
Let's suppose that I change the AutoHotkey for:
;====================================
;Excel paste special
;====================================
;$if the send contains same letters of hotstring
#d::
If (WinActive("ahk_exe excel.exe")) 
{
  Send, !esvt
  Sleep, 100
  Send, {Enter}
  Return
}
Else
{
  MsgBox,4144,Error,Active windows is not an excel windows
  Return
}

Open in new window

When Notepad ++ process is running.
I would have error message instead of having the default windows action related to #d which is open desktop.
This question is crucial for my daily actions as I want to avoid clash between other default windows shortcuts.
Thank you for your help.
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Joe, sorry I forgot that I can manage this through context sensitivity.
Thank you very much for the reminder!
You're welcome, Luis, happy to help. Regards, Joe
Joe,
If I want to put double conditions, can I proceed like this:

;====================================
;Excel Fill
;====================================

#If (WinActive("ahk_exe excel.exe") or WinActive("ahk_exe WINWORD.exe"))
$#f::
Send, !hh
Sleep, 100
Return
#If ; end the context-sensitivity so it doesn't affect subsequent hotkeys/hotstrings

Open in new window


Thank you for your help.
> can I proceed like this

Yes. Notice this at the #If documentation (copied here under "Fair Use"):
#If [Expression]

Creates context-sensitive hotkeys and hotstrings. Such hotkeys perform a different action (or none at all) depending on the result of an expression.

Expression: Any valid expression.

Any valid expression may be used to define the context in which a hotkey should be active. For example:

#If WinActive("ahk_class Notepad") or WinActive(MyWindowTitle)

Thus, you may proceed with the AutoHotkey code that you posted above...it will work fine! Regards, Joe
Noted, thank you very much for your feedback!
You're very welcome!