def keyPressEvent(self, event):
if not self.isStarted or self.isPaused or self.curPiece.shape() == NoShape:
super(TetrixBoard, self).keyPressEvent(event)
return
key = event.key()
if key == Qt.Key_Left:
self.tryMove(self.curPiece, self.curX - 1, self.curY)
elif key == Qt.Key_Right:
self.tryMove(self.curPiece, self.curX + 1, self.curY)
elif key == Qt.Key_Down:
self.tryMove(self.curPiece.rotatedRight(), self.curX, self.curY)
elif key == Qt.Key_Up:
self.tryMove(self.curPiece.rotatedLeft(), self.curX, self.curY)
elif key == Qt.Key_Space:
self.dropDown()
elif key == Qt.Key_D:
self.oneLineDown()
else:
super(TetrixBoard, self).keyPressEvent(event)
python类Key_Left()的实例源码
def keyPressEvent(self, event):
if not self.isStarted or self.isPaused or self.curPiece.shape() == NoShape:
super(TetrixBoard, self).keyPressEvent(event)
return
key = event.key()
if key == Qt.Key_Left:
self.tryMove(self.curPiece, self.curX - 1, self.curY)
elif key == Qt.Key_Right:
self.tryMove(self.curPiece, self.curX + 1, self.curY)
elif key == Qt.Key_Down:
self.tryMove(self.curPiece.rotatedRight(), self.curX, self.curY)
elif key == Qt.Key_Up:
self.tryMove(self.curPiece.rotatedLeft(), self.curX, self.curY)
elif key == Qt.Key_Space:
self.dropDown()
elif key == Qt.Key_D:
self.oneLineDown()
else:
super(TetrixBoard, self).keyPressEvent(event)
def keyPressEvent(self, event):
if not self.isStarted or self.isPaused or self.curPiece.shape() == NoShape:
super(TetrixBoard, self).keyPressEvent(event)
return
key = event.key()
if key == Qt.Key_Left:
self.tryMove(self.curPiece, self.curX - 1, self.curY)
elif key == Qt.Key_Right:
self.tryMove(self.curPiece, self.curX + 1, self.curY)
elif key == Qt.Key_Down:
self.tryMove(self.curPiece.rotatedRight(), self.curX, self.curY)
elif key == Qt.Key_Up:
self.tryMove(self.curPiece.rotatedLeft(), self.curX, self.curY)
elif key == Qt.Key_Space:
self.dropDown()
elif key == Qt.Key_D:
self.oneLineDown()
else:
super(TetrixBoard, self).keyPressEvent(event)
def set_up_keyboard_keys(self):
row_1 = [Qt.Key_Q, Qt.Key_W, Qt.Key_E, Qt.Key_R, Qt.Key_T, Qt.Key_Y,
Qt.Key_Y, Qt.Key_I, Qt.Key_O, Qt.Key_P, Qt.Key_Backspace]
row_2 = [Qt.Key_A, Qt.Key_S, Qt.Key_D, Qt.Key_F, Qt.Key_G, Qt.Key_H,
Qt.Key_J, Qt.Key_K, Qt.Key_L, Qt.Key_Ccedilla, Qt.Key_Return]
row_3 = [Qt.Key_Aring, Qt.Key_Z, Qt.Key_X, Qt.Key_C, Qt.Key_V,
Qt.Key_B, Qt.Key_N, Qt.Key_M, Qt.Key_Comma, Qt.Key_Period,
Qt.Key_Question, Qt.Key_Aring]
row_4 = [Qt.Key_Aring, Qt.Key_Space,
Qt.Key_Left, Qt.Key_Right, Qt.Key_Aring]
self.keyboard_keys = [row_1, row_2, row_3, row_4]
def keyPressEvent(self, event):
key = event.key()
modifiers = event.modifiers()
if key in (Qt.Key_Up, Qt.Key_Down, Qt.Key_Left, Qt.Key_Right):
self._arrowKeyPressEvent(event)
elif key == Qt.Key_Return:
index = self._lastSelectedCell
if index is not None:
self.glyphActivated.emit(self._glyphs[index])
elif modifiers in (Qt.NoModifier, Qt.ShiftModifier):
self._glyphNameInputEvent(event)
else:
super(GlyphCellWidget, self).keyPressEvent(event)
def _arrowKeyPressEvent(self, event):
count = event.count()
key = event.key()
modifiers = event.modifiers()
# TODO: it might be the case that self._lastSelectedCell cannot be None
# when we arrive here whatsoever
if self._lastSelectedCell is not None:
if key == Qt.Key_Up:
delta = -self._columnCount
elif key == Qt.Key_Down:
delta = self._columnCount
elif key == Qt.Key_Left:
delta = -1
elif key == Qt.Key_Right:
delta = 1
newSel = self._lastSelectedCell + delta * count
if newSel < 0 or newSel >= len(self._glyphs):
return
if modifiers & Qt.ShiftModifier:
self._selection |= self._linearSelection(newSel)
else:
self._selection = {newSel}
self._lastSelectedCell = newSel
self.scrollToCell(newSel)
self.selectionChanged.emit()
self.update()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape and self.isFullScreen():
self.showNormal()
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_Q:
self.close()
sys.exit()
if event.key() == Qt.Key_Space:
if self.player.player.state() == self.player.player.PlayingState:
self.player.pause()
elif self.player.player.state() == self.player.player.StoppedState or self.player.player.PausedState:
self.player.play()
if event.key() == Qt.Key_Plus:
self.player.setVolume(self.player.volume() + 5)
if event.key() == Qt.Key_Minus:
self.player.setVolume(self.player.volume() - 5)
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_M:
self.player.setMuted(not self.player.isMuted())
if event.key() == Qt.Key_Right:
self.player.player.setPosition(self.player.player.position() + 10000)
if event.key() == Qt.Key_Left:
self.player.player.setPosition(self.player.player.position() - 10000)
if event.key() == Qt.Key_Up:
self.player.player.setPosition(self.player.player.position() + 60000)
if event.key() == Qt.Key_Down:
self.player.player.setPosition(self.player.player.position() - 60000)
super().keyPressEvent(event)
def keyPressEvent(self, event):
"""Keyboard press event
Effective key: W, A, S, D, ?, ?, ?, ?
Press a key on keyboard, the function will get an event, if the condition is met, call the function
run_action().
Args:
event, this argument will get when an event of keyboard pressed occured
"""
key_press = event.key()
# don't need autorepeat, while haven't released, just run once
if not event.isAutoRepeat():
if key_press == Qt.Key_Up: # up
run_action('camup')
elif key_press == Qt.Key_Right: # right
run_action('camright')
elif key_press == Qt.Key_Down: # down
run_action('camdown')
elif key_press == Qt.Key_Left: # left
run_action('camleft')
elif key_press == Qt.Key_W: # W
run_action('forward')
elif key_press == Qt.Key_A: # A
run_action('fwleft')
elif key_press == Qt.Key_S: # S
run_action('backward')
elif key_press == Qt.Key_D: # D
run_action('fwright')
def keyReleaseEvent(self, event):
"""Keyboard released event
Effective key: W,A,S,D, ?, ?, ?, ?
Release a key on keyboard, the function will get an event, if the condition is met, call the function
run_action().
Args:
event, this argument will get when an event of keyboard release occured
"""
# don't need autorepeat, while haven't pressed, just run once
key_release = event.key()
if not event.isAutoRepeat():
if key_release == Qt.Key_Up: # up
run_action('camready')
elif key_release == Qt.Key_Right: # right
run_action('camready')
elif key_release == Qt.Key_Down: # down
run_action('camready')
elif key_release == Qt.Key_Left: # left
run_action('camready')
elif key_release == Qt.Key_W: # W
run_action('stop')
elif key_release == Qt.Key_A: # A
run_action('fwstraight')
elif key_release == Qt.Key_S: # S
run_action('stop')
elif key_release == Qt.Key_D: # D
run_action('fwstraight')
def keyPressEvent(self, event):
"""Keyboard press event
Press a key on keyboard, the function will get an event, if the condition is met, call the function
run_action().
In camera calibration mode, Effective key: W,A,S,D, ?, ?, ?, ?, ESC
In front wheel calibration mode, Effective key: A, D, ?, ?, ESC
In back wheel calibration mode, Effective key: A, D, ?, ?, ESC
Args:
event, this argument will get when an event of keyboard pressed occured
"""
key_press = event.key()
if key_press in (Qt.Key_Up, Qt.Key_W): # UP
if self.calibration_status == 1:
cali_action('camcaliup')
elif self.calibration_status == 2:
pass
elif self.calibration_status == 3:
pass
elif key_press in (Qt.Key_Right, Qt.Key_D): # RIGHT
if self.calibration_status == 1:
cali_action('camcaliright')
elif self.calibration_status == 2:
cali_action('fwcaliright')
elif self.calibration_status == 3:
cali_action('bwcaliright')
elif key_press in (Qt.Key_Down, Qt.Key_S): # DOWN
if self.calibration_status == 1:
cali_action('camcalidown')
elif self.calibration_status == 2:
pass
elif self.calibration_status == 3:
pass
elif key_press in (Qt.Key_Left, Qt.Key_A): # LEFT
if self.calibration_status == 1:
cali_action('camcalileft')
elif self.calibration_status == 2:
cali_action('fwcalileft')
elif self.calibration_status == 3:
cali_action('bwcalileft')
cali_action('forward')
elif key_press == Qt.Key_Escape: # ESC
run_action('stop')
self.close()