使用AutoHotkey单击窗口中的按钮

时间:2013-09-16 23:54:59

标签: 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菜单,将我带到Appearance子类别。此时我想点击"更改..."按钮设置字体。我不想发送一堆标签或指定屏幕坐标来选择按钮;因为这看起来像kludgey,并可能打破未来的更新。但是,我无法让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 )<<<<

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

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

首先,在第一个ControlClick参数中包含单词“ClassNN”是错误的,尽管我发现使用了几个例子。参数可以是按钮的文本(更改...),文本的开头(更改)或其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
}

如果您在按热键和进一步输入之间没有等待片刻,它确实很奇怪,并且它可能具有更强大的导航功能,但它可以正常工作。