Qt LEFT CTRL Key Code

In Qt QKeyEvent I can check if Ctrl was pressed if ev->key() is Qt::Key_Control . But how can I distinguish between the left and right Ctrl keys?

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

+6
source share
4 answers

It is not possible to do this using pure Qt methods, as far as I know.

However, depending on your platform, you can distinguish keys using QKeyEvent::nativeScanCode() instead of QKeyEvent::key() .

For example, on Windows, you can check which Ctrl key was pressed as follows:

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

The left and right keys are part of the virtual key code → using nativeVirtualKey () to compare with VK_ * enums windows instead of nativescancode ().

+1
source

According to the Qt namespace reference , enum Qt::Key has a different meaning for Qt::Key_Alt and Qt::Key_AltGr .

However, enum Qt::KeyboardModifier and enum Qt::Modifier do not see the key pair as different modifiers.

(note: I would post this as a comment, but I don't have enough reputation yet)

+1
source

If VK_RCONTROL does not work, check the value of nativeScanCode ctrl-right:

std :: soy <nativeScanCode (); and use this value:

int control_right = 285; if (key-> nativeScanCode () == control_right) {...

0
source

Source: https://habr.com/ru/post/919430/


All Articles