PDA

View Full Version : Getting real key in keyPressEvent



EricF
19th March 2008, 14:20
I want to develop a control that listen for keyPressEvent and displays the key combination entered. For example, if control and O is pressed, I want to display Ctrl + O.

I found a way to do this using a dummy QKeySequence to translate Qt::Key to string. However, when I hit Shift and ; I get "Shift + :" in the string returned. Is there a way when retrieving the key in key press event(or any other event) to not get the result of the key combination ?

jpn
19th March 2008, 22:26
Here's something to start with:


int key = event->key();
int modifiers = event->modifiers();
if (key >= Qt::Key_Shift && key <= Qt::Key_Alt)
key = 0;
qDebug() << QKeySequence(modifiers ? modifiers : key, modifiers ? key : 0).toString().remove(", ");

EricF
20th March 2008, 15:39
This will also display "Shift :" instead of "Shift ;". The thing is the key gets translated before I get it. I would understand the text() function to return ':' if shift was pressed with ';' but the key() function should return Qt::Key_SemiColon right ?