[Solved] Obtaining keyboard keycode from QWidget::macEvent()'s EventRef paraneter?



  • Hello,

    I am making a Qt application and want to intercept keypresses at a deeper level. To do this I overrided the "QWidget::macEvent()":http://qt-project.org/doc/qt-4.8/qwidget.html#macEvent function to intercept the EventRef parameter.

    For different events (i.e. keyboard events, mouse events, window events) EventRef is equal to the following values:

    0x104d649f0
    0x101425740
    0x104d649f0
    0x101485950
    0x101425740
    0x101485950

    How can I tell if the event is a keyboard event? And how can I find out which key was pressed? I've looked at event handling documentation and most of them don't mention EventRef. Some articles mention how EventRef is the Carbon event derived from the Cocoa NSEvent event.



  • After some guessing work, and looking at "this chapter from a book":http://oreilly.com/catalog/learncarbon/chapter/ch06.html#CHAP-CH06-6, I've succeeded in interpreting the EventRef object for keypresses in Qt applications:

    MyProject.pro: Include Carbon framework:

    @macx {
    LIBS += -framework Carbon
    }@

    MyWidget.h: override "QWidget::macEvent()":http://qt-project.org/doc/qt-4.8/qwidget.html#macEvent

    @class MyWidget : public QWidget {

    ...

    private:
    #if defined(Q_OS_MAC)
    bool macEvent(EventHandlerCallRef, EventRef event);
    #endif
    };@

    MyWidget.cpp:

    @#if defined(Q_OS_MAC)
    #include <Carbon/Carbon.h>
    #endif

    ...

    #if defined(Q_OS_MAC)
    bool MyWidget::macEvent(EventHandlerCallRef, EventRef event) {
    uint32_t keyCode;

    switch (GetEventClass(event)) {
    case kEventClassApplication:
    qDebug() << "kEventClassApplication";
    break;
    case kEventClassCommand:
    qDebug() << "kEventClassCommand";
    break;
    case kEventClassControl:
    qDebug() << "kEventClassControl";
    break;
    case kEventClassKeyboard:
    qDebug() << "kEventClassKeyboard";

    switch(GetEventKind(event)) {
     case kEventRawKeyDown:
      qDebug() << "kEventRawKeyDown";
    
      GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, sizeof(keyCode), NULL, &keyCode);
      qDebug() << "keyCode =" << keyCode;
    
      break;
     case kEventRawKeyRepeat:
      qDebug() << "kEventRawKeyRepeat";
      break;
     case kEventRawKeyUp:
      qDebug() << "kEventRawKeyUp";
    
      GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, sizeof(keyCode), NULL, &keyCode);
      qDebug() << "keyCode =" << keyCode;
    
      break;
     case kEventRawKeyModifiersChanged:
      qDebug() << "kEventRawKeyModifiersChanged";
      break;
     case kEventHotKeyPressed:
      qDebug() << "kEventHotKeyPressed";
      break;
     case kEventHotKeyReleased:
      qDebug() << "kEventHotKeyReleased";
      break;
    }
    
    break;
    

    case kEventClassMenu:
    qDebug() << "kEventClassMenu";
    break;
    case kEventClassMouse:
    qDebug() << "kEventClassMouse";
    break;
    case kEventClassTablet:
    qDebug() << "kEventClassTablet";
    break;
    case kEventClassTextInput:
    qDebug() << "kEventClassTextInput";
    break;
    case kEventClassWindow:
    qDebug() << "kEventClassWindow";
    break;
    default:
    break;
    }

    return true;
    }
    #endif@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.