Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a keyboard modifier is pressed (Shift, Ctrl, or Alt)?

Tags:

I am building a UI with Qt Creator and I want buttons to perform different actions with different modifiers. So I thought I could call functions with dynamic string properties that would perform the action depending on the modifier.

Is there a simpler way to do this?

like image 527
user1087058 Avatar asked Jan 07 '12 19:01

user1087058


People also ask

Where is the modifier key on a keyboard?

On an IBM compatible computer, modifier keys include Alt, Ctrl, Shift, and the Windows key. On the Apple Macintosh computer, the Control, Option, Command, and Shift keys are modifier keys. Additionally, most laptop and some desktop keyboards contain an Fn modifier key.

What is modifier key in C#?

Control, shift and alt keys are called modifier keys. If you want to execute the code in keydown or keyup event only when user press a key along with modifier keys then use modifiers property of second argument e that contains the modifier keys pressed along with the key pressed on the keyboard.


2 Answers

It looks like all you need to do is check the keyboardModifiers in your button handler, and select a different action as appropriate. The various modifiers can be OR'd together in order to check for multi-key combinations:

PyQt5:

import sys from PyQt5 import QtCore, QtWidgets  class Window(QtWidgets.QWidget):     def __init__(self):         super().__init__()         self.button = QtWidgets.QPushButton('Test')         self.button.clicked.connect(self.handleButton)         layout = QtWidgets.QVBoxLayout(self)         layout.addWidget(self.button)      def handleButton(self):         modifiers = QtWidgets.QApplication.keyboardModifiers()         if modifiers == QtCore.Qt.ShiftModifier:             print('Shift+Click')         elif modifiers == QtCore.Qt.ControlModifier:             print('Control+Click')         elif modifiers == (QtCore.Qt.ControlModifier |                            QtCore.Qt.ShiftModifier):             print('Control+Shift+Click')         else:             print('Click')  if __name__ == '__main__':      app = QtWidgets.QApplication(sys.argv)     window = Window()     window.show()     sys.exit(app.exec()) 

PyQt4:

import sys from PyQt4 import QtGui, QtCore  class Window(QtGui.QWidget):     def __init__(self):         super(Window, self).__init__()         self.button = QtGui.QPushButton('Test')         self.button.clicked.connect(self.handleButton)         layout = QtGui.QVBoxLayout(self)         layout.addWidget(self.button)      def handleButton(self):         modifiers = QtGui.QApplication.keyboardModifiers()         if modifiers == QtCore.Qt.ShiftModifier:             print('Shift+Click')         elif modifiers == QtCore.Qt.ControlModifier:             print('Control+Click')         elif modifiers == (QtCore.Qt.ControlModifier |                            QtCore.Qt.ShiftModifier):             print('Control+Shift+Click')         else:             print('Click')  if __name__ == '__main__':      app = QtGui.QApplication(sys.argv)     window = Window()     window.show()     sys.exit(app.exec_()) 
like image 66
ekhumoro Avatar answered Sep 24 '22 15:09

ekhumoro


I was trying to handle multiple keys pressed at the same time (e.g. A and W or W and D). The solution below works with multiple keys being pressed at the same time (including Ctrl, Shift, Alt, etc.).

def keyPressEvent(self, event):     self.firstrelease = True     astr = "pressed: " + str(event.key())     self.keylist.append(astr)  def keyReleaseEvent(self, event):     if self.firstrelease == True:         self.processmultikeys(self.keylist)      self.firstrelease = False     del self.keylist[-1]  def processmultikeys(self, keyspressed):     # Your logic here     print keyspressed 

Go here for the original discussion of this solution: How to get multiple key presses in single event?

like image 38
Paul Avatar answered Sep 23 '22 15:09

Paul