Question

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.

Was it helpful?

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
}

OTHER TIPS

According to the Qt Namespace Reference, the enum Qt::Key has a different value for Qt::Key_Alt and Qt::Key_AltGr.

However, enum Qt::KeyboardModifier and enum Qt::Modifier don't see the pair of keys as different modifiers.

(note: I would have posted this as a comment but I don't have enough rep. yet)

Left and Right keys are part of virtual key code -> use nativeVirtualKey() to compare with windows VK_* enums instead of nativescancode().

If VK_RCONTROLdoes not work, check your nativeScanCode value of ctrl-right:

std::cout<<keyEvent->nativeScanCode(); and use this value:

int control_right = 285;
if(key->nativeScanCode() == control_right){...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top