如何使用 python-keybinder 获取剪贴板内容
我有两个代码示例。第一个是获取当前剪贴板内容并打印它,第二个是使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为警告,我正在工作,目前无法测试此答案中的代码,但它至少应该为您指明正确的方向。
我认为问题是您无法将两者结合起来回调。
我能想到的解决方案有两种。
1) 使用全局变量来存储剪贴板数据并从中读取 keybinder 回调
2) 摆脱第一个回调。
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
2) Get rid of the first callback.