Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 03:19, 26 July 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to capture Keyevents in thread or exe

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: eswar_illuri (23 May 2007)
Last edited: hamishwillee (26 Jul 2012)

The code below shows how a thread can capture some keys and react to them, optionally sending them to the top app if it's not interested in them itself. You'll only need a cleanup stack active for this thread, no active scheduler needed.

Headers Required:

#include <W32STD.H> 
#include <APGWGNAM.H>
#include <e32keys.h> //for key codes

Libraries Required:

LIBRARY ws32.lib
LIBRARY apgrfx.lib

Source:

       // connect to window server
RWsSession ws;
User::LeaveIfError(ws.Connect());
CleanupClosePushL(ws);
 
TRequestStatus status;
 
// create a window group for the thread
RWindowGroup wg(ws);
wg.Construct((TUint32)&wg, EFalse);
CleanupClosePushL(wg);
 
// capture a key
User::LeaveIfError(wg.CaptureKey(EKeyLeftArrow, 0, 0));
 
// listen for the key presses
ws.EventReady(&status);
 
// hide this window group from the app switcher
wg.SetOrdinalPosition(-1);
wg.EnableReceiptOfFocus(EFalse);
CApaWindowGroupName* wn=CApaWindowGroupName::NewLC(ws);
wn->SetHidden(ETrue);
wn->SetWindowGroupName(wg);
 
// handle key events
for(;;) {
User::WaitForAnyRequest();
if (status.Int()==KErrNone) {
TWsEvent e;
ws.GetEvent(e);
 
TInt c;
TKeyEvent* aKeyEvent=e.Key();
c=aKeyEvent->iCode;
 
// do something with keypress
 
// if not ours, then send to top window group
// note that this breaks key repeat
TInt wgid=ws.GetFocusWindowGroup();
ws.SendEventToWindowGroup(wgid, e);
}
ws.EventReady(&status);
 
// stop condition
}
 
// clean up
ws.EventReadyCancel();
CleanupStack::PopAndDestroy(3); //ws, wg, wn


Related Links:

This page was last modified on 26 July 2012, at 03:19.
34 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×