Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple key presses in single event?

Tags:

qt

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?

like image 468
SunnyShah Avatar asked Aug 24 '11 14:08

SunnyShah


3 Answers

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
like image 91
Paul Avatar answered Nov 05 '22 20:11

Paul


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();
}
like image 42
SunnyShah Avatar answered Nov 05 '22 20:11

SunnyShah


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.

like image 38
Vinicius Kamakura Avatar answered Nov 05 '22 18:11

Vinicius Kamakura