B
B
bychok3002017-03-11 12:48:02
Python
bychok300, 2017-03-11 12:48:02

Why doesn't QKeyEvent work without a window?

If I add a function that catches pressing a button on the keyboard:

def keyPressEvent(self, event):
        if type(event) == QKeyEvent:
            if event.key() == Qt.Key_0 or Qt.Key_1:
                print('Key was pressed')

In this piece of code:
def distance(x1, y1, x2, y2):
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

class Frame:
    def __init__(self, position, time):
        self.position = position
        self.time = time

    def speed(self, frame):
        d = distance(*self.position, *frame.position)
        time_delta = abs(frame.time - self.time)
        return d / time_delta

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.last_frame = None
        self.setMouseTracking(True)

    def mouseMoveEvent(self, event):

        nowTime = datetime.now()
        mouseWasMoveAt = nowTime.strftime('%H:%M:%S')

        new_frame = Frame((event.x(), event.y()), time.time())

        if self.last_frame:
            print(new_frame.speed(self.last_frame), mouseWasMoveAt ) #print speed and time

        self.last_frame = new_frame

    def keyPressEvent(self, event):
        if type(event) == QKeyEvent:
            if event.key() == Qt.Key_0 or Qt.Key_1:
                print('Key was pressed')    

if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = MainWindow()
    w.resize(900,600)
    w.show()

    app.exec_()

Everything works well, everything I want is displayed, but if I want the same thing, but without creating a window, then nothing works, below is the code without a window:
class Frame:
    def __init__(self, position, time):
        self.position = position
        self.time = time


    def speed(self, frame):
        d = distance(*self.position, *frame.position)
        time_delta = abs(frame.time - self.time)
        return d / time_delta

    def keyPressEvent(self, event):
        if type(event) == QKeyEvent:
            if event.key() == Qt.Key_0 or Qt.Key_1:
                print('Key was pressed')

def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1)**2 + (y2-y1)**2)

def get_current_cursor_position():
    pos = QCursor.pos()
    return pos.x(), pos.y()

def get_current_frame():
    return Frame(get_current_cursor_position(), time.time())



if __name__ == '__main__':

    app = QApplication(sys.argv)

    last_frame = get_current_frame()


    while True:
        nowTime = datetime.now()
        mouseWasMoveAt = nowTime.strftime('%Y-%m-%d %H:%M:%S')

        new_frame = get_current_frame()

        if new_frame.speed(last_frame) != 0:

            print(mouseWasMoveAt)
            last_frame = new_frame
            time.sleep(0.07)

Here, this piece of code does not work, how can I fix it, maybe I did everything anyway? And the second question: if I write click tracking like this:
def keyPressEvent(self, event):
       if type(event) == QKeyEvent:
           if event.key() == Qt.Key_0 or Qt.Key_1:
               print('Key was pressed')

print() writes me whatever key I press, if I change if event.key() == Qt.Key_0 print() write only Key_0 . Why is that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andy_U, 2017-03-11
@Andy_U

Here is an obvious mistake:
It should be like this:
Well, or like this:

key = event.key()
if key == Qt.Key_0 or key == Qt.Key_1:

And with frame, at least in the form you wrote, nothing will work, because this class is not inherited from QmainWindow etc.

S
Sergey6661313, 2017-03-11
@Sergey6661313

Events in pyqt can only receive objects inherited from QObject. Because inside their code and the code in the qt libraries there is code that "sends" messages and calls the keyPressEvent method on the objects. You create a new Frame class and do not inherit any Qt functions for it. Therefore, the keyPressEvent function will never be called in it.
This is the answer to the question "why" ... but I can’t answer how to do it right on Qt ...
However, I can advise you to dig towards a third-party keyboard library , which, in principle, can do without Qt:

import keyboard

while True:
    print(keyboard.is_pressed("space"))   # printed True or False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question