如果我添加此功能:
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_()
一切正常,但这个KeyPressevent只有在我有一个窗口时才有效,但我不需要一个窗口,我希望没有它的情况下处理键盘事件。我尝试这样做:
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)
这段代码不起作用,我该怎么办? 另一个问题是: 如果我像这样写我的keyEvent func
def keyPressEvent(self, event):
if type(event) == QKeyEvent:
if event.key() == Qt.Key_0 or Qt.Key_1:
print('Key was pressed')
print()会打印出我按下的任何键,但是如果我将if语句更改为event.key()== Qt.Key_0 print()将只打印Key_0按钮。 为什么会这样?
答案 0 :(得分:0)
在您的工作代码keyPressEvent
中,MainWindow
是QMainWindow
的成员,而Qt
则继承自MainWindow
。由于继承,keyPressEvent
框架知道如何将事件分派给Frame
的实例,因此QWidget
将按预期调用。
失败的代码Frame
不会以任何方式从Qt
(或其他任何事情)继承。因此,if event.key() == Qt.Key_0 or Qt.Key_1:
的任何实例基本上都独立于if event.key() == Qt.Key_0 or event.key() == Qt.Key_1:
,因此不会向其发布任何事件。
关于你的第二个问题......
如果我将if语句更改为event.key()== Qt.Key_0 print()将 仅打印Key_0按钮。为什么会这样?
而不是......
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<ContentTemplate>
<asp:CheckBox ID="chk_select" AutoPostBack="true" runat="server" OnCheckedChanged="chk_select_CheckedChanged"/>
</ContentTemplate>
<Triggers>
<asp:Asyncpostbacktrigger controlid="chk_select" eventname="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
你可能想要......
var LS = document.getElementById("left");
for ( var number = 5; number > 0; number--)
{
LS.appendChild(document.createElement("img"));
}
如果您想要的是使用键盘输入而不是完整的GUI的控制台应用程序,您可能需要查看python curses。