如何使后台运行的程序获取按键事件
2010-12-12 20:33
375 查看
一、基本思路:
首先程序运行时注册按键事件,然后将程序转入后台运行,当按键事件发生后在AppUi的HandleKeyEventL中处理。
相关代码如下:
void CClockSSAppUi::SetCaptureKey()
{
// If there is another handle, we have to cancel it first.
CancelCaptureKey();
// This will capture scan code of the keypress.
iHandleCaptureKey = CCoeEnv::Static()-> RootWin().CaptureKeyUpAndDowns(
KOkKeyScanCode, EModifierShift, EModifierShift PRIORITYCAPTUREKEY);
//// WARNING: We need to capture the normal code of keypress otherwise
// the key event will be sent to another application.
iHandleCaptureKey2 = CCoeEnv::Static()-> RootWin().CaptureKey(
KOkKeyCode, EModifierShift, EModifierShift PRIORITYCAPTUREKEY);
}
TKeyResponse CClockSSAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if ((KOkKeyScanCode == (TUint) aKeyEvent.iScanCode) && (EEventKeyDown == aType)
&& ((aKeyEvent.iModifiers & EModifierShift) == EModifierShift) )
{
CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
globalNote->ShowNoteL(EAknGlobalInformationNote, _L(”Captured KEY!”));
CleanupStack::PopAndDestroy();
}
}
需要注意的是:如果你的是多view程序,并且调用了AppUi::AddToStackL接收按键事件,那么在这些控件OfferKeyEventL的处理中对于不相关的键事件一定要返回EKeyWasNotConsumed,好让AppUi最后能处理。
二、如何将程序送到后台运行
比如说,当按下back后,在HandleCommandL()中加入如下处理:
case EAknSoftkeyBack:
{
TApaTask apaTask(iEikonEnv->WsSession());
apaTask.SetWgId(CCoeEnv::Static()->RootWin().Identifier());
apaTask.SendToBackground();
break;
}
解释一下:
A task is a running application.
A task is identified by its association with the running application’s window group.
apaTask把自己送到后台,并在后台继续运行。
首先程序运行时注册按键事件,然后将程序转入后台运行,当按键事件发生后在AppUi的HandleKeyEventL中处理。
相关代码如下:
void CClockSSAppUi::SetCaptureKey()
{
// If there is another handle, we have to cancel it first.
CancelCaptureKey();
// This will capture scan code of the keypress.
iHandleCaptureKey = CCoeEnv::Static()-> RootWin().CaptureKeyUpAndDowns(
KOkKeyScanCode, EModifierShift, EModifierShift PRIORITYCAPTUREKEY);
//// WARNING: We need to capture the normal code of keypress otherwise
// the key event will be sent to another application.
iHandleCaptureKey2 = CCoeEnv::Static()-> RootWin().CaptureKey(
KOkKeyCode, EModifierShift, EModifierShift PRIORITYCAPTUREKEY);
}
TKeyResponse CClockSSAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if ((KOkKeyScanCode == (TUint) aKeyEvent.iScanCode) && (EEventKeyDown == aType)
&& ((aKeyEvent.iModifiers & EModifierShift) == EModifierShift) )
{
CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
globalNote->ShowNoteL(EAknGlobalInformationNote, _L(”Captured KEY!”));
CleanupStack::PopAndDestroy();
}
}
需要注意的是:如果你的是多view程序,并且调用了AppUi::AddToStackL接收按键事件,那么在这些控件OfferKeyEventL的处理中对于不相关的键事件一定要返回EKeyWasNotConsumed,好让AppUi最后能处理。
二、如何将程序送到后台运行
比如说,当按下back后,在HandleCommandL()中加入如下处理:
case EAknSoftkeyBack:
{
TApaTask apaTask(iEikonEnv->WsSession());
apaTask.SetWgId(CCoeEnv::Static()->RootWin().Identifier());
apaTask.SendToBackground();
break;
}
解释一下:
A task is a running application.
A task is identified by its association with the running application’s window group.
apaTask把自己送到后台,并在后台继续运行。
相关文章推荐
- Linux中如何让进程(或正在运行的程序)到后台运行?[zz]
- 如何获取程序运行时间?
- Linux中如何让进程(或正在运行的程序)到后台运行?
- 如何在终端下以后台模式运行Linux程序
- iOS如何拦截音量按键事件(前台+后台)
- java 如何获取程序运行时的相对路径
- 如何让控制台的程序在后台运行
- 027、获取后台正在运行的程序
- linux可执行程序如何在系统后台运行
- linux在用户程序中如何向操作系统发送按键事件
- swing登陆程序的异步loading效果,避免程序假死状态?swing 后台操作事件长 , 界面如何显示正在等待?
- linux下c++程序扔在后台运行,如何避免断开ssh会话,程序也关闭
- [linux] 如何在后台运行程序
- 如何在service中获取按键监听事件呢
- iOS如何保持程序在后台长时间运行
- Linux中如何让进程(或正在运行的程序)到后台运行?
- iOS4支持后台运行,程序的事件响应
- 如何通过程序kill掉后台运行shell脚本中的mapreduce和hive任务
- Go语言如何计算程序运行的时间效率,获取时间差
- Linux中如何让进程(或正在运行的程序)到后台运行?