我需要模拟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_())
答案 0 :(得分:3)
对于今后需要答案的人我在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_())
答案 1 :(得分:0)
没有深入研究这一点。我正在寻找一种方法将Unicode集限制为严格的ASCII,例如128 ...
[范围(128)中x的chr(x)] - >显示价值..