send keystrokes from unicode string pyqt pyside

2k views Asked by At

I need to simulate keystrokes in pyqt4 or pyside, I already have the following code which is fine but it adds only capital Latin letters, no small or other languages letters are allowed

def sendkeys(self, char, modifier=QtCore.Qt.NoModifier):
    event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
    QtCore.QCoreApplication.postEvent(self, event)

def sendstring(self, string):
    for chara in string:
        print(chara)
        self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0])

this is a complete example to get the idea work with latin letters

from PySide import QtCore, QtGui, QtWebKit


class WebPage(QtWebKit.QWebPage):
    """
    QWebPage that prints Javascript errors.
    """

def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
    print('Javascript error at line number %d' % lineNumber)
    print('%s' % message)
    print('Source ID: %s' % sourceID)


class myWindow(QtWebKit.QWebView):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)
        self.loadFinished.connect(self.on_loadFinished)
        self.runloadfinished = True
        self.web_page = WebPage()
        self.setPage(self.web_page)
        self.load(QtCore.QUrl('http://stackoverflow.com/'))
def on_loadFinished(self):
    if self.runloadfinished:
        self.sendkeys(QtCore.Qt.Key_Tab)
        self.sendstring('Hello World')
        self.sendkeys(QtCore.Qt.Key_Enter)
        self.sendkeys(QtCore.Qt.Key_Return)
        self.runloadfinished = False

def sendkeys(self, char, modifier=QtCore.Qt.NoModifier):
    event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
    QtCore.QCoreApplication.postEvent(self, event)

def sendstring(self, string):
    for chara in string:
        print(chara)
        self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0])


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')
    main = myWindow()
    main.show()
    sys.exit(app.exec_())
2

There are 2 answers

0
Mohamed Yousof On BEST ANSWER

for anyone who needs an answer in future I have hired someone on upwork.com and got the answer, here is a full working example and it works with python 2 and 3, pyqt and pyside

# -*- coding: utf-8 -*-

from PySide import QtCore, QtGui, QtWebKit


class WebPage(QtWebKit.QWebPage):
    """
    QWebPage that prints Javascript errors.
    """

    def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
        print('Javascript error at line number %d' % lineNumber)
        print('%s' % message)
        print('Source ID: %s' % sourceID)


class myWindow(QtWebKit.QWebView):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)
        self.loadFinished.connect(self.on_loadFinished)
        self.runloadfinished = True
        self.web_page = WebPage()
        self.setPage(self.web_page)
        self.load(QtCore.QUrl('http://stackoverflow.com/'))

    def on_loadFinished(self):
        if self.runloadfinished:
            self.sendkeys(QtCore.Qt.Key_Tab)
            self.sendstring("لا اله الا الله ")
            #self.sendstring("Hello World")
            self.sendkeys(QtCore.Qt.Key_Enter)
            self.sendkeys(QtCore.Qt.Key_Return)
            self.runloadfinished = False

    def sendkeys(self, char, modifier=QtCore.Qt.NoModifier, text=None):
        if not text:
            event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
        else:
            event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier, text)
        QtCore.QCoreApplication.postEvent(self, event)

    def sendstring(self, string):
        for chara in string:
            print(chara)

            # QtCore.QString() is missing when using PyQt4 for Python3
            # since str() is unicode by default on Python3
            # http://inputvalidation.blogspot.in/2010/10/python3-pyqt4-and-missing-qstring.html
            try:
                text = QtCore.QString(chara)
            except AttributeError:
                text = str(chara)

            self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0],
                          text=text)


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')

    main = myWindow()
    main.show()

    sys.exit(app.exec_())
1
Michael Mather On

Without looking deep into this. I'd look for a way to limit your Unicode set to strictly ASCII say 128...

[chr(x) for x in range(128)] --> to show values..