;在某特定控件激活时生效。可以在记事本中测试(编辑窗口为Edit1控件),在Word中不生效:
#If ActiveControlIs("Edit1")
6::Send {F5}
7::MsgBox
#If
ActiveControlIs(Control) {
ControlGetFocus, FocusedControl, A
return (FocusedControl=Control)
}
在某类控件激活时生效,代码如下
; 示例 : 在所有的编辑控件中的轻松删除单词的快捷键.
#If ActiveControlIsOfClass("Edit")
^BS::Send ^+{Left}{Del}
^Del::Send ^+{Right}{Del}
ActiveControlIsOfClass(Class) {
ControlGetFocus, FocusedControl, A
ControlGet, FocusedControlHwnd, Hwnd,, %FocusedControl%, A
WinGetClass, FocusedControlClass, ahk_id %FocusedControlHwnd%
return (FocusedControlClass=Class)
}
#If
无需激活在某类窗口上即生效。代码如下
; 示例 : 在任务栏上滚动鼠标来调节音量.
#If MouseIsOver("ahk_class Shell_TrayWnd")
WheelUp::Send {Volume_Up}
WheelDown::Send {Volume_Down}
MouseIsOver(WinTitle) {
MouseGetPos,,, Win
return WinExist(WinTitle . " ahk_id " . Win)
}
在某特定窗口激活时生效,用#IfWin 指令可以简单地创建窗口激活时的、上下文相关的热键. 例如:
#IfWinActive ahk_class Notepad #space::MsgBox You pressed Win+Spacebar in Notepad.
#IfWinExist ahk_class Notepad #n::WinActivate ; 激活由 #IfWin 找到的窗口.
在某些窗口激活时生效,通过 #IfWinActive ahk_group MyGroup
使用窗口组
函数化:
#IfWinActive, - Microsoft Outlook ahk_class RCtrl_renwnd32 ;for Outlook 2010, uncomment this line
y::HandleOutlookKeys("^+1", "y") ;archive message using Quick Steps Hotkey (ctrl+Shift+1)
f::HandleOutlookKeys("^f", "f") ;forwards message
r::HandleOutlookKeys("^r", "r") ;replies to message
#IfWinActive
HandleOutlookKeys( specialKey, NormalKey )
{
;Activates key only on main outlook window, not m, tasks, contacts, etc.
IfWinActive, - Microsoft Outlook ahk_class RCtrl_renwnd32, NUIDocumentWindow, , ;for Outlook 2010, uncomment this line
{
;Find out which control in Outlook has focus
ControlGetFocus currentCtrl, A
;Set list of controls that should respond to specialKey. Controls are the list of emails and the main
;(and minor) controls of the reading pane, including controls when viewing certain attachments.
ctrlList = Acrobat Preview Window1,AfxWndW5,AfxWndW6,EXCEL71,MsoCommandBar1,OlkPicturePreviewer1,paneClassDC1,OutlookGrid1,OutlookGrid2,RichEdit20WPT2,RichEdit20WPT4,RichEdit20WPT5,RICHEDIT50W1,SUPERGRID2,SUPERGRID1,_WwG1
If currentCtrl in %ctrlList%
Send %specialKey%
;Allow typing normalKey somewhere else in the main Outlook window. (Like the search field or the folder pane.)
Else
Send %NormalKey%
}
;Allow typing normalKey in another window type within Outlook, like a mail message, task, appointment, etc.
Else
{
Send %NormalKey%
}
}