c++qteventskeyboard-eventsctrl

How to distinguish between left and right modifier keys


In Qt's QKeyEvent, I can check whether Ctrl was pressed by checking if ev->key() is Qt::Key_Control, but how can I distinguish between the left and right Ctrl keys?

I also need the same thing for Alt and Shift keys.


Solution

  • There is no way to do this using pure Qt methods, as far as I know.

    Depending on your platform, however, you might be able to distinguish between the keys using the QKeyEvent::nativeScanCode() method instead of QKeyEvent::key().

    For example, on Windows you should be able to test which Ctrl key was pressed as follows:

    if (event->nativeScanCode() == VK_LCONTROL) {
      // left control pressed
    } else if (event->nativeScanCode() == VK_RCONTROL) {
      // right control pressed
    }