2

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:

enter image description here

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);
}
TenG
  • 3,843
  • 2
  • 25
  • 42
  • 1
    I think that coding your proposed "if" statement would be equivalent to coding "if A and B and A and B" which the compiler would optimise to just "if A and B". I would do some good old-fashioned println debugging in processKeyRelease to find out exactly what it is seeing when the device presses the keys. – aja Jan 21 '18 at 13:13
  • Thanks aja. As you expected my if check change made no difference. I've added some more debug. – TenG Jan 21 '18 at 13:33
  • The QKeyEvent object apparently contains a count value which is the number of keys involved in the event. Perhaps you could test for 3. I'm no Qt expert but it might be worth a shot. – aja Jan 22 '18 at 07:40

0 Answers0