Skip to content Skip to sidebar Skip to footer

Get The Selected Text Content From Other Programs

When I am using other programs (e.g. opening a pdf or word), I will select some text contents (like a word or paragraph) by using the mouse. I want my python program to get this te

Solution 1:

This is an easy task, you haven't specified the pyqt version, so I'll post the solution for PyQt4, here you go:

from PyQt4.QtCore import QObject, pyqtSlot, SIGNAL, SLOT
from PyQt4.QtGui import QApplication, QMessageBox
import sys


class MyClipboard(QObject):

    @pyqtSlot()
    def changedSlot(self):
        if(QApplication.clipboard().mimeData().hasText()):
            QMessageBox.information(None, "Text has been copied somewhere!",
                                    QApplication.clipboard().text())


def main():
    app = QApplication(sys.argv)
    listener = MyClipboard()

    app.setQuitOnLastWindowClosed(False)
    QObject.connect(QApplication.clipboard(), SIGNAL(
        "dataChanged()"), listener, SLOT("changedSlot()"))

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Post a Comment for "Get The Selected Text Content From Other Programs"