Python源码示例:PyQt5.QtCore.Qt.Key_Right()
示例1
def keyPressEvent(self, event):
if not self.isStarted or BOARD_DATA.currentShape == Shape.shapeNone:
super(Tetris, self).keyPressEvent(event)
return
key = event.key()
if key == Qt.Key_P:
self.pause()
return
if self.isPaused:
return
elif key == Qt.Key_Left:
BOARD_DATA.moveLeft()
elif key == Qt.Key_Right:
BOARD_DATA.moveRight()
elif key == Qt.Key_Up:
BOARD_DATA.rotateLeft()
elif key == Qt.Key_Space:
self.tboard.score += BOARD_DATA.dropDown()
else:
super(Tetris, self).keyPressEvent(event)
self.updateWindow()
示例2
def keyPressEvent(self, event: QKeyEvent) -> None:
if self.isReadOnly():
return
block: QTextBlock = self.textCursor().block()
if event.key() in [Qt.Key_Down, Qt.Key_Up, Qt.Key_Left, Qt.Key_Right]:
return
if not event.key() == 16777220:
super().keyPressEvent(event)
if not isinstance(self.ignoreLength, bool):
textToWrite: str = block.text()[self.ignoreLength:]
# TODO: Handle multiline input!!!
if event.key() == 16777220:
self.userInputSignal.emit(textToWrite)
return
示例3
def init_action(self):
zoom_minus = QShortcut(QKeySequence("Ctrl+-"), self)
zoom_minus.activated.connect(self.minus)
zoom_plus = QShortcut(QKeySequence("Ctrl+="), self)
zoom_plus.activated.connect(self.plus)
switch_left = QShortcut(QKeySequence(Qt.Key_Left), self)
switch_left.activated.connect(self.left)
switch_right = QShortcut(QKeySequence(Qt.Key_Right), self)
switch_right.activated.connect(self.right)
'''
a1 = QAction(self)
a1.setShortcut('Ctrl++')
self.addAction(a1)
a1.triggered.connect(self.plus)
a2 = QAction(self)
a2.setShortcut('Ctrl+-')
self.addAction(a2)
a2.triggered.connect(self.minus)
'''
示例4
def left(self):
self.widget.switch_page(right=False)
#def keyPressEvent(self, event):
#这里event.key()显示的是按键的编码
#print("按下:" + str(event.key()))
# 举例,这里Qt.Key_A注意虽然字母大写,但按键事件对大小写不敏感
#if (event.key() == Qt.Key_Left):
# self.widget.switch_page(right=False)
#elif (event.key() == Qt.Key_Right):
# self.widget.switch_page(right=True)
#elif (event.modifiers() == Qt.CTRL and event.key() == Qt.Key_Plus):
# self.widget.zoom_book(plus=True)
#elif (event.modifiers() == Qt.CTRL and event.key() == Qt.Key_Minus):
# self.widget.zoom_book(plus=False)
示例5
def right(self, count=1):
self._key_press(Qt.Key_Right, count, 'scrollBarMaximum', Qt.Horizontal)
示例6
def keyPressEvent(self, event):
if not self.is_started or self.inner_board.current_tetris == tetrisShape().shape_empty:
super(TetrisGame, self).keyPressEvent(event)
return
key = event.key()
# P键暂停
if key == Qt.Key_P:
self.pause()
return
if self.is_paused:
return
# 向左
elif key == Qt.Key_Left:
self.inner_board.moveLeft()
# 向右
elif key == Qt.Key_Right:
self.inner_board.moveRight()
# 旋转
elif key == Qt.Key_Up:
self.inner_board.rotateAnticlockwise()
# 快速坠落
elif key == Qt.Key_Space:
self.external_board.score += self.inner_board.dropDown()
else:
super(TetrisGame, self).keyPressEvent(event)
self.updateWindow()
示例7
def _qtKeyToUMKey(self, key):
if key == Qt.Key_Shift:
return KeyEvent.ShiftKey
elif key == Qt.Key_Control:
return KeyEvent.ControlKey
elif key == Qt.Key_Alt:
return KeyEvent.AltKey
elif key == Qt.Key_Space:
return KeyEvent.SpaceKey
elif key == Qt.Key_Meta:
return KeyEvent.MetaKey
elif key == Qt.Key_Enter or key == Qt.Key_Return:
return KeyEvent.EnterKey
elif key == Qt.Key_Up:
return KeyEvent.UpKey
elif key == Qt.Key_Down:
return KeyEvent.DownKey
elif key == Qt.Key_Left:
return KeyEvent.LeftKey
elif key == Qt.Key_Right:
return KeyEvent.RightKey
elif key == Qt.Key_Minus:
return KeyEvent.MinusKey
elif key == Qt.Key_Underscore:
return KeyEvent.UnderscoreKey
elif key == Qt.Key_Plus:
return KeyEvent.PlusKey
elif key == Qt.Key_Equal:
return KeyEvent.EqualKey
return key
示例8
def keyPressEvent(self, event: QtGui.QKeyEvent) -> None:
key_press = event.key()
# if key_press == Qt.Key_Up:
# self.snake.direction = 'u'
# elif key_press == Qt.Key_Down:
# self.snake.direction = 'd'
# elif key_press == Qt.Key_Right:
# self.snake.direction = 'r'
# elif key_press == Qt.Key_Left:
# self.snake.direction = 'l'
示例9
def keyPressEvent(self, event):
#HOT KEYS
key = event.key()
# Duplicate the frame step size (speed) when pressed > or .:
if key == Qt.Key_Greater or key == Qt.Key_Period:
self.frame_step *= 2
self.ui.spinBox_step.setValue(self.frame_step)
# Half the frame step size (speed) when pressed: < or ,
elif key == Qt.Key_Less or key == Qt.Key_Comma:
self.frame_step //= 2
if self.frame_step < 1:
self.frame_step = 1
self.ui.spinBox_step.setValue(self.frame_step)
# Move backwards when are pressed
elif key == Qt.Key_Left:
self.frame_number -= self.frame_step
if self.frame_number < 0:
self.frame_number = 0
self.ui.spinBox_frame.setValue(self.frame_number)
# Move forward when are pressed
elif key == Qt.Key_Right:
self.frame_number += self.frame_step
if self.frame_number >= self.tot_frames:
self.frame_number = self.tot_frames - 1
self.ui.spinBox_frame.setValue(self.frame_number)
#super().keyPressEvent(event)
示例10
def keyPressEvent(self, event):
if event.modifiers() & Qt.ShiftModifier \
and event.key() in [Qt.Key_Up, Qt.Key_Down, Qt.Key_Left, Qt.Key_Right]:
dlat = 1.0 / (self.radarwidget.zoom * self.radarwidget.ar)
dlon = 1.0 / (self.radarwidget.zoom * self.radarwidget.flat_earth)
if event.key() == Qt.Key_Up:
self.radarwidget.panzoom(pan=(dlat, 0.0))
elif event.key() == Qt.Key_Down:
self.radarwidget.panzoom(pan=(-dlat, 0.0))
elif event.key() == Qt.Key_Left:
self.radarwidget.panzoom(pan=(0.0, -dlon))
elif event.key() == Qt.Key_Right:
self.radarwidget.panzoom(pan=(0.0, dlon))
elif event.key() == Qt.Key_Escape:
self.closeEvent()
elif event.key() == Qt.Key_F11: # F11 = Toggle Full Screen mode
if not self.isFullScreen():
self.showFullScreen()
else:
self.showNormal()
else:
# All other events go to the BlueSky console
self.console.keyPressEvent(event)
event.accept()
示例11
def textChanged(self, e): # QKeyEvent(QEvent.KeyPress, Qt.Key_Enter, Qt.NoModifier)
# if (32<e.key()<96 or 123<e.key()<126 or 0x1000001<e.key()<0x1000005 or e.key==Qt.Key_Delete):
# 大键盘为Ret小键盘为Enter
if (e.key() in (Qt.Key_Return, Qt.Key_Enter, Qt.Key_Semicolon, Qt.Key_BraceRight, Qt.Key_Up, Qt.Key_Down,
Qt.Key_Left, Qt.Key_Right, Qt.Key_Tab, Qt.Key_Delete, Qt.Key_Backspace)):
self.renderStyle()
self.loadColorPanel()
self.actions["undo"].setEnabled(self.editor.isUndoAvailable())
self.actions["redo"].setEnabled(self.editor.isRedoAvailable())
示例12
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')
示例13
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')
示例14
def __init__(self, parent=None, plugins=None, fullscreen=False):
super(DeenGui, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.plugins = plugins
self.widgets = []
self.ui.actionLoad_from_file.triggered.connect(self.load_from_file)
self.ui.actionQuit.triggered.connect(QApplication.quit)
self.ui.actionAbout.triggered.connect(self.show_about)
self.ui.actionStatus_console.triggered.connect(self.show_status_console)
self.ui.actionTop_to_bottom.triggered.connect(self.set_widget_direction_toptobottom)
self.ui.actionLeft_to_right.triggered.connect(self.set_widget_direction_lefttoright)
# Set default direction
self.set_widget_direction_toptobottom()
self.ui.actionCopy_to_clipboard.triggered.connect(self.copy_content_to_clipboard)
self.ui.actionSave_content_to_file.triggered.connect(self.save_widget_content_to_file)
self.ui.actionSearch.triggered.connect(self.toggle_search_box_visibility)
self.widgets.append(DeenEncoderWidget(self))
for widget in self.widgets:
self.ui.encoder_widget_layout.addWidget(widget)
self.load_from_file_dialog = QFileDialog(self)
self.setWindowTitle('deen')
self.log = DeenLogger(self)
self.widgets[0].set_field_focus()
# Add action fuzzy search
self.fuzzy_search_ui = FuzzySearchUi(self)
self.fuzzy_search_action_shortcut = QShortcut(QKeySequence(Qt.CTRL | Qt.Key_R), self)
self.fuzzy_search_action_shortcut.activated.connect(self.fuzzy_search_action)
self.clear_current_widget_shortcut = QShortcut(QKeySequence(Qt.CTRL | Qt.Key_Q), self)
self.clear_current_widget_shortcut.activated.connect(self.clear_current_widget)
self.hide_search_box_shortcut = QShortcut(QKeySequence(Qt.CTRL | Qt.Key_F), self)
self.hide_search_box_shortcut.activated.connect(self.toggle_search_box_visibility)
self.next_encoder_widget_shortcut = QShortcut(QKeySequence(Qt.ALT | Qt.Key_Right), self)
self.next_encoder_widget_shortcut.activated.connect(self.toggle_next_encoder_focus)
self.prev_encoder_widget_shortcut = QShortcut(QKeySequence(Qt.ALT | Qt.Key_Left), self)
self.prev_encoder_widget_shortcut.activated.connect(self.toggle_prev_encoder_focus)
if fullscreen:
self.showMaximized()
self.show()
示例15
def test_MicroPythonREPLPane_keyPressEvent_right(qtapp):
"""
Ensure right arrows in the REPL are handled correctly.
"""
mock_serial = mock.MagicMock()
rp = mu.interface.panes.MicroPythonREPLPane(mock_serial)
data = mock.MagicMock
data.key = mock.MagicMock(return_value=Qt.Key_Right)
data.text = mock.MagicMock(return_value="1b")
data.modifiers = mock.MagicMock(return_value=None)
rp.keyPressEvent(data)
mock_serial.write.assert_called_once_with(b"\x1B[C")
示例16
def test_PythonProcessPane_parse_input_right_arrow(qtapp):
"""
Right Arrow causes the cursor to move to the right one place.
"""
ppp = mu.interface.panes.PythonProcessPane()
mock_cursor = mock.MagicMock()
ppp.textCursor = mock.MagicMock(return_value=mock_cursor)
ppp.setTextCursor = mock.MagicMock()
key = Qt.Key_Right
text = ""
modifiers = None
ppp.parse_input(key, text, modifiers)
mock_cursor.movePosition.assert_called_once_with(QTextCursor.Right)
ppp.setTextCursor.assert_called_once_with(mock_cursor)
示例17
def keyPressEvent(self, event):
''' Handle keyboard input for bluesky. '''
# Enter-key: enter command
if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return:
if self.command_line:
# emit a signal with the command for the simulation thread
self.stack(self.command_line)
# Clear any shape command preview on the radar display
# self.radarwidget.previewpoly(None)
return
newcmd = self.command_line
cursorpos = None
if event.key() >= Qt.Key_Space and event.key() <= Qt.Key_AsciiTilde:
pos = self.lineEdit.cursor_pos()
newcmd = newcmd[:pos] + event.text() + newcmd[pos:]
# Update the cursor position with the length of the added text
cursorpos = pos + len(event.text())
elif event.key() == Qt.Key_Backspace:
pos = self.lineEdit.cursor_pos()
newcmd = newcmd[:pos - 1] + newcmd[pos:]
cursorpos = pos - 1
elif event.key() == Qt.Key_Tab:
if newcmd:
newcmd, displaytext = autocomplete.complete(newcmd)
if displaytext:
self.echo(displaytext)
elif not event.modifiers() & (Qt.ControlModifier | Qt.ShiftModifier |
Qt.AltModifier | Qt.MetaModifier):
if event.key() == Qt.Key_Up:
if self.history_pos == 0:
self.command_mem = newcmd
if len(self.command_history) >= self.history_pos + 1:
self.history_pos += 1
newcmd = self.command_history[-self.history_pos]
elif event.key() == Qt.Key_Down:
if self.history_pos > 0:
self.history_pos -= 1
if self.history_pos == 0:
newcmd = self.command_mem
else:
newcmd = self.command_history[-self.history_pos]
elif event.key() == Qt.Key_Left:
self.lineEdit.cursor_left()
elif event.key() == Qt.Key_Right:
self.lineEdit.cursor_right()
else:
# Remaining keys are things like sole modifier keys, and function keys
super(Console, self).keyPressEvent(event)
else:
event.ignore()
return
# Final processing of the command line
self.set_cmdline(newcmd, cursorpos)
示例18
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()
示例19
def keyPressEvent(self, event):
print(event.key())
if event.key() == Qt.Key_M:
self.send_flag = False
self.com.write(b"m")
self.send_flag = False
elif event.key() == Qt.Key_Return or event.key()==Qt.Key_Enter:
self.send_flag = False
self.com.write(b"m")
self.send_flag = False
elif event.key() == Qt.Key_N or event.key() == 92:
self.send_flag = False
self.com.write(b"n")
self.send_flag = False
elif event.key() == Qt.Key_Minus:
self.send_flag = False
self.com.write(b"-")
self.send_flag = False
elif event.key() == Qt.Key_Equal:
self.send_flag = False
self.com.write(b"=")
self.send_flag = False
elif event.key() == Qt.Key_W or event.key() == Qt.Key_Up:
self.send_flag = True
self.key.append(b"w")
elif event.key() == Qt.Key_A or event.key() == Qt.Key_Left:
self.send_flag = True
self.key.append(b"a")
elif event.key() == Qt.Key_S or event.key() == Qt.Key_Down:
self.send_flag = True
self.key.append(b"s")
elif event.key() == Qt.Key_D or event.key() == Qt.Key_Right:
self.send_flag = True
self.key.append(b"d")
elif event.key() == Qt.Key_J:
self.send_flag = True
self.key.append(b"j")
elif event.key() == Qt.Key_K:
self.send_flag = True
self.key.append(b"k")
elif event.key() == Qt.Key_Escape:
self.send_flag = False
self.com.write(b"\x03")
elif event.key() == Qt.Key_Control:
self.keyControlPressed = True
elif event.key() == Qt.Key_C:
if self.keyControlPressed:
self.send_flag = False
self.com.write(b"\x03")
# self.key_label.setText(self.key.decode())