Results 1 to 2 of 2

Thread: Injecting key events at the QApplication level

  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 22:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Injecting key events at the QApplication level

    I think you need to send the event to the widget that should recieve it.

    If you want the main widget to receive it, post to it. You could alos post to the focus widget.

    Cheers,
    _

Similar Threads

  1. Replies: 16
    Last Post: 11th June 2014, 08:16
  2. Replies: 2
    Last Post: 11th May 2011, 18:59
  3. Replies: 2
    Last Post: 4th November 2010, 11:10
  4. Catching Mouse Events on Top-Level Window
    By andyp in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2009, 11:26
  5. Replies: 2
    Last Post: 21st June 2009, 07: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.