C

处理键盘输入

如果系统配备了键盘,您可以使用PlatformInterface::handleKeyEvent()并配合Qul::EventQueue 向应用程序发送键盘事件。Qt Quick Ultralite引擎会将键盘事件原样传递给应用程序进行进一步处理,而不会对其进行解析。应用程序中的任何视觉QML项均可接收键盘事件,更多信息请参阅Keys QML Type 。

以下是一个发送键选择事件的示例:

struct KeyboardEvent
{
    uint64_t timestamp;
    Qul::PlatformInterface::KeyEventType type;
    int key;
    unsigned int nativeScanCode;
    unsigned int modifiers;
    uint32_t ucs4;
    bool autorepeat;
};

class PlatformKeyboardEventQueue : public Qul::EventQueue<KeyboardEvent>
{
    void onEvent(const KeyboardEvent &event) QUL_DECL_OVERRIDE
    {
        // Use the information stored in the custom struct shown above
        // to transfer the information into the Qul engine
        Qul::PlatformInterface::handleKeyEvent(event.timestamp,
                                               event.type,
                                               event.key,
                                               event.nativeScanCode,
                                               event.modifiers,
                                               NULL,
                                               event.autorepeat,
                                               event.ucs4);
    }
};

static PlatformKeyboardEventQueue keyboardEventQueue;

void keyboardISR()
{
    // Here would be platform specific code to fetch data from the keyboard.
    static KeyboardEvent event;
    event.timestamp = Qul::Platform::getPlatformInstance()->currentTimestamp();
    event.type = Qul::PlatformInterface::KeyPressEvent;
    event.key = Qul::PlatformInterface::Key_A;
    event.nativeScanCode = 0;
    event.modifiers = Qul::PlatformInterface::NoKeyboardModifier;
    event.ucs4 = 0x61;
    event.autorepeat = false;

    keyboardEventQueue.postEventFromInterrupt(event);
}

注意: Qt Quick Ultralite 仅将键盘事件类型、键码和本机扫描码转发给应用程序。本示例中显示的其他参数不会被转发。

在某些 Qt 许可下可用。
了解更多。