Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hotkey if statement using multiple conditionals

The following script works to open Firefox's location/"awesome" bar from anywhere using control-l, except when using Acrobat/Adobe reader. This is because control-l in Acrobat goes full screen. It works, but it's ugly and uses nested #ifWinNotActive.

#IfWinNotActive, ahk_class MozillaWindowClass
#IfWinNotActive, ahk_class ahk_class AcrobatSDIWindow
^l::
WinActivate, ahk_class MozillaWindowClass
Send, ^l
return
#IfWinNotActive
#IfWinNotActive

The below code replacement doesn't work. Autohotkey doesn't complain with errors, but ignores the !WinActive conditionals and furthermore appears to become caught in an infinite loop. Any ideas why? (I tried the return statement both before and after the closing bracket.)

^l::
if (!WinActive(ahk_class,MozillaWindowClass)) and (!WinActive(ahk_class,AcrobatSDIWindow)) {
   WinActivate, ahk_class MozillaWindowClass
   Send, ^l
}
return
like image 302
Jeff Axelrod Avatar asked Mar 26 '12 01:03

Jeff Axelrod


People also ask

How to use multiple conditions in an IF statement?

Multiple conditions in if statement 1 and comparison = for this to work normally both conditions provided with should be true. If the first condition falls... 2 or Comparison = for this to work normally either condition needs to be true. The compiler checks the first condition... More ...

What is IF-ELSE conditional statement in Python?

Last Updated : 26 Mar, 2020 If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true.

How to use if and function in excel if condition?

Excel If and function. If you want to test multiple conditions and want every condition evaluates to true, then you need to use the AND function. Take a dataset listed in the screenshot. To evaluate commission, put the formula in the Commission box. =IF (AND (B2>=150,C2>=150),10%,IF (AND (B2>=101,C2>=101),7%,IF (AND (B2>=51),5%,IF (AND ...

How do you use multiple conditions in Excel VBA?

VBA IF Statement with Multiple Conditions: ElseIF Statement If you have multiple conditions and you want to extract the result by testing all the conditions with the IF statement, then you can utilize the ElseIF statement in VBA Excel. In the following section, we will show you the example with a VBA code.


1 Answers

With the WinActive function you need quotes around ahk_class MozillaWindowClass
and you don't need the comma. The infinite loop could be resolved by adding a hook $.

$^l::
if (!WinActive("ahk_class MozillaWindowClass"))
   and (!WinActive("ahk_class AcrobatSDIWindow")) 
{
   WinActivate, ahk_class MozillaWindowClass
   Send, ^l
} else
   Send, ^l
Return

However, writing it this way is only necessary if you are using AutoHotkey basic, which is out of date.
Unless you have a legitimate reason for not upgrading to AutoHotkey_L (which is unlikely)
you can accomplish what you were trying in the first example with the #If directive.

#If !WinActive("ahk_class CalcFrame") && !WinActive("ahk_class Notepad")

^l::
Run, notepad
Winwait, ahk_class Notepad
Send, test
Return

f1::traytip,, test

#If

In this example Ctrl+L and F1 will only work as coded if
calculator and/or notepad are not currently active,
otherwise they act as they normally would.

For anyone not familiar with AutoHotkey shorthand, ! means not.

like image 81
Honest Abe Avatar answered Sep 24 '22 20:09

Honest Abe