9

I am creating an application, where "Left Arrow + Down Arrow" press has different behavior ( It is not same as first left arrow and then left arrow ), currently in keyPressEvent event I am getting them one by one in two separate calls.

Is there any way by which I can get multiple keypress in one keyboard event?

SunnyShah
  • 28,934
  • 30
  • 90
  • 137

3 Answers3

12

Thanks for this. I am posting code for the Python (PyQt) equivalent so that someone else might find it useful.

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):
    print keyspressed
Paul
  • 2,409
  • 2
  • 26
  • 29
9

I solved the problem by below code.

QSet<Qt::Key> keysPressed;

void Widget::keyPressEvent(QKeyEvent * event) {
    m_bFirstRelease = true;
    keysPressed+= event->key();
}

void Widget::keyReleaseEvent(QKeyEvent *) {
    if(m_bFirstRelease) {
        processMultiKeys(keysPressed);
    }
    m_bFirstRelease = false;
    keysPressed-= event->key();
}
SunnyShah
  • 28,934
  • 30
  • 90
  • 137
1

Nothing is "at the same time" and I believe in Qt you can't have that type of behaviour (except for modifier keys like shift, alt, etc).

Approach the problem in a different way. When you receive one of the keys, check to see if you received the other in a short while back, say 20ms before.

Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43