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_())
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