click - 使用 AutoHotkey 单击窗口中的按钮

标签 click autohotkey

我想制作一个 AutoHotkey 脚本来更改 PuTTY SSH 客户端中的字体。 (我更喜欢小字体以获得高信息密度,但是当我向同事展示一些东西时,他们需要能够清楚地看到它。)我已经走了这么远:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
#SingleInstance force  ; Lets the RunMe plugin for Notepad++ reload the script with Shift-F5.

#IfWinActive ahk_class PuTTY    ; If PuTTY is active
  ^+1::                         ; and Ctrl-Shift-1 is pressed
  {
    Send !{Space}               ; Alt-Space to open the system menu
    Send g                      ; open Change Settings
    Send !g                     ; select the Category menu
    Send w                      ; select the Window category
    Send {Right}                ; expand the category
    Send a                      ; select the Appearance subcategory
    ControlClick, ClassNN Button8, ahk_class PuTTYConfigBox, , Left, 1
  }
#IfWinActive

当从 PuTTY 终端窗口运行时,通过“发送”的所有内容都按预期导航 PuTTY 菜单,将我带到外观子类别。此时我想单击“更改...”按钮来设置字体。我不想发送一堆标签或指定屏幕坐标来选择按钮;因为这看起来很笨拙,并且可能会随着 future 的更新而中断。但是,我无法让 ControlClick 工作。经过几个小时的研究,我上面使用的行是我最好的猜测,我不明白为什么它什么也没做。

这是我将鼠标悬停在按钮上时的 Window Spy 输出:
>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
PuTTY Reconfiguration
ahk_class PuTTYConfigBox

>>>>>>>>>>>>( Mouse Position )<<<<<<<<<<<<<
On Screen:  1051, 207  (less often used)
In Active Window:   432, 202

>>>>>>>>>( Now Under Mouse Cursor )<<<<<<<<
ClassNN:    Button8
Text:   Change...
Color:  0xF0F0F0  (Blue=F0 Green=F0 Red=F0)

>>>>>>>>>>( Active Window Position )<<<<<<<<<<
left: 619     top: 5     width: 456     height: 438

>>>>>>>>>>>( Status Bar Text )<<<<<<<<<<

>>>>>>>>>>>( Visible Window Text )<<<<<<<<<<<
&Apply
&Cancel
Cate&gory:
Cursor appearance:
B&lock
&Underline
&Vertical line
Cursor &blinks
Adjust the use of the cursor
Fo&nt used in the terminal window
Font: Lucida Console, 24-point
Change...
Allow selection of variable-pitch fonts
Font &quality:
Antialiased
Non-Antialiased
ClearType
Default
Font settings
Hide mouse &pointer when typing in window
Adjust the use of the mouse pointer
Gap b&etween text and window edge:
&Sunken-edge border (slightly thicker)
Adjust the window border

>>>>>>>>>>>( Hidden Window Text )<<<<<<<<<<<

>>>>( TitleMatchMode=slow Visible Text )<<<<
1

>>>>( TitleMatchMode=slow Hidden Text )<<<<

谢谢你的帮助。

最佳答案

我需要做两件事才能让它发挥作用。

首先,在第一个 ControlClick 参数中包含“ClassNN”这个词是错误的,尽管我发现有几个例子使用了它。参数可以是按钮的文本(Change...)、文本的开头(Change)或其ClassNN(Button8),但不能是“ClassNN Button8”。之后的所有内容都是不必要的,并且可以使用默认值正常工作。我目前只使用“ControlClick, Change...”作为整行,尽管明确指定 WinTitle 可能更明智(“PuTTY Reconfiguration”或“ahk_class PuTTYConfigBox”都有效)。

其次,正如 MCL 所指出的,在 ControlClick 命令之前,我需要“WinWait, PuTTY Reconfiguration”。我不完全清楚为什么,但它有效。

这是我最终的工作代码,F9 切换到 ProggyCleanTT 12 点,F10 切换到 Lucida Console 20 点:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
#SingleInstance force  ; Lets the RunMe plugin for Notepad++ reload the script with Shift-F5.

; This will only work for PuTTY sessions in which, under Window/Behavior, you have checked
; "System menu appears on ALT-Space". Don't forget to save the change.

#IfWinActive ahk_class PuTTY
  F9::ChangePuttyFont("ProggyCleanTT", 12)
  F10::ChangePuttyFont("Lucida Console", 20)
#IfWinActive

ChangePuttyFont(font, size)
{
  Send !{Space}               ; open the system menu
  Send g                      ; open Change Settings
  Send !g                     ; select the Category menu
  Send Window                 ; select the Window category
  Send {Right}                ; expand the category
  Send Appearance             ; select the Appearance subcategory
  WinWait, PuTTY Reconfiguration  ; This is necessary for some reason
  ControlClick, Change...     ; click the "Change..." button (under Font Settings)
  Send %font%                 ; select font
  Send !s                     ; select size field
  Send %size%                 ; select size
  Send {Enter}                ; close font window
  SEND !a                     ; close settings window
  return
}

如果您在按下热键和进一步输入之间不等待片刻,它会做一些奇怪的事情,并且它可能具有更强大的导航,但它确实有效。

关于click - 使用 AutoHotkey 单击窗口中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18839234/

相关文章:

android - 可点击的图像 - 安卓

scala - 使用 Scala 发送击键和鼠标点击

html-email - 通过带有表情符号的 ahk 发送电子邮件会产生问题

autohotkey - 有没有办法将多行字符串分配给对象属性?

autohotkey - 重新映射键的热键不会触发热字符串

jquery - jQuery 动画完成后如何执行 .click() ?

jQuery 在 prev 中找到相同的 td。和下一行

javascript - 检测到双击事件时需要取消点击/鼠标弹起事件

com - AHK COM 保存时通过文件路径传递变量

javascript - Angular Ctrl 单击?