如何解决如何使用 AutoHotKey 在 Microsoft Outlook 搜索字段中检测焦点
问题
如何编写 AutoHotKey 以检测窗口顶部的 Microsoft Outlook 搜索字段,以便仅在该窗口之外启用热键?该搜索字段如下所示:
详情
我打算添加类似于 gmail 的单字符热键: https://support.google.com/mail/answer/6594?hl=en#zippy=%2Cactions ,特别是用于删除的 # 键绑定。我的实验基于 AutoHotKey 脚本,例如:
https://github.com/pimlottc-gov/gmailkeys/blob/master/gmailkeys-2013.ahk#L37
该脚本使用 #IfWinActive 将其热键限制在主 Outlook 窗口中:
#IfWinActive,- Outlook ahk_class rctrl_renwnd32,NUIDocumentwindow ;for Outlook 2013,uncomment this line
但是,如果我不更改上面的 #IfWinActive
语句,然后通过以下方式添加 #
热键:
+3::Send {Delete} ; Means "#" key: Delete selected message(s)
然后在运行脚本时,然后单击上面的搜索字段,然后键入 #,然后当然会将 Delete 键发送到该字段中,而不仅仅是将 # 传递到搜索字段中。
我通过重新绑定 Ctrl+e 和 /(后者是用于搜索/过滤的 Gmail 绑定)到一个临时输入弹出窗口,我在其中输入搜索表达式,然后让 AutoHotKey 将其输入到字段中。但这当然是一个黑客:
; Search for some expression
;
; Hack: We have to popup an input dialog Box,prompt for the
; search expression,and then when done,type it into the
; underlying search field. This is needed to avoid having other
; single-key bindings get "eaten" when needing to type into the
; Outlook search field,as as of 2021-05-23 I Could not find a way
; to detect that specific input field.
;
^e::
/::
; Save current active (Outlook) window so we can return to after prompting for the search expression:
WinGet,winid,A ; <-- need to identify window A = active
; Prompt for the search expression:
InputBox,search_expr,Search Expression,Enter the search expression.
; Return to the Outlook window:
WinActivate ahk_id %winid%
; If the user presses Escape or clicks on the Cancel button,do nothing:
if (!ErrorLevel) {
; but if we are doing the search:
; Get into the search field:
Send ^e
; Select all prior text so we can wipe it out:
Send ^a
; ... by typing in all of the expression:
Send %search_expr%
; then do the search:
Send {Enter}
}
return
无论我在 Outlook 主窗口 Window Spy(AutoHotKey 附带的应用程序)中单击什么位置,类始终保持不变。
AutoHotkey 版本:1.1.33.08
解决方法
请注意,Shift+3 只会在您的键盘布局上产生 #。实际使用 # 键作为热键会更正确。
此外,您引用的代码是非常传统的 AHK。里面有不少东西不属于现代AHK。
我可能还建议使用上下文敏感的热键来执行此操作。
这样,您将保留 # 键的本机功能。
上下文敏感的热键可以这样完成:
SetTitleMatchMode,2
#If,WinActive("A") == WinExist("- Outlook ahk_exe OUTLOOK.EXE") && !SearchBarFocused()
#::SendInput,{Delete}
#If
SearchBarFocused()
{
ControlGetFocus,ctrl
return InStr("RICHEDIT60W1,RichEdit20WPT1",ctrl)
}
我还首先检查 Outlook 是否实际上是活动窗口。可能有点多余,但可以使重映射无法在其他具有该名称控件的窗口中处于活动状态。
,经过一些实验(包括根据 0x464e's answer 进行的简化)后,我找到了解决方案,并且本质上反转逻辑,在某种程度上,在 https://github.com/pimlottc-gov/gmailkeys/blob/master/gmailkeys-2013.ahk#L76 中:
; Outlook-specific Hotkeys:
;
; Reference material used in this implementation:
; https://autohotkey.com/board/topic/38389-archiving-email-in-outlook-with-a-single-keystroke/
;
; As best I can tell,the window text 'NUIDocumentWindow' is not present
; on any other items except the main window. Also,I look for the phrase
; ' - Outlook' in the title,which will not appear in the title (unless
; a user types this string into the subject of a message or task).
#IfWinActive - Outlook ahk_class rctrl_renwnd32,NUIDocumentWindow
#::
if (SentNormalKeyInOutlookEditControl("#"))
{
return
}
Send {Delete} ; Delete selected message(s)
return
#IfWinActive
SentNormalKeyInOutlookEditControl(normal_key)
{
; Find out which control in Outlook has focus
ControlGetFocus,currentCtrl
; ; 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.
; ; The control 'RichEdit20WPT1' (email subject line) is used extensively for inline editing. Thus it had to be included in bad_ctrl_list.
; ctrlList = Acrobat Preview Window1,AfxWndW5,AfxWndW6,EXCEL71,MsoCommandBar1,OlkPicturePreviewer1,paneClassDC1,RichEdit20WPT2,RichEdit20WPT4,RichEdit20WPT5,RICHEDIT50W1,SUPERGRID1,_WwG1
bad_ctrl_list = RICHEDIT60W1,RichEdit20WPT1
; RICHEDIT60W1 is the search field at the top of the window
if currentCtrl in %bad_ctrl_list%
{
; MsgBox,BAD Control with focus = %currentCtrl% normal_key = %normal_key%
Send %normal_key%
return 1
}
return 0
}
编辑 2021-05-25 06:58:23:根据 0x464e's answer 用“#”替换旧的“+3”。 编辑 2021-05-25 08:20:09:防止“-消息”窗口接收热键(单独的消息窗口,而不是主窗口)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。