[英]how can i get the alphabet value like F1 or F2 from keyPressEvent?
I want to print key value like ['a', 'b', ... , 'f11', 'f12']我想打印像 ['a', 'b', ... , 'f11', 'f12'] 这样的键值
def keyPressEvent(self,e):
print(e.key())
but when i press f1, the function key, then it printed the value 16777264. how can i convert 16777264 to f1?但是当我按下 f1 时,function 键,然后打印值 16777264。我怎样才能将 16777264 转换为 f1?
key()
returns an int that can be matched using the QtCore.Qt.Key
enum. key()
返回一个可以使用QtCore.Qt.Key
枚举匹配的 int。
Qt provides F key identifiers up to F35, so you can build a simple map and check it in the key event. Qt 提供高达 F35 的 F 键标识符,因此您可以构建一个简单的 map 并在键事件中检查它。
class FKeyTest(QtWidgets.QWidget):
fKeys = {}
for f in range(1, 36):
fKeys[getattr(QtCore.Qt, 'Key_F{}'.format(f))] = 'F{}'.format(f)
def keyPressEvent(self, event):
key = self.fKeys.get(event.key())
if key:
print(key)
Compare it with the QtCore.Qt.Key enum.将其与 QtCore.Qt.Key 枚举进行比较。 See here .
见这里。
if e.key() == QtCore.Qt.Key_F1:
Also, if you want the name of an arbitrary key press, you can use this:此外,如果您想要任意按键的名称,您可以使用以下命令:
from PyQt5.QtCore import Qt
keys = {}
for key, value in vars(Qt).items():
if isinstance(value, Qt.Key):
keys[value] = key
keys[16777264]
>>> 'Key_F1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.