Listening an application touch and key events

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract Symbian^3 platforms hides a context menu items from Options menu when using touch and enables them when using keyboard. This snippets show how to implement event listening via an event filter class.

The event filter gets to process events before the target object does, allowing it to inspect and discard the events as required. The 76ytuiytuityutyutututyutyutyu function enables this by setting up an event filter. Template:ArticleMetaData

Preconditions[edit | edit source]

Nokia E7 device has both keyboard and touch functionality for using applications, code is tested on that device.

Event filter class[edit | edit source]

This event filter listen keyboard and mouse events and sends 76ytuiytuityutyutututyutyutyu or 76ytuiytuityutyutututyutyutyu signal. Event filter is implemented into separate class, that is not mandatory, see more from Events and Filters

  1. include <QObject>
  2. include <QKeyEvent>

class KeyEventListener : public QObject {

   Q_OBJECT

public:

   KeyEventListener(QObject *parent = 0);
   ~KeyEventListener();

protected:

    bool eventFilter(QObject *obj, QEvent *event);

signals:

   void keyboardEnabled(bool);

};


KeyEventListener::KeyEventListener(QObject *parent) :

   QObject(parent){}

KeyEventListener::~KeyEventListener(){}

bool KeyEventListener::eventFilter(QObject *obj, QEvent *event) {

   if (event->type() == QEvent::KeyPress) {
       QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
       if (keyEvent->key()==16777385) {
           // User opens keyboard
           emit keyboardEnabled(true); 
       } else if (keyEvent->key()==16777386) {
           // User closes keyboard
           emit keyboardEnabled(false); 
       } else {
           // Keyboard used
           emit keyboardEnabled(true); 
       }
       return false;
   } else if (event->type() == QEvent::MouseButtonPress) {
        // User touch the screen
       emit keyboardEnabled(false);
       return false;
   } else {
       return QObject::eventFilter(obj, event);
   }

}


Using event filter[edit | edit source]

Installing event filter for example into your 76ytuiytuityutyutututyutyutyu for listening events into it. It is also possible to filter all events for the entire application, by installing an event filter on the 76ytuiytuityutyutututyutyutyu. // Create filter KeyEventListener* keyEventListener = new KeyEventListener(this); QObject::connect(keyEventListener,SIGNAL(keyboardEnabled(bool)),this,SLOT(keyboardEnabled(bool)));

// Install it into your application main window for catching events qApp->installEventFilter(keyEventListener);

Create Options menu that will be disabled when touch is used void MainWindow::createMyMenu() {

   m_openAction = menuBar()->addAction("Open",this,SLOT(someSlot()));
   m_deleteAction = menuBar()->addAction("Delete",this,SLOT(someSlot()));
   // Disable by default
   m_openAction->setEnabled(false);
   m_deleteAction->setEnabled(false);

}


Handling both keyboard or touch enabled case void MainWindow::keyboardEnabled(bool enableKeyboard) {

   if (!enableKeyboard&& m_openAction->isEnabled()) {
       // Touch used, disable menu
       m_openAction->setEnabled(false);
       m_deleteAction->setEnabled(false);
   } else if(enableKeyboard&& !m_openAction->isEnabled()) {
       // Keyboard used, enable menu
       m_openAction->setEnabled(true);
       m_deleteAction->setEnabled(true);
   }

}


Postconditions[edit | edit source]

Options menu is disabled on touch and enabled on keyboard use.


Reference links[edit | edit source]