经过一些猜测工作,并从一本书中查看这一章,我成功地解释了 Qt 应用程序中按键的 EventRef 对象:
MyProject.pro:包括Carbon
框架:
macx {
LIBS += -framework Carbon
}
MyWidget.h : 覆盖QWidget::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