PDA

View Full Version : Handling of dead keys in keyPressEvent()



ghorwin
29th November 2006, 04:11
Hi there,

I wan't to capture key presses, including dead keys. So for instance, if you press ~ and afterwards n, you get ñ (at least on a spanish keyboard layout).

When I reimplement keyPressEvent() in my widget, I get however always the ~ and n separately. I would rather get only one keyPressEvent() with the complete character.

How would I best do this?
Bye,
Andreas

wysota
29th November 2006, 09:31
You'll always get two key press events, because at the time when '~' is pressed there is no way telling if something else is going to be pressed as well, so the event has to be triggered. As for the composed character, it should be available in QKeyEvent::text() during the second key event.

ghorwin
30th November 2006, 14:34
This is however not what I observe. I receive a keyPressEvent() after the dead key was pressed and can detect that correctly when comparing the event->key() with for instance Qt::Key_Dead_Tilde. However, regardless whether I accept or ignore this event, the next keypress event will just give me the plain character. So event->text() contains a "n" instead of a "ñ".

Any suggestions?
Andreas

wysota
30th November 2006, 14:42
Try setting Qt::WA_KeyCompression flag for widget that receives those key events.

ghorwin
2nd December 2006, 13:26
Got it to work. The handling of dead keys is directly linked to the keyboard layout selected. I wrote a small test program and tested it on windows and linux and apparently the settings in the X-Windows system were the problem.

Anyway, if the keyboard layout is correctly set up, Qt doesn't generate a keypressevent for dead keys, but only for the fully composed key. I also tried the compression flag, but it didn't make any difference. It really only depends on the keyboard settings (dead keys are also different from language to language).

Thanks again for the suggestions!
Andreas