Pergunta

I want to execute a function when something gets put on the clipboard (e.g. strg+c).

How can I observe the clipboard in TCL/TK so that i can handle a notify if something gets on it (event driven).

I did some research and the command after is not quite that what i was looking.

proc observeClipboard {} {

        set lClipboardContent [clipboard get]
        # do something with clipboard content
        after 1000 observeClipboard
}

It doesn't worked as expected and also it wouldn't be an event driven (smoother) solution.

Foi útil?

Solução

The simplest way is probably to always own the clipboard selection.
This has several downsides: You are responsible for the clipboard, and some clipboard contents might be lost, so this is not bullet-proof.

proc readclip {} {
    after 50 {
        puts [set cnt [clipboard get]]
        clipboard clear
        clipboard append $cnt
        selection own -command readclip -selection CLIPBOARD .
        selection handle . [list string range $cnt]
    }
}
selection own -command readclip -selection CLIPBOARD .

When readclip is invoked, the new application has requested the ownership over the clipboard, but it does not yet have the ownership, so we wait a bit to let it get it, setup everything etc.

Also note that if more than 1 application does this, both applications "battle" over the ownership of the clipboard, which is a bad thing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top