Simulate a keypress(转)

 



Simulation of keys can be used in numerous Applications where we need to programmatically simulate key events for various purposes.

Simulating key events can be done in the following ways:

Contents

  [hide ]

[edit]Procedure1: Using TKeyEvent:

First set the iCode,iScanCode,iModifiers & iRepeats for the key to be simulated of the TKeyEvent class. Using RWsSession call the SimulateKeyEvent() by 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 that at least in Symbian OS v9.3, this method is protected with the capability: SwEvent.

NOTE: This seems to be working in S60 V3 MR emulator without capabilities, but produces WSERV 66 panic, when run on a device without capabilities.

NOTE2: This method does exists in S60 V3 MR SDK headers, but not in the documentation.

[edit]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 codes are same.

Note that at least in Symbian OS v9.3, this method is protected with the capability: SwEvent.

[edit]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);

Note that at least in Symbian OS v9.3, this method is protected with the capability: SwEvent.

[edit]Procedure4: Using TRawEvent:

Simulation can also be done using the TRawEvent class by 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);

Note that in Symbian OS v9.x, this method is protected with the capability: SwEvent.

[edit]Procedure5: Using SimulateRawEvent:

RWsSession::SimulateRawEvent(TRawEvent aEvent)

This function should only be used for test code. Note that in Symbian OS v9.x, this method is protected with the capability: SwEvent.

[edit]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.

转载于:https://www.cnblogs.com/yaoliang11/archive/2010/11/02/1866980.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值