Chinaunix首页 | 论坛 | 博客
  • 博客访问: 624134
  • 博文数量: 116
  • 博客积分: 6078
  • 博客等级: 准将
  • 技术积分: 1214
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-23 10:09
文章分类

全部博文(116)

文章存档

2016年(1)

2015年(4)

2011年(2)

2010年(21)

2009年(88)

分类: C/C++

2009-06-12 16:06:48

我们有时需要在后台运行软件捕捉用户手机的按键信息,一般可以作为安全软件,检查用户的输入,同样也可以作为黑客软件盗用用户的输入.
要实现该功能,有以下几个步骤
1.要求具有capability:SwEvent
2.头文件包括#include
增加成员变量
 RWsSession    iWsSession; // Window session.
 RWindowGroup  iWg;    // Windows group.
3.处理类为继承CActive
4.在类的ContructL函数
void ConstructL()
{
 // create session.
 User::LeaveIfError(iWsSession.Connect());
 // create window group.
 iWg = RWindowGroup(iWsSession);
 User::LeaveIfError(iWg.Construct((TUint32)&iWg, EFalse));//EFalse 不接收键盘Focus
 // Enable app to recive event.
 iWg.SetOrdinalPosition(-1, ECoeWinPriorityNormal+2 );//设置窗口的顺序和优先级,-1表示最上面
 iWg.EnableReceiptOfFocus(EFalse); //该窗口不接受键盘Focus
 // window group
 CApaWindowGroupName* wn = CApaWindowGroupName::NewLC(iWsSession); //创建窗口组名
 wn->SetHidden(ETrue); //隐藏名字
 wn->SetWindowGroupName(iWg); //与窗口组绑定
 CleanupStack::PopAndDestroy();
 // set key to capture.
 User::LeaveIfError(iWg.CaptureKey(EKeyCaptureNum0, 0,0));
//EKeyCaptureNum0==0x30 ,数字0,设置要求Windows Server无论哪个窗口按0键按下都要通知,后面0,0可以作为要求同时按Ctrl或Alt按键.
//如果需要捕获其他按键的话,只要添加相关的语句就可以
 // start it.  启动线程捕获事件
 if (!IsActive()) {
 iWsSession.EventReady(&iStatus);
 SetActive();
 }
}
5.类函数RunL中
void RunL()
{
  TWsEvent e;
  iWsSession.GetEvent(e);
  // Get event type.
  TInt type = e.Type();
  // check event type.
  switch (type)
  {
  case EEventKey:
      switch(code)
        {
                case EKeyCaptureNum0:
                         break;    //数字0 
        }
  case EEventKeyUp:    //向上按键
   break;
  case EEventKeyDown: //向下按键
   break;
 }
  TInt wgId = iWsSession.GetFocusWindowGroup();
  iWsSession.SendEventToWindowGroup( wgId, e );  //注意要将捕获的事件转发给当前Focus窗口
 iWsSession.EventReady(&iStatus);
  SetActive();   //重新启动
}
6.对于捕获了按键后想进行其他处理的话,建议使用其他线程(活动对象)或定时器的方式,不要在捕获线程中直接处理,避免影响处理速度.
阅读(1568) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~