从 AutoHotKey 脚本在 Windows 上复制/粘贴空文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3381451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Catch copy/paste of empty text on Windows from AutoHotKey script
提问by Edan Maor
By default on Windows, when copying text, it gets put in the clipboard. But when attempting to copy emptytext, the clipboard is untouched. For example, selecting no text in your editor, then hitting ctrl+c
, will cause no change in the clipboard.
默认情况下,在 Windows 上,复制文本时,它会被放入剪贴板。但是当尝试复制空文本时,剪贴板没有受到影响。例如,在编辑器中不选择任何文本,然后点击ctrl+c
,剪贴板不会发生任何变化。
Problem is, I need to catch this event with AutoHotKey. Since the clipboard is unchanged, I have no idea how to do this cleanly (without a timeout, that is).
问题是,我需要用 AutoHotKey 来捕捉这个事件。由于剪贴板没有改变,我不知道如何干净地做到这一点(即没有超时)。
Does anyone have any idea how to do this?
有谁知道如何做到这一点?
Edit:To clarify, I'm sending the ctrl+c from within AutoHotKey. I'm doing so to tell if any text is selected, i.e., I'm sending ctrl+c, then checking if any text was copied to the clipboard or not. Problem is, if no text is selected, the clipboard handlers for AutoHotKey never get called, forcing me to use a timeout, which isn't good practice.
编辑:澄清一下,我从 AutoHotKey 中发送 ctrl+c。我这样做是为了判断是否选择了任何文本,即,我正在发送 ctrl+c,然后检查是否有任何文本被复制到剪贴板。问题是,如果没有选择文本,则 AutoHotKey 的剪贴板处理程序永远不会被调用,迫使我使用超时,这不是一个好习惯。
回答by Samuel Tong
Here is what I did. Since the clipboard is a variable in AutoHotkey, you can check to see if it is empty. I first cleared the clipboard, send control+c, then see if the clipboard is still empty. You can temporarily move the current clipboard to a temporary place first if you want.
这是我所做的。由于剪贴板是 AutoHotkey 中的一个变量,您可以检查它是否为空。我先清空了剪贴板,发送control+c,然后查看剪贴板是否还是空的。如果需要,您可以先将当前剪贴板临时移动到一个临时位置。
ClipSaved := ClipboardAll
Clipboard = ; empties the clipboard
Send ^+{Left} ; I just used highlight left to select text, you can replace this with
; whatever your program uses to select an input.
Send ^c ; attempt to copy text
If Clipboard = ; checks to see if clipboard is empty
{
break ; Put what you want to do if the clipboard is empty, I used break to stop a loop
}
Clipboard := ClipSaved ; puts the original clipboard contents back
I was searching text from an open document in which the user can choose a forward or backward direction. When going backwards, it would get stuck in a loop at the beginning of the document. I set a loop limit to keep it from being an infinite loop, but it still wasted time having to wait for the loop to finish. I used the break function to end the loop if the clipboard was empty.
我正在从一个打开的文档中搜索文本,用户可以在其中选择向前或向后的方向。倒退时,它会卡在文档开头的循环中。我设置了一个循环限制以防止它成为无限循环,但它仍然浪费时间不得不等待循环完成。如果剪贴板为空,我使用 break 函数结束循环。
To give credit where credit is due, I got the inspiration from another post which had other tood tips. It posted you can check for a blank variable with this script. http://www.autohotkey.net/~deleyd/xprxmp/autohotkey_expression_examples.htm#Jv := ""
为了在信用到期的地方给予信用,我从另一篇有其他提示的帖子中获得了灵感。它发布了您可以使用此脚本检查空白变量。 http://www.autohotkey.net/~deleyd/xprxmp/autohotkey_expression_examples.htm#Jv := ""
If v =
MsgBox v = ""
If (v = "")
MsgBox v = ""
From the AutoHotkey documentation website I found out how to temporarily store and replace the clipboard content. http://www.autohotkey.com/docs/misc/Clipboard.htm
从 AutoHotkey 文档网站我发现了如何临时存储和替换剪贴板内容。http://www.autohotkey.com/docs/misc/Clipboard.htm
ClipSaved := ClipboardAll ; Save the entire clipboard to a variable of your choice.
;... here make temporary use of the clipboard, such as for pasting Unicode text via Transform Unicode ...
Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved = ; Free the memory in case the clipboard was very large.
Hope this helps. Samuel
希望这可以帮助。塞缪尔
回答by Edan Maor
Here's the solution I currently use. Basically, it comes down to sending ctrl+c
, waiting a certain timeout, then seeing if text was actually copied. If it wasn't, I know there is not selection.
这是我目前使用的解决方案。基本上,它归结为 send ctrl+c
,等待某个超时,然后查看文本是否实际被复制。如果不是,我知道没有选择。
There is no way, afaik, to avoid waiting a timeout, since Windows takes a certain time to perform the copy
operation. I set the timeout to 0.15 seconds, so it isn't too bad.
没有办法避免等待超时,因为 Windows 需要一定的时间来执行copy
操作。我将超时设置为 0.15 秒,所以还不错。
Here's the function I use whenever I want to grab the contents of the clipboard, or check if it's empty. I always call this function first:
这是每当我想抓取剪贴板的内容或检查它是否为空时使用的函数。我总是先调用这个函数:
clipped_text :=
clip_empty := false
ClipSaved =
is_clipped := false
clip_speed := 0.15
Clip() {
global ClipSaved
global clip_empty
global clipped_text
global is_clipped
global clip_speed
if (!is_clipped) {
ClipSaved := ClipboardAll ; Save the entire clipboard to a variable of your choice.
; msgbox % ClipSaved
is_clipped := true
}
clipboard = ; Empty the clipboard
Send ^{c}
ClipWait clip_speed
if (ErrorLevel = 1)
{
clip_empty := false
}
else
{
clip_empty := true
clipped_text := clipboard
}
}
And I use this function to actually get the contents of the clipboard or check if it's empty:
我使用这个函数来实际获取剪贴板的内容或检查它是否为空:
IsTextSelected() {
global ClipSaved
global clip_empty
global clipped_text
if (clip_empty == true) {
return true
}
else {
return false
}
}
To get the contents of the clipboard I just look at the clipped_text variable.
要获取剪贴板的内容,我只需查看 clipped_text 变量。
After performing a "Clip()" operation, I always call the following function to restore the clipboard (this function is called oncefor multiple calls of Clip()
):
执行“Clip()”操作后,我总是调用以下函数来恢复剪贴板(多次调用该函数时调用一次Clip()
):
UnClip() {
global ClipSaved
global clip_empty
global clipped_text
global is_clipped
is_clipped := false
Clipboard := ClipSaved
ClipSaved =
}
回答by degenerate
While not an actual answer to this question, a google search might lead you here if you are looking for a way to catch text on paste and modify it before pasting.
虽然不是这个问题的实际答案,但如果您正在寻找一种在粘贴时捕获文本并在粘贴前修改它的方法,谷歌搜索可能会将您带到这里。
Here's the script which eliminates whitespace from text pasted from clipboard on CTRL+ V:
这是从剪贴板上粘贴的文本中消除空白的脚本CTRL+ V:
~^v::
Trimmed := RegExReplace(Clipboard, "^\s+", "")
Trimmed := RegExReplace(Trimmed, "\s+$", "")
Clipboard = %Trimmed%
SendInput ^v
return
回答by iran
Script for babylon (Middle Mouse Key for firefox):
babylon 脚本(firefox 的鼠标中键):
MButton::
SetTitleMatchMode, 2
send {LButton}{LButton}
Send ^c
sleep, 100
send {F10}
sleep, 100
SendInput {Raw}%clipboard%
send {enter}
Return
回答by bgmCoder
I had the same problem - I would send the copy command, but it wouldn't copy anything. I tried working with timers to no avail.
我有同样的问题 - 我会发送复制命令,但它不会复制任何东西。我尝试使用计时器但无济于事。
Here is what I ended up doing (trying different modes):
这是我最终做的事情(尝试不同的模式):
thisclipboard := clipboard . a_now ;add NOW so that it won't possibly be the same as the contents of the clipboard
sendplay,^c
if(clipboard == thisclipboard){
sendinput,^c
}
if(clipboard == thisclipboard){
send,^c
}
回答by RaptorX
Maybe you should hotkey the Ctrl + C instead, that way any time that hotkey is pressed you will know.
也许你应该热键 Ctrl + C,这样任何时候按下热键你都会知道。
You might want to make sure to send the normal Ctrl + C action to windows so you can copy. consider this example:
您可能需要确保将正常的 Ctrl + C 操作发送到 Windows,以便您可以进行复制。考虑这个例子:
~^c::
msgbox, % "Clipboard Changed even if you didnt copy anything"
. "(...not really but you tried at least)"
return
That message will fire up every time you press Ctrl + C even if you didnt copy anything to the clipboard. At the same time you will be sending the native function of Ctrl + C to windows so your clipboard WILL change if you copied something.
即使您没有将任何内容复制到剪贴板,每次按 Ctrl + C 时都会触发该消息。同时,您将 Ctrl + C 的本机功能发送到 Windows,因此如果您复制了某些内容,您的剪贴板将发生变化。
From the help file:
从帮助文件:
~: When the hotkey fires, its key's native function will not be blocked (hidden from the system).
~: 当热键触发时,其键的本机功能不会被阻止(对系统隐藏)。
You might want to also have an onClipboardChange
to check when the clipboard really changed.
您可能还想onClipboardChange
检查剪贴板何时真正发生变化。
回答by Robert Mark Bram
I think I have a solution. Set aside current clipboard, then copy. Compare what you have copied to an empty string.. if it's equal, then something was copied; otherwise, nothing was copied. At then, restore the clipboard to what you saved. Here is a code sample demonstrating the principle.
我想我有一个解决方案。搁置当前剪贴板,然后复制。将您复制的内容与空字符串进行比较。如果相等,则复制了某些内容;否则,不会复制任何内容。然后,将剪贴板恢复到您保存的内容。这是一个演示该原理的代码示例。
^#x::
ClipSaved := ClipboardAll ; Save the entire clipboard to a variable of your choice.
; ... here make temporary use of the clipboard, such as for pasting Unicode text via Transform Unicode ...
Clipboard := ; Clear the clipboard
Send, {CTRLDOWN}c{CTRLUP}
if (Clipboard = "") {
Send, you copied nothing
} else {
Send, you copied something
}
Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved = ; Free the memory in case the clipboard was very large.
return
Actually, I was hoping that there is another way to simply test if the cursor is currently selecting anything. I have asked this question on the AutoHotkey forums (http://www.autohotkey.com/forum/posting.php?mode=reply&t=69468), but until or if there is a better answer, I will use the above method.
实际上,我希望有另一种方法可以简单地测试光标当前是否正在选择任何内容。我已经在 AutoHotkey 论坛 (http://www.autohotkey.com/forum/posting.php?mode=reply&t=69468) 上问过这个问题,但是直到或者如果有更好的答案,我将使用上述方法。