PDA

View Full Version : how to catch key combination inside keyPressEvent.



babu198649
13th May 2008, 10:01
hi
i want to catch a combination of keypresses inside keyPressEvent such as shift+K;
normally single key presses can be caught inside keyPressEvent using case statements .

how to catch combination of keypresses inside keyPressEvent.

jpn
13th May 2008, 10:12
See QKeyEvent docs.

Hint: Shift is so called modifier.

babu198649
13th May 2008, 10:30
i tried like the below and it did not worked. how to use the modifier inside keypressevent.


void GLWidget::keyPressEvent(QKeyEvent * event) {
switch (event->key()) {
case (Qt::Key_1):
if (event->modifiers()==Qt::ShiftModifier)
qDebug()<<"shift and one";
break;
}
}

jpn
13th May 2008, 10:43
That's the way keyboards work. When you press Shift+1, at least on western keyboards it produces an exclamation mark ("!"), not one ("1") right? There is Qt::Key_Exclam for that. You might be able to get Shift+1 when you use numpad.

babu198649
13th May 2008, 10:48
i want to enter in to a case(switch-case) when shift and one (Shift+1) is pressed .how do i do it inside keyPressEvent.

jpn
13th May 2008, 11:19
As I said, you can do it with the numpad only. And you will have to change the if-statement because Qt::KeypadModifier will be active as well:


if (event->modifiers() & Qt::ShiftModifier)
...

babu198649
13th May 2008, 13:19
thanks jpn it works,
is there any advantage in using bitwise AND operator in preference to == operator(although both works). How does the bitwise AND operator works here(ie. logic ) .