×
Namespaces

Variants
Actions
Revision as of 04:16, 27 January 2012 by hamishwillee (Talk | contribs)

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

Simulate key events using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to simulate key events programmatically in Symbian C++. This functionality is provided for "test purposes", but is useful in many other cases. The following sections show a number of different methods to simulate key events.

Article Metadata
Platform Security
Signing Required: DevCert
Capabilities: SwEvent
Article
Created: giridharn (25 May 2007)
Last edited: hamishwillee (27 Jan 2012)

Contents

Procedure1: Using TKeyEvent:

First set the TKeyEvent values for iCode, iScanCode, iModifiers & iRepeats for the key to be simulated. Then call RWsSession::SimulateKeyEvent() passing the keyevent to be generated.

The following is the sample code for simulation of green key:

Location: W32STD.H 	// RWsSession 
Link against: ws32.lib // RWsSession
 
Location: W32STD.H // TKeyEvent
 
RWsSession wsSession=CCoeEnv::Static()->WsSession();
TKeyEvent keyEvent;
keyEvent.iCode = EKeyYes; //member of TKeyCode
keyEvent.iScanCode = EStdKeyYes;
keyEvent.iModifiers = 0;
keyEvent.iRepeats = 0;
wsSession.SimulateKeyEvent(keyEvent);
wsSession.Flush();

Note.pngNote: 

  • From Symbian OS v9.1, this method is protected with the capability: SwEvent.
  • Testing This seems to be working in S60 V3 MR emulator without capabilities (capabilities not checked on Emulator), but produces WSERV 66 panic, when run on a device without capabilities.
  • This method does exists in S60 V3 MR SDK headers, but not in the documentation.

Procedure2: Using TWsEvent:

Simulation of a key can also be accomplished by using TWsEvent class, wherein the event key to be simulated is passed to the RWsSession by calling the SendEventToWindowGroup() and also passing the id of the focus window. This API sends an event to a window group.

The following is the sample code for simulation of Up Arrow key:

Location: W32STD.H 	// RWsSession 
Link against: ws32.lib // RWsSession
 
Location: W32STD.H // TWsEvent
 
TWsEvent event;
RWsSession wsSession=CCoeEnv::Static()->WsSession();
TInt id = wsSession.GetFocusWindowGroup();
event.SetType(EEventKey);
event.SetTimeNow();
event.Key()->iCode = EKeyUpArrow;
event.Key()->iModifiers = 0;
event.Key()->iRepeats = 0;
event.Key()->iScanCode = EStdKeyUpArrow;
wsSession.SendEventToWindowGroup( id, event );
wsSession.Flush();

or

Location: W32STD.H 	// RWsSession 
Link against: ws32.lib // RWsSession
 
Location: W32STD.H // TWsEvent
 
TWsEvent event;
RWsSession wsSession=CCoeEnv::Static()->WsSession();
TInt id = wsSession.GetFocusWindowGroup();
event.SetType(EEventKey);
event.SetTimeNow();
event.Key()->iCode = EKeyUpArrow;
event.Key()->iModifiers = 0;
event.Key()->iRepeats = 0;
event.Key()->iScanCode = 0;
wsSession.SendEventToWindowGroup( id, event );

The output of both code snippets are same.

Procedure3: Using TApaTask:

This API finds the app using TApaTaskList, and sends the required Key event to that App.

Location: apgtask.h 	  // TApaTaskList
Link against: apgrfx.lib // TApaTaskList
 
Location: apgtask.h // TApaTask
Link against: apgrfx.lib // TApaTask
 
TApaTaskList tlist(iEikonEnv->WsSession());
TApaTask app(tlist.FindApp(_L("AppName")));
 
TKeyEvent key;
key.iModifiers = 0;
key.iRepeats = 0;
key.iCode = EKeyDownArrow;
key.iScanCode = EStdKeyDownArrow;
 
app.SendKey(key);

Procedure4: Using TRawEvent:

Simulation can also be done using the TRawEvent::Set() API passing the respective keydown/keyup event and the scancode of the key. After setting the event, add this event to the usersrv.

The following is the sample code for simulation of Red key:

Location: e32event.h       // TRawEvent
Link against: euser.lib // TRawEvent
 
Location: e32std.h // User class
Link against: euser.lib // User class
 
TRawEvent lEventDown;
lEventDown.Set(TRawEvent::EKeyDown, EStdKeyNo);
UserSvr::AddEvent(lEventDown);
User::After(100000);
TRawEvent lEventUp;
lEventUp.Set(TRawEvent::EKeyUp, EStdKeyNo);
UserSvr::AddEvent(lEventUp);


Procedure5: Using SimulateRawEvent:

RWsSession::SimulateRawEvent(TRawEvent aEvent)

This function should only be used for test code.

Special case

If you do not wish to simulate a key event, but open a dialog, with keys 0-9 or so, so that the opening key will appear in the dialog, you can just open the dialog with EEventKeyDown, so the EEventKey will be directed to the dialog itself. This does not require capabilities.

This page was last modified on 27 January 2012, at 04:16.
61 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.

×