Up to: Mika Raento's Symbian Programming pages

Symbian Programming - Capturing key presses in thread/exe

If you want to change the behaviour of some keys on the phone, you can do this by capturing them in a thread (or exe), and handling them yourself. This is not hard, but figuring out all the steps needed takes a bit of effort.

3rd edition

The method below does work on 3rd edition but has the limitation that numeric keys are not captured since the phone application is already getting them in its own way. To get (almost) all keys you can use an animation dll instead.

1st/2nd edition

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.

	// connect to window server
	RWsSession ws;

	TRequestStatus status;

	// create a window group for the thread
	RWindowGroup wg(ws);
	wg.Construct((TUint32)&wg, EFalse);

	// capture a key
	User::LeaveIfError(wg.CaptureKey(EKeyLeftArrow, 0, 0));

	// listen for the key presses

	// hide this window group from the app switcher
	CApaWindowGroupName* wn=CApaWindowGroupName::NewLC(ws);

	// handle key events
	for(;;) {
		if (status.Int()==KErrNone) {
			TWsEvent e;

			TInt c;
			TKeyEvent* aKeyEvent=e.Key();

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

		// stop condition

	// clean up
	CleanupStack::PopAndDestroy(3); //ws, wg, wn

The codes shows how to hide a window group as well. You'll need to add some means of detecting when the thread should finish.

Mika Raento, mikie(at)iki.fi