2

What is the way to handle non-latin keys pressed with Qt?

For example, for 'W' key pressed QKeyEvent::key() returns 87, but for 'Ц' - the same key in the russian layout - it returns 1062.

So I can't use constants like Qt::Key_W for checking which key has been pressed: they won't work if a user switches the layout.

Thank you

mvlabat
  • 577
  • 4
  • 17

1 Answers1

2

The meaning of a key depends on the currently selected layout. What you observe is correct. If you pressed that key in any other application, you wouldn't get a W, but a Ц (C).

A given key means Qt::Key_W only if it's in a layout that produces the Roman W.

If you intend to refer to physical keys, you can try using QKeyEvent::nativeScanCode() and/or QKeyEvent::nativeVirtualKey(). These values are platform-dependent, of course.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Thank you for your answer! As it turned out, only `QKeyEvent::nativeScanCode()` returns proper (the same) values for the same keys in different layouts. But if they are platform specific, I can't imagine, how I can use them... Also, according to Qt docs (about `QKeyEvent::nativeScanCode()`): "Note: On Mac OS/X, this function is not useful, because there is no way to get the scan code from Carbon or Cocoa. The function always returns 1 (or 0 in the case explained above)." So I'm thrilled what the way so many programs handle key events?.. – mvlabat Jun 26 '15 at 15:36
  • 2
    @mvlabat Once you let the user program the key<->function assignments, it's not necessary anymore to be stuck to any preset scan codes. That's how everyone else deals with it. Your application can have the `W` default, and you can simply assign more keys to the same function. On a Cyryllic keyboard, someone could also assign the Ц to that function. Just make sure that you can assign more than one key to a function! Basically you have to reformulate your problem. – Kuba hasn't forgotten Monica Jun 26 '15 at 15:56