Detecting right shift key in Qt

Is it possible to grab the right shift key only in Qt?

In the list of values for the enumeration Qt :: Key there Key_Shift

(well Key_Kana_Shift

, and Key_Eisu_Shift

, but they seem for the Japanese keyboard), but I do not know how to distinguish between the left and right shift keys, is it possible?

I would like to find a solution that works for major platforms (GNU / Linux, Windows, MacOS)

Grateful for the help and courtesy, Tord

+3


source to share


2 answers


Qt does not provide a portable name for these keys. However, this gives you platform-specific scancodes for keys. This is done through QKeyEvent::nativeScanCode()

. In your function, QWidget::keyPressEvent()

add a temporary code to print the keystroke scan code:

#include <QDebug>
#include <QKeyEvent>
void YourWidgetClass::keyPressEvent(QKeyEvent *event)
{
    qDebug() << event->nativeScanCode();
    event->accept();
}

      

Now start your program and hit the toggle keys. Here on my system (Linux / X11) left shift has code 50, right shift has code 62. So you can check them:



void YourWidgetClass::keyPressEvent(QKeyEvent *event)
{
    switch (event.nativeScanCode()) {
    case 50: // left shift
    case 62: // right shift
    }
}

      

These codes will be the same for all Linux systems running on X11 using XCB. They may differ from other Linux systems (for example, for those who run Wayland instead of X11.) And they also differ from Windows, although all versions of Windows use (AFAIK) the same codes. You can use Google for Windows scan codes. Note that scan codes are usually documented in hexadecimal, so when you see "30" you must use "0x30" in your code.

You cannot do this on Mac OS. On Mac OS, you will need to use a non-Qt API to get the code.

+1


source


If you are coding Windows OS you can use the nativeVirtualKey () const type. Windows provides identifiers for individual keystrokes, including the left and right shift keys (VK_LSHIFT and VK_RSHIFT respectively) https://msdn.microsoft.com/en-us/library/ms927178.aspx . After accessing the virtual keys of your keypress event, do the following:



if (event->nativeVirtualKey() == VK_LSHIFT) {
// left shift specific code
} else if (event->nativeVirtualKey() == VK_RSHIFT) {
// right shift specific code
}

      

+1


source







All Articles