Why does a USB Numeric Keypad generating two events under Mac OS X when NumLock is on?

84 Views Asked by At

I have just acquired a Compucessory 2-port USB numeric keypad (Model: 34222).

Under Linux, using Python 2 and PySide, it works correctly regardless of the NumLock setting. (It generates different values, but it only generates one value per keypress.)

Under Mac OS X (Sierra), when NumLock is off, all is well. When it is on, every keypress generates the correct value (e.g. 49) but then this is followed by 16777227 which is the value for the "5" key when the NumLock is on.

Here's a whittled-down version of the problem:

#!/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()
0

There are 0 best solutions below