Autohotkey - Different commands when different programs are active

大城市里の小女人 提交于 2019-12-23 23:08:56

问题


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?


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/43087871/autohotkey-different-commands-when-different-programs-are-active

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!