我刚买了一台Compucessory 2口USB数字键盘(型号:34222)。
在Linux下,使用Python 2和PySide,无论NumLock设置如何,它都能正常工作。 (它生成不同的值,但每个按键只生成一个值。)
在Mac OS X(Sierra)下,当NumLock关闭时,一切都很顺利。当它打开时,每个按键都会生成正确的值(例如49),但接着是16777227,这是NumLock打开时“5”键的值。
这是问题的缩减版本:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class QGV(QGraphicsView):
def __init__(self, parent=None):
super(QGV, self).__init__(parent)
self.scene = QGraphicsScene(0, 0, 200, 200, self)
self.setScene(self.scene)
def keyPressEvent(self, event):
deg = {55: 225, 56: 270, 57: 315, # 7 8 9
52: 180, 54: 0, # 4 (5) 6
49: 135, 50: 90, 51: 45, # 1 2 3
16777232: 225, 16777235: 270, 16777238: 315, # 7 8 9
16777234: 180, 16777236: 0, # 4 (5) 6
16777233: 135, 16777237: 90, 16777239: 45} # 1 2 3
if event.isAutoRepeat():
return
k = event.key()
if k == Qt.Key_Escape:
QApplication.closeAllWindows()
if k in deg:
print("Set: {0:3d} degrees ({1:8d})".format(deg[k], k))
elif k in (53, 16777227):
print("Reset: ({0:8d})".format(k))
else:
print("Ignored: ({0:8d})".format(k))
super(QGV, self).keyPressEvent(event)
def main():
import sys
app = QApplication(sys.argv)
qgv = QGV()
qgv.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()