Qt does not provide a portable name for these keys. However, it does give you access to the platform-specific scancodes for the keys. This is done through QKeyEvent::nativeScanCode()
. In your QWidget::keyPressEvent()
function, add some temporary code to print the scan code of the keys you press:
#include <QDebug>
#include <QKeyEvent>
void YourWidgetClass::keyPressEvent(QKeyEvent *event)
{
qDebug() << event->nativeScanCode();
event->accept();
}
Now run your program and press the shift keys. Here on my system (Linux/X11), left shift has the code 50, right shift has the code 62. So that means you can check for those:
void YourWidgetClass::keyPressEvent(QKeyEvent *event)
{
switch (event.nativeScanCode()) {
case 50: // left shift
case 62: // right shift
}
}
These codes will be the same on all Linux systems running in X11 using XCB. They might be different on other Linux systems (like those running Wayland instead of X11.) And they are different on Windows too, though all Windows versions use (AFAIK) the same codes. You can Google for what the scan codes are on Windows. Note that scan codes are usually documented in hex, so when you see "30", you should use "0x30" in your code.
You can't do this on Mac OS though. On Mac OS, you would need to use a non-Qt API to get the code.