×
Namespaces

Variants
Actions

如何在线程或exe中捕捉按键事件

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 24 Aug 2009 创建
最后由 hamishwillee 在 15 Dec 2011 编辑
  • 详细描述

下列代码演示了一个线程如何捕捉按键并响应他们,如果不需要处理可以选择将他们发送到最顶端的app中,你只需要为线程生成清除栈即可,不需要活动调度。

  • 头文件
#include <W32STD.H> 
#include <APGWGNAM.H>
#include <e32keys.h> //for key codes
  • 需要的链接库
LIBRARY  ws32.lib
LIBRARY apgrfx.lib


  • 源文件
       // 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
This page was last modified on 15 December 2011, at 09:05.
38 page views in the last 30 days.
×