Skip to content

Instantly share code, notes, and snippets.

@trim21
Created April 9, 2019 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trim21/c0bbba5e22eec4c92550ba211ea4b11e to your computer and use it in GitHub Desktop.
Save trim21/c0bbba5e22eec4c92550ba211ea4b11e to your computer and use it in GitHub Desktop.

之前写了一个能用就行的版本, 也没认真研究过这个东西, 今天要写一个比较复杂的条件怎么也不能按照预想的办法运行才仔细看了看文档, 研究了一下.

gist

因为我平常喜欢在中文状态下用英文标点, 但是如果用英文标点又不加一个空格的话, 就会显得看起来很费劲, 所以这个脚本是用来在输入了逗号或者句号之后自动添加一个空格的.

但这个脚本我需要不在ide或者编辑器里运行, 最好是只在我需要输入文本的地方比如vnote, qq之类的地方运行, 所以需要匹配窗口的标题.

autohotkey要匹配窗口的话, 有几个地方需要设置:

SetTitleMatchMode example:

SetTitleMatchMode, 2
SetTitleMatchMode, Fast ;可以使用正则表达式对标题进行匹配

SetTitleMatchMode, 可以设置为1, 2, 3, RegEx或者Fast|Slow

(下面的WinTitle均为IfWinNotActiveWinTitle参数)

1是窗口的标题必须以WinTitle开始 2是窗口的标题必须包含WinTitle 3是两者必须完全相同 RegEx则是可以使用正则匹配 在设置为以上四个值之后, 还可以再设置为Fast或者Slow Fast 默认行为

这两者的区别我没有研究, 主要是WinActiveWinText参数行为有些不同,但我不用到这个参数, 所以没有仔细看文档

然后是定义具体的热键.

控制热键是否起效不能使用if(){}, 而必须使用#IfWinActive或者IfWinNotActive

example:

#IfWinNotActive

!c::
SendRaw, ````
Send, {Left}
return

#IfWinNotActive

(原来以为可以用if(){}来定义热键, 然后发现无论如何这些热键都是起效的, 仔细看文档才发现必须要用#If才行.)

然后就是#IfWinActive的参数.

#IfWinActive WinTitle, WinText 我没有用到WinText参数, 所以全部都是留空的

首先打开这个Window Spy

image

这里面的三行就对应WinTitle的三种写法

第一种: 直接写内容, 比如

#IfWinActive, Cent Browser
; hot keys
#IfWinActive

因为我前面选择了SetTitleMatchMode, 2, 这里标题中包含我的WinTitle CentBrowser, 所以在这里定义的hot keys会起效

第二种: 用ahk_class 调用参数的时候直接写成这样

#IfWinActive, ahk_class Chrome_WidgetWin_1
; hot keys
#IfWinActive

第三种: 用ahk_exe 指的是现在前台窗口的可执行文件的名字

#IfWinActive, ahk_class chrome.exe
; hot keys
#IfWinActive

还有第四种, ahk_group, 为了匹配一系列的窗口我用了这个功能

首先使用groupadd, 把一系列的匹配条件添加到一个group里

GroupAdd, NonMDApp, ahk_exe pycharm64.exe
GroupAdd, NonMDApp, ahk_exe Code.exe
GroupAdd, NonMDApp, ahk_exe goland64.exe
GroupAdd, NonMDApp, ahk_exe chrome.exe

这里就是把四个ahk_exe的匹配条件添加到了NonMDApp里面去.

然后在使用的时候使用#IfWinActive, ahk_group NonMDApp, 这个时候只要匹配到NonMDApp的任何一个条件, 就认为符合条件.

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, 2
SetTitleMatchMode, Fast ;可以使用正则表达式对标题进行匹配
GroupAdd, NonMDApp, ahk_exe pycharm64.exe
GroupAdd, NonMDApp, ahk_exe Code.exe
GroupAdd, NonMDApp, ahk_exe goland64.exe
GroupAdd, NonMDApp, ahk_exe chrome.exe
; GroupAdd, NonMDApp, ahk_exe mintty.exe
IME_GET(WinTitle="")
;-----------------------------------------------------------
; IMEの状態の取得
; 対象: AHK v1.0.34以降
; WinTitle : 対象Window (省略時:アクティブウィンドウ)
; 戻り値 1:ON 0:OFF
;-----------------------------------------------------------
{
ifEqual WinTitle,, SetEnv,WinTitle,A
WinGet,hWnd,ID,%WinTitle%
DefaultIMEWnd := DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hWnd, Uint)
;Message : WM_IME_CONTROL wParam:IMC_GETOPENSTATUS
DetectSave := A_DetectHiddenWindows
DetectHiddenWindows,ON
SendMessage 0x283, 0x005,0,,ahk_id %DefaultIMEWnd%
DetectHiddenWindows,%DetectSave%
Return ErrorLevel
}
!q::
ToolTip
If (IME_GET()=0)
ToolTip,EN ;shift得反着提示,提示切换后的状态。
else
ToolTip,中
return
#IfWinNotActive, ahk_group NonMDApp
!c::
SendRaw, ````
Send, {Left}
return
!b::
; blod
SendRaw **
Send {Left}
return
!l::
SendRaw []()
Send {Left}{Left}{Left}
return
$^v::
char1=http
endOfBlock = http
t = %clipboard%
firstChar:=SubStr(t, 1, 4)
IfInString, firstChar, %endOfBlock%
{
Length := StrLen(t) + 3
SendRaw [](
send %t%
send )
loop %Length%{
send {Left}
}
} else {
Send ^v
}
return
$,::
If (IME_GET()=0)
send `,
else
send `,{space}
return
$.::
If (IME_GET()=0)
send `.
else
send `.{space}
return
^Enter::
send {Home}{Enter}{Left}
return
!Enter::
send {End}{Enter}
return
#IfWinNotActive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment