如何使用 keyPressEvent() 处理键盘快捷键?它应该用于它吗?
How to handle keyboard shortcuts using keyPressEvent()? And should it be used for it?
在 QTableView
的 keyPressEvent()
方法中定义自定义信号发射时:
def keyPressEvent(self, e):
if e.text()=='w':
self.emit(SIGNAL('writeRequested'))
if e.text()=='o':
self.emit(SIGNAL('openRequested'))
我正在使用传入的 e
参数来确定按下了哪个键盘键。有了这个 "technique" 我一次只能使用一个字符。其次,我无法使用 Ctrl+Key
、Alt+Key
或 Shift+Key
的组合。第三,我不知道 Delete
和 Backspaces
键是什么,所以我可以将它们与 e.text()
进行比较。
所以问题真的很少...
在 Qt 文档中列出了所有键盘键,因此它们可用于进行 e.text()=='keyboardKey'
比较。
如何使用我正在使用的 "technique" 处理双键盘组合键(例如 Ctrl+Key
)(从视图内部发送自定义信号 keyPressEvent()
?
是否有更简单的方法来挂钩键盘键以触发方法或函数(这样用户可以在鼠标位于 QTableView
上方时使用键盘快捷键来启动 "action")?
如果你看keyPressEvent()
you will see that the e
argument you describe in your question is of type QKeyEvent
的签名。
QKeyEvent
个实例有一个方法 key()
which returns an integer that can be matched against constants in the enum Qt.Key
.
例如:
if e.key() == Qt.Key_Backspace:
print 'The backspace key was pressed'
类似地,QKeyEvent
有一个方法 modifiers()
. Because there can be multiple keyboard modifiers pressed at once, you need to use this a little differently. The result from modifiers()
is the binary OR of one or more of the constants in the Qt.KeyboardModifier
枚举。要测试是否按下了给定的修饰符,您需要执行二进制 AND。例如:
if e.modifiers() & Qt.ShiftModifier:
print 'The Shift key is pressed'
if e.modifiers() & Qt.ControlModifier:
print 'The control key is pressed'
if e.modifiers() & Qt.ShiftModifier and e.modifiers() & Qt.ControlModifier:
print 'Ctrl+Shift was pressed'
注意:在上面的示例中,如果同时按下 ctrl+shift,则所有三个 if 语句将按顺序执行。
只是为了完整性,如果你想要更困难的序列(例如 ctrl-c 后跟 ctrl-k)只需使用 QKeySequence as shortcut of a QAction which can be added to any QWidget.
在 QTableView
的 keyPressEvent()
方法中定义自定义信号发射时:
def keyPressEvent(self, e):
if e.text()=='w':
self.emit(SIGNAL('writeRequested'))
if e.text()=='o':
self.emit(SIGNAL('openRequested'))
我正在使用传入的 e
参数来确定按下了哪个键盘键。有了这个 "technique" 我一次只能使用一个字符。其次,我无法使用 Ctrl+Key
、Alt+Key
或 Shift+Key
的组合。第三,我不知道 Delete
和 Backspaces
键是什么,所以我可以将它们与 e.text()
进行比较。
所以问题真的很少...
在 Qt 文档中列出了所有键盘键,因此它们可用于进行
e.text()=='keyboardKey'
比较。如何使用我正在使用的 "technique" 处理双键盘组合键(例如
Ctrl+Key
)(从视图内部发送自定义信号keyPressEvent()
?是否有更简单的方法来挂钩键盘键以触发方法或函数(这样用户可以在鼠标位于
QTableView
上方时使用键盘快捷键来启动 "action")?
如果你看keyPressEvent()
you will see that the e
argument you describe in your question is of type QKeyEvent
的签名。
QKeyEvent
个实例有一个方法 key()
which returns an integer that can be matched against constants in the enum Qt.Key
.
例如:
if e.key() == Qt.Key_Backspace:
print 'The backspace key was pressed'
类似地,QKeyEvent
有一个方法 modifiers()
. Because there can be multiple keyboard modifiers pressed at once, you need to use this a little differently. The result from modifiers()
is the binary OR of one or more of the constants in the Qt.KeyboardModifier
枚举。要测试是否按下了给定的修饰符,您需要执行二进制 AND。例如:
if e.modifiers() & Qt.ShiftModifier:
print 'The Shift key is pressed'
if e.modifiers() & Qt.ControlModifier:
print 'The control key is pressed'
if e.modifiers() & Qt.ShiftModifier and e.modifiers() & Qt.ControlModifier:
print 'Ctrl+Shift was pressed'
注意:在上面的示例中,如果同时按下 ctrl+shift,则所有三个 if 语句将按顺序执行。
只是为了完整性,如果你想要更困难的序列(例如 ctrl-c 后跟 ctrl-k)只需使用 QKeySequence as shortcut of a QAction which can be added to any QWidget.