如何使用 python-keybinder 获取剪贴板内容

发布于 2024-12-29 06:10:06 字数 611 浏览 3 评论 0原文

我有两个代码示例。第一个是获取当前剪贴板内容并打印它,第二个是使用 python-keybinder 在热键按下时执行一些操作。我坚持将这两者结合在一起。我希望在热键按下时打印剪贴板内容(即我进行文本选择,按下热键并打印该选择)。这是我的代码:

获取选择:

import gtk

def _clipboard_changed(clipboard, event):
    text = clipboard.wait_for_text()
    print text

gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY).connect("owner-change", _clipboard_changed)

gtk.main()

绑定热键:

import gtk
import keybinder

def callback():
    print "pressed"
    gtk.main_quit()

if __name__ == '__main__':
    keystr = "<Ctrl>A"
    keybinder.bind(keystr, callback)
    gtk.main()

I've got two code samples. The first is to get current clipboard content and print it, the second is using python-keybinder to do some action on a hotkey press. I'm stuck with combining those two together. I want my clipboard content to be printed on a hotkey press (i.e. I do a text selection, press a hotkey and this selection is printed). Here's my code:

To get selection:

import gtk

def _clipboard_changed(clipboard, event):
    text = clipboard.wait_for_text()
    print text

gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY).connect("owner-change", _clipboard_changed)

gtk.main()

To bind a hotkey:

import gtk
import keybinder

def callback():
    print "pressed"
    gtk.main_quit()

if __name__ == '__main__':
    keystr = "<Ctrl>A"
    keybinder.bind(keystr, callback)
    gtk.main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2025-01-05 06:10:06

作为警告,我正在工作,目前无法测试此答案中的代码,但它至少应该为您指明正确的方向。

我认为问题是您无法将两者结合起来回调。
我能想到的解决方案有两种。

1) 使用全局变量来存储剪贴板数据并从中读取 keybinder 回调

    import gtk
    import keybinder

    cbText = ""

    def keybinder_callback():
        print cbText
        gtk.main_quit()

    def _clipboard_changed(clipboard, event):
        global cbText
        cbText = clipboard.wait_for_text()

    gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY).connect("owner-change", _clipboard_changed)

    if __name__ == '__main__':
        keystr = "<Ctrl>A"
        keybinder.bind(keystr, keybinder_callback)
        gtk.main()

2) 摆脱第一个回调。

    import gtk
    import keybinder

    def callback():
        print "pressed"
        clipboard = gtk.clipboard_get()
        text = clipboard.wait_for_text()
        print text
        gtk.main_quit()

    if __name__ == '__main__':
        keystr = "<Ctrl>A"
        keybinder.bind(keystr, callback)
        gtk.main()

Just as a warning I am at work and currently unable to test the code in this answer but it should at least point you in the right direction.

I assume the problem is that you can't combine the two callbacks.
There are two solutions I can think of.

1) Use a global to store the clipboard data and read from it the keybinder callback

    import gtk
    import keybinder

    cbText = ""

    def keybinder_callback():
        print cbText
        gtk.main_quit()

    def _clipboard_changed(clipboard, event):
        global cbText
        cbText = clipboard.wait_for_text()

    gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY).connect("owner-change", _clipboard_changed)

    if __name__ == '__main__':
        keystr = "<Ctrl>A"
        keybinder.bind(keystr, keybinder_callback)
        gtk.main()

2) Get rid of the first callback.

    import gtk
    import keybinder

    def callback():
        print "pressed"
        clipboard = gtk.clipboard_get()
        text = clipboard.wait_for_text()
        print text
        gtk.main_quit()

    if __name__ == '__main__':
        keystr = "<Ctrl>A"
        keybinder.bind(keystr, callback)
        gtk.main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文