Answer the question
In order to leave comments, you need to log in
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')
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_()
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)
def keyPressEvent(self, event):
if type(event) == QKeyEvent:
if event.key() == Qt.Key_0 or Qt.Key_1:
print('Key was pressed')
Answer the question
In order to leave comments, you need to log in
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:
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 questionAsk a Question
731 491 924 answers to any question