×
Namespaces

Variants
Actions

Listening an application touch and key events

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to listen for application key and mouse/touch events. 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 QObject::installEventFilter() function enables this by setting up an event filter.

Article Metadata
Tested with
Devices(s): Nokia E7, Nokia N900
Compatibility
Platform(s): S60 5th Edition, Symbian^3, Maemo 5
Symbian
Symbian^3
Article
Keywords: QObject::installEventFilter()
Created: tepaa (14 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

Preconditions

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

Event filter class

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

#include <QObject>
#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

Installing event filter for example into your QMainWindow for listening events into it. It is also possible to filter all events for the entire application, by installing an event filter on the QApplication.

// 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

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


Reference links

This page was last modified on 11 October 2012, at 04:17.
53 page views in the last 30 days.
×