How to Handle the Red End Key in a Qt Application

From Qt Wiki
Revision as of 06:13, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

h1. Solution

This requires wrapping the QApplication::symbianEventFilter() function.

<br />Required libraries: QtGui<br />Required header files: w32std.h avkon.hrh<br />PlatSec Capabilities: None<br />Compatibility: S60 3.1 - Symbian^3

bool EndKeyAwareApplication::symbianEventFilter(const QSymbianEvent '''event)<br />{<br /> if (event-&gt;type()  QSymbianEvent::WindowServerEvent) &amp;#123;
        //filtering window server events (TWsEvent)
        if (event-&amp;gt;windowServerEvent()-&amp;gt;Type()  KAknUidValueEndKeyCloseEvent) {<br /> //filtering AVKON close event sent by end key
<br /> //send a Key_Hangup press/release to the focused widget<br /> //change this section if your application needs to do something else<br /> QObject''' target = focusWidget();<br /> if (target) {<br /> QEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Hangup, Qt::NoModifier);<br /> postEvent(target, event);<br /> QEvent *event2 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Hangup, Qt::NoModifier);<br /> postEvent(target, event2);<br /> }<br /> //return true to prevent default handling of the event (default handler will quit app)<br /> return true;<br /> }<br /> }<br /> //allow base class to handle any other events in the default way<br /> return QApplication::symbianEventFilter(event);<br />}

This sample shows a simple way of handling the end key so that your application is not quit. It does not prevent the application being closed by the task switcher, and does not interfere with operation of the phone application. Pressing the red key will bring the phone application to the foreground, but leave your application running in the background.

Variants and Extensions

To save state and exit when the red key is pressed, you should instead use the QCoreApplication::aboutToQuit signal, which is cross-platform

Summary

This recipe shows how you handle the red End key in a Qt application.