如何使用 keyPressEvent() 处理键盘快捷键?它应该用于它吗? [英] How to handle keyboard shortcuts using keyPressEvent()? And should it be used for it?

查看:255
本文介绍了如何使用 keyPressEvent() 处理键盘快捷键?它应该用于它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QTableViewkeyPressEvent() 方法中定义自定义信号发射时:

While defining the custom signal emission from within QTableView's keyPressEvent() method:

def keyPressEvent(self, e):
    if e.text()=='w':
        self.emit(SIGNAL('writeRequested'))
    if e.text()=='o':
        self.emit(SIGNAL('openRequested')) 

我正在使用传入的 e 参数来确定按下了什么键盘键.使用这种技术",我当时只能使用一个字符.其次,我无法使用 Ctrl+KeyAlt+KeyShift+Key 的组合.第三,我无法弄清楚 DeleteBackspaces 键是什么,所以我可以将它们与 e.text() 进行比较.

I am using incoming e argument to figure out what keyboard key was pressed. With this "technique" I am limited to only one character at the time. Secondly, I am not able to use a combination of Ctrl+Key, Alt+Key or Shift+Key. Third, I can't figure out what Delete and Backspaces keys are so I could compare them against e.text().

所以真的有几个问题......

So there are few questions really...

  1. Qt 文档中列出了所有键盘键的位置,以便它们可用于进行 e.text()=='keyboardKey' 比较.

如何使用我正在使用的技术"处理双键盘组合键(例如 Ctrl+Key)(从视图的 keyPressEvent 内部发送自定义信号()?

How to handle the double-keyboard keys combinations (such as Ctrl+Key) with the "technique" I am using (sending a custom signal from inside of view's keyPressEvent()?

是否有其他更简单的方法来挂钩键盘键以触发方法或功能(因此用户可以在鼠标位于 QTableView 上方时使用键盘快捷键启动动作")?

Is there an alternative easier way to hook the keyboard keys to trigger a method or a function (so the user could use keyboard shortcuts while mouse positioned above QTableView to launch the "action")?

推荐答案

如果看keyPressEvent() 你会看到你在问题中描述的 e 参数是 QKeyEvent.

If you look at the signature of keyPressEvent() you will see that the e argument you describe in your question is of type QKeyEvent.

QKeyEvent 实例有一个方法 key() 返回一个可以与枚举中的常量匹配的整数 Qt.Key.

QKeyEvent instances have a method 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() 的结果是 Qt.KeyboardModifier 枚举.要测试是否按下了给定的修饰符,您需要执行二进制 AND.例如:

Similarly, QKeyEvent has a method 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 enum. To test if a given modifier is pressed, you need to perform the binary AND. For example:

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 语句都会依次执行.

Note: In the example above, if both ctrl+shift were pressed, then all three if statements execute, in sequence.

这篇关于如何使用 keyPressEvent() 处理键盘快捷键?它应该用于它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆