esehov11
esehov11

Reputation: 35

Autohotkey - Different commands when different programs are active

I am trying to write a script that when clicking "SHIFT+ALT+I" it sends a particular command when Microsft Word is active and another command if Microsoft PowerPoint is Active (both programs are running at the same time).

The code should be something like:

"if Microsoft Word is open"
+!i::
MouseMove, 300,50,0
MouseClick, left

"if Microsoft Powerpoint is open"
+!i::
MouseMove, 600,100,0
MouseClick, left

Is there any (easy) way of doing that?

Upvotes: 3

Views: 647

Answers (2)

Jim U
Jim U

Reputation: 3366

This is the correct way of defining application-specific hotkeys

Define F1 macros conditional on active window:

SetTitleMatchMode 2     ; All #If statements match anywhere in title

#IfWinActive Microsoft Word
  F1:: MsgBox F1 pressed in Word
#IfWinActive Notepad
  F1:: MsgBox F1 pressed in Notepad
#IfWinActive
  F1:: MsgBox F1 pressed elsewhere

Upvotes: 2

Thorondale
Thorondale

Reputation: 73

Yes, this is quite straightforward.

First off: The structure you propose is possible, but not with a standard hotkey trigger. You can have a timer running which checks if a window is open and that then detects keypresses and then takes action. Or you can indeed use what @user already linked to. I would consider it more neat and efficiënt to have one hotkey trigger a window check and then take action accordingly.

The code would then look like:

+!i::
IfWinActive, Microsoft Word
{
MouseMove, 300,50,0
MouseClick, left
}

IfWinActive, Microsoft Powerpoint
{
MouseMove, 600,100,0
MouseClick, left
}

Return

Do note that this function checks the window title, so there has in this example to be "Microsoft Word" literally in the window title. Read up on this function here: https://autohotkey.com/docs/commands/WinActive.htm

Upvotes: 1

Related Questions