简体   繁体   English

如何从 keyPressEvent 中获取 F1 或 F2 等字母值?

[英]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.

相关问题 如何从函数列表 [f1, f2, f3,...fn] 组成嵌套的 function g=fn(...(f3(f2(f1()))...) - How to compose a nested function g=fn(…(f3(f2(f1()))…) from a list of functions [f1, f2, f3,…fn] 如何解决python中的表达式,例如f1(x) = f2(x)? - How do I solve expressions in python such as f1(x) = f2(x)? Python 如何使用(function F1 内部的变量) function 内部(F2 在 F1 内部) - Python how to use (variables inside function F1) inside function (F2 which is inside F1) 在给定时间/样本量下,频率f1和f2之间指数变化的正弦波 - sine wave that exponentialy changes between frequencies f1 and f2 at given time/amount of samples 使用pandas DataFrame比较两个file_csv以查找差异并将F2连接到F1 - Compare two file_csv using pandas DataFrame to find the difference and join F2 into F1 如何提高分类的 F1 分数 - How to improve F1 score for classification 如何在 Keras 模型中使用 F1 Score? - How to use F1 Score with Keras model? 如何从 Django 中的 F() / F() 获取浮点数 - How to get float from F() / F() in Django 为什么 NumPy 浮点类型从“f2”开始,而整数类型从“i1”开始? - Why do NumPy floating-point types start from "f2" but integer types from "i1"? 将SWIG应用于C ++时,f1(const char * str)是否与f1(char * str)不同? - Is f1(const char* str) different from f1(char* str) when applying SWIG to C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM