I have a USB device with 6 buttons that I am trying to use with my app. The document for the device lists the key mappings for each button. One of these button for example is said to map to Ctrl+Shift+F20.
Coding this though does not work:
The code I have inherited is:
void devControl::processKeyRelease(QKeyEvent *ke)
{
int key = ke->key();
if (ke->modifiers() & Qt::ShiftModifier &&
ke->modifiers() & Qt::ControlModifier)
{
if ( key = Qt::Key_F20 )
{
// Do somethings
}
}
.
.
}
I used USBLyzer ( http://www.usblyzer.com/ ) and evtest on Linux ( https://www.systutorials.com/docs/linux/man/1-evtest/ ) to check the data/keys being sent and found for this button the following:
The raw value is 01 03 00 6F 00 00 00 00 00
.
Looking at all the buttons data, they all exhibit the same behaviour in the the modifiers are repeated.
My thought is to try this:
if (ke->modifiers() & Qt::ShiftModifier &&
ke->modifiers() & Qt::ControlModifier &&
ke->modifiers() & Qt::ShiftModifier &&
ke->modifiers() & Qt::ControlModifier)
But is this correct? I am waiting to try this in the morning when I go back in to work, but would appreciate advice form anyone who has seen this before.
EDIT 1
The above did not make a difference. I added some more debug as follows:
if (e->type() == QEvent::KeyPress) {
QKeyEvent *eKey = static_cast<QKeyEvent *>(e);
qDebug() << "Key PRESS event " << eKey->modifiers() << Qt::Key(eKey->key());
}
The results were:
Key PRESS event QFlags<Qt::KeyboardModifiers>(ControlModifier) Qt::Key(Key_Control)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ControlModifier) Qt::Key(Key_Control)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ControlModifier) Qt::Key(Key_Control)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ControlModifier) Qt::Key(Key_Control)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ControlModifier) Qt::Key(Key_Control)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_Shift)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_Shift)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_Shift)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_Shift)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_Shift)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_F20)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_F20)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_F20)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_F20)
Key PRESS event QFlags<Qt::KeyboardModifiers>(ShiftModifier|ControlModifier) Qt::Key(Key_F20)
I need a way to know when the whole event or key combinations start and end for sure. Then I can accumulate the presses over the whole event and decide what button was pressed.
EDIT 2
Decided to handle this by adding booleans to record when key Control, Shift or Alt are detected, and when a non-modifier is detected assume we have all the keys (true for this device) then execute the appropriate logic.
This works now.
bool _controlPressed; bool _shiftPressed; bool _altPressed; bool _action_Completed;
These are set to false when a new button press is detected.
Then each time OnKeyRelease is called:
if (key == Qt::Key_Control ){
_action_Completed = false; // We expect one or more key
_controlPressed = true;
return;
}
if (key == Qt::Key_Shift ) {
_action_Completed = false; // We expect one or more key
_shiftPressed = true;
return;
}
if (key == Qt::Key_Alt ){
_action_Completed = false; // We expect one or more key
_altPressed = true;
return;
}
if ( _controlPressed && _shiftPressed && _altPressed && ! _action_Completed )
{
if (! mappedKey( key)) {
return;
}
doCtrlShiftAlt( key);
return;
}
if ( _controlPressed && _shiftPressed && ! _action_Completed )
{
if (! mappedKey( key)) {
return;
}
doCtrlShift( key);
return;
}
if ( _shiftPressed && ! _action_Completed )
{
if (! mappedKey( key)) {
return;
}
doShift( key);
return;
}
if (! mappedKey( key)) {
return;
}
if ( ! _action_Completed )
{
doNoMod( key);
}