Results 1 to 2 of 2

Thread: Injecting key events at the QApplication level

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2013
    Location
    Everett WA USA
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Injecting key events at the QApplication level

    I am trying to inject the key event to QApplication using following code.

    KeyHandler.h

    class KeyHandler :
    public QObject,
    public QWSKeyboardHandle
    {
    public:
    void initialize();

    protected slots:
    void injectKeyEvent();

    private:
    int fd;
    QSocketNotifier *pKeyNotifier;
    ...
    ...
    ...

    };

    KeyHandler.cpp



    #include <iostream>

    #include <unistd.h>

    #include <fcntl.h>

    #include <linux/input.h>

    ......

    const char* POWER_KEY_DEVICE = "/dev/event0";


    void KeyHandler::initialize()
    {

    fd = open(POWER_KEY_DEVICE, O_RDONLY, 0);

    if(0 <= fd)
    {
    std::cerr<<"Could not open file descriptor the key device.";
    }

    pKeyNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);

    connect(
    pKeyNotifier,
    SIGNAL(activated(int)),
    this,
    SLOT(injectKeyEvent())
    );
    }


    KeyHandler::injectKeyEvent()
    {
    input_event sampleKeyEvent;

    ssize_t readStatus = read(fd, &sampleKeyEvent, sizeof(struct input_event);

    if ((0 < readStatus) && (KEY_POWER == sampleKeyEvent.code))
    {
    // Prepare QKeyEvent with Q Key code
    QKeyEvent *pKeyEvent = new QKeyEvent(
    ((0 == sampleKeyEvent.value) ? QEvent::KeyRelease : QEvent::KeyPress),
    Qt::Key_Q,
    Qt::NoModifier
    );

    std::cout << "Post QKeyEvent " << std::endl;
    QCoreApplicationstEvent(qApp, pKeyEvent);

    /* Used sendEvent for test:
    QCoreApplication::sendEvent(qApp, &keyEvent);
    */
    }
    }


    Question:

    - Is this way to inject Key input to QApplication? OR
    - Am I missing anything here?
    - Will the event posted to QApplication always be captured by MainWidget?
    Last edited by sraju; 30th January 2014 at 21:28.

Similar Threads

  1. Replies: 16
    Last Post: 11th June 2014, 07:16
  2. Replies: 2
    Last Post: 11th May 2011, 17:59
  3. Replies: 2
    Last Post: 4th November 2010, 10:10
  4. Catching Mouse Events on Top-Level Window
    By andyp in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2009, 10:26
  5. Replies: 2
    Last Post: 21st June 2009, 06:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.