【发布时间】:2020-05-23 14:08:35
【问题描述】:
使用 AutoHotkey 脚本,我想设置键盘命令 Ctrl+D 以删除任何活动 Windows 应用程序中的当前行。
怎么做?
【问题讨论】:
标签: autohotkey
使用 AutoHotkey 脚本,我想设置键盘命令 Ctrl+D 以删除任何活动 Windows 应用程序中的当前行。
怎么做?
【问题讨论】:
标签: autohotkey
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del}
可能不适用于所有边缘情况,但通过记事本中的一些非常基本测试。 =~)
【讨论】:
HaveSpacesuit 的回答有效,但使用一段时间后,我意识到它会删除活动行,有时会重新定位下面行的间距。
这让我重新思考他的解决方案。我没有从队伍的前面走到后面,而是尝试从后面走到前面。这解决了重新定位问题。
SendInput {End}
SendInput +{Home}
SendInput ^+{Left}
SendInput {Delete}
不过还是有一个小问题。如果光标在空行上,上面还有更多空行,则所有空行都将被删除。
我不知道替换没有这种行为的^+{Left} 的组合键,所以我不得不编写一个更全面的解决方案。
^d:: DeleteCurrentLine()
DeleteCurrentLine() {
SendInput {End}
SendInput +{Home}
If get_SelectedText() = "" {
; On an empty line.
SendInput {Delete}
} Else {
SendInput ^+{Left}
SendInput {Delete}
}
}
get_SelectedText() {
; See if selection can be captured without using the clipboard.
WinActive("A")
ControlGetFocus ctrl
ControlGet selectedText, Selected,, %ctrl%
;If not, use the clipboard as a fallback.
If (selectedText = "") {
originalClipboard := ClipboardAll ; Store current clipboard.
Clipboard := ""
SendInput ^c
ClipWait .2
selectedText := ClipBoard
ClipBoard := originalClipboard
}
Return selectedText
}
据我所知,这不会产生意外行为。
但是,如果您使用剪贴板管理器,请小心,因为此脚本会在必要时使用剪贴板作为获取所选文本的中介。这将影响剪贴板管理器的历史记录。
【讨论】:
^d::SendInput {End} {ShiftDown}{Home 2}{Left}{ShiftUp}{Delete}{Right} 解决了重新定位问题,只删除了一个空行
如果您遇到需要为不同程序提供不同行为的问题,您可以为特定程序“复制”您的 ^d 命令,如下所示:
SetTitleMatchMode, 2 ; Makes the #IfWinActive name searching flexible
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del} ; Generic response to ^d.
#IfWinActive, Gmail ; Gmail specific response
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del} ; adapt this line for gmail
#IfWinActive ; End of Gmail's specific response to ^d
#IfWinActive, Excel ; Excel specific response.
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del} ; adapt this line for Excel
#IfWinActive ; End of Excel's specific response to ^d
这样,您的 ^d 命令在 Excel 和 Gmail 中的工作方式将有所不同。
【讨论】:
我有一个简单的方法来解决重新定位问题。不使用剪贴板。
重新定位问题是由于需要处理 2 个单独的案例。
如果一行中有现有文本, 我们要全选,然后删除文本(退格 1) 再退格一次以删除空行(退格2)
如果是空行, 我们要删除空行(退格1)
为了满足上述两种情况,我引入了一个虚拟角色。 这将确保两个案例的行为方式相同。 所以退格两次,每次都会产生相同的转换。
简单地说,
; enable delete line shortcut
^d::
Send {Home}
Send {Shift Down}{End}{Shift Up}
Send d
Send {Backspace 2}
Send {down}
return
这种方法的缺点, 撤消时会出现虚拟字符“d”。不错的权衡,因为我不经常撤消删除行。
【讨论】: