代码之家  ›  专栏  ›  技术社区  ›  Edan Maor

从自动热键脚本捕获Windows上空文本的复制/粘贴

  •  2
  • Edan Maor  · 技术社区  · 15 年前

    默认情况下,在Windows上,当复制文本时,它会被放入剪贴板。但是当你试图复制 空的 文本,则剪贴板不受影响。例如,在编辑器中选择“无文本”,然后点击 ctrl+c ,将不会导致剪贴板发生更改。

    编辑: 为了澄清,我在AutoHotKey中发送ctrl+c。我这样做是为了判断是否有文本被选中,也就是说,我发送ctrl+c,然后检查是否有文本被复制到剪贴板。问题是,如果没有选择文本,自动热键的剪贴板处理程序就永远不会被调用,这迫使我使用超时,这不是一个好的做法。

    7 回复  |  直到 15 年前
        1
  •  3
  •   Samuel Tong    13 年前

    这是我所做的。因为剪贴板是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
    

    我正在从一个打开的文档中搜索文本,用户可以在其中选择前进或后退方向。当后退时,它会卡在文档开头的循环中。我设置了一个循环限制,以防止它成为一个无限循环,但它仍然浪费了等待循环完成的时间。如果剪贴板是空的,我使用break函数来结束循环。

    http://www.autohotkey.net/~deleyd/xprxmp/autohotkey_expression_examples.htm#J v:=”“

    If v =
        MsgBox v = ""
    
    If (v = "")
        MsgBox v = ""
    

    从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.
    

    塞缪尔

        2
  •  2
  •   Edan Maor    14 年前

    ctrl+c ,等待某个超时,然后查看文本是否确实被复制。如果不是,我知道没有选择。

    copy 操作。我把超时设置为0.15秒,所以还不算太糟。

    每当我想抓取剪贴板的内容,或者检查它是否为空时,我都会用到这个函数。我总是先调用这个函数:

    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
        }
    }
    

    IsTextSelected() {
        global ClipSaved
        global clip_empty
        global clipped_text
    
        if (clip_empty == true) {
            return true
        }
        else {
            return false
        }
    }
    

    在执行“Clip()”操作之后,我总是调用以下函数来恢复剪贴板(这个函数被调用 一旦 Clip() ):

    UnClip() {
        global ClipSaved
        global clip_empty
        global clipped_text
        global is_clipped
    
        is_clipped := false
        Clipboard := ClipSaved
        ClipSaved =
    }
    
        3
  •  2
  •   Syscall - leaving SO... Juhzuri    8 年前

    虽然不是这个问题的真正答案,但如果你是,谷歌搜索可能会把你带到这里 .

    控制 +

    ~^v::
    Trimmed := RegExReplace(Clipboard, "^\s+", "")
    Trimmed := RegExReplace(Trimmed, "\s+$", "")
    Clipboard = %Trimmed%
    SendInput ^v
    return
    
        4
  •  0
  •   Robert Mark Bram    14 年前

    我想我有个解决办法。将当前剪贴板放在一边,然后复制。将复制的内容与空字符串进行比较。。如果它是相等的,那么有东西被复制了;否则,什么都没有被复制。然后,将剪贴板还原为保存的内容。下面是一个代码示例,演示了该原理。

    ^#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
    

    实际上,我希望有另一种方法可以简单地测试光标当前是否正在选择任何内容。我在自动热键论坛上问过这个问题(http://www.autohotkey.com/forum/posting.php?mode=reply&t=69468),但直到或如果有更好的答案,我将使用上述方法。

        5
  •  0
  •   Sudarshan Hugo    13 年前

    巴比伦脚本(firefox的鼠标中键):

    MButton::
    SetTitleMatchMode, 2
    send {LButton}{LButton}
    Send ^c
    sleep, 100
    send {F10}
    sleep, 100
    SendInput {Raw}%clipboard%
    send {enter}
    Return
    
        6
  •  0
  •   bgmCoder    12 年前

    我也有同样的问题-我会发送复制命令,但它不会复制任何内容。我试着用计时器工作,但没用。

    以下是我最后所做的(尝试不同的模式):

    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
        }
    
        7
  •  0
  •   Shayki Abramczyk Cece Dong - MSFT    6 年前

    也许你应该用热键Ctrl+C来代替,这样每当按下热键你都会知道。

    您可能需要确保将常规的Ctrl+C操作发送到windows,以便进行复制。 考虑这个例子:

    ~^c::
    msgbox, % "Clipboard Changed even if you didnt copy anything"
            . "(...not really but you tried at least)"
    return
    

    从帮助文件:

    :当热键触发时,其键的本机功能不会被阻止(对系统隐藏)。

    你可能还想 onClipboardChange 以检查剪贴板何时真正更改。