我需要在pyqt4或pyside中模拟击键,我已经有了以下代码,这很好,但它只添加了大写拉丁字母,不允许使用小写字母或其他语言字母
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])
这是一个完整的例子,使想法工作与拉丁字母
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_())
对于任何将来需要答案的人,我在upwork.com上雇佣了一个人并得到了答案,这里是一个完整的工作示例,它适用于python 2和3、pyqt和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_())
不需要深入研究。我想找一种方法来限制你的Unicode集严格为ASCII,比如128…
[chr(x)for x in range(128)]->以显示值。。