PDA

View Full Version : Problem detecting Ctrl Shortcut key



aguayro
1st August 2013, 01:32
I have an app with a keyboard Shortcut reader.
It works ok, detect Alt + Key, Shift + Key, etc. but when i try to detect Ctr + Key, Key is not a letter, is a strange character.
Why is this happening?


Here is the code:


void rs_GetKey::keyPressEvent(QKeyEvent *kEvent) {

QStringList keySequence;

if (kEvent->modifiers() & Qt::ControlModifier) {
keySequence << "Ctrl + ";
}
if (kEvent->modifiers() & Qt::AltModifier) {
keySequence << "Alt + ";
}
if (kEvent->modifiers() & Qt::ShiftModifier) {
keySequence << "Shift + ";
}

keySequence << kEvent->text();

ui->label->setText(keySequence.join(""));

}

ChrisW67
1st August 2013, 02:12
Probably because, for example, Ctrl+A is mapped to the text "\x01", Ctrl+B to "\x02" and so on. However, since you don't tell what the actual values are I cannot be sure. If you want to know which key was pressed you should use QKeyEvent::key().

aguayro
1st August 2013, 15:00
if i use QKeyEvent::key(), it returns a numeric value (the number corresponding to each character).
Is there any way to convert it to text on the fly, or i have to make my own QMap with a table of value/key?

I'm doing: ui->label->setText( QStrng(kEvent->key() );

It seems to work fine, but while i only press ctrl, it adds a "!" character, if i press only Alt modifier, adds a "#" character... would be possible ignore/remove these "ghost" characters?