注:此贴在我blog中地址为http://www.devdiv.net/home/space.php?uid=1&do=blog&id=313 主程序: #include "MGuardMain.h" #include "MGuardKeyCapture.h" LOCAL_C void MainL( const TDesC& /*aArgs*/ ) { capturer = new (ELeave)CGuardKeyCapturer(); capturer->StartCapturingL(); CActiveScheduler::Start(); } LOCAL_C void DoStartL() { // Create active scheduler (to run active objects) CActiveScheduler* scheduler = new (ELeave) CActiveScheduler(); CleanupStack::PushL(scheduler); CActiveScheduler::Install(scheduler); // Call main function with command line TBuf<256> cmdLine; RProcess().CommandLine(cmdLine); MainL(cmdLine); // Delete active scheduler CleanupStack::PopAndDestroy(scheduler); } // Global Functions GLDEF_C TInt E32Main() { // Create cleanup stack CTrapCleanup* cleanup = CTrapCleanup::New(); // Create output console TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen))); if (createError) return createError; // Run application code inside TRAP harness, wait keypress when terminated TRAPD(mainError, DoStartL()); if (mainError) console->Printf(KTextFailed, mainError); console->Printf(KTextPressAnyKey); console->Getch(); delete console; delete cleanup; return KErrNone; } |
主程序的头文件
/* * ============================================================================ * Name : MGuardMain.h * Part of : MGuardMain * Created : 18.07.2007 by xueyw * Description: * Exe header file * Version : * Copyright: * ============================================================================ */ #ifndef __MGUARDMAIN_H__ #define __MGUARDMAIN_H__ // Include Files #include <e32base.h> // Function Prototypes GLDEF_C TInt E32Main(); #endif // __MGUARDMAIN_H__ // End of file
锁屏实现的头文件
/* * ============================================================================ * Name : MGuardKeyCapture.h * Part of : MGuard * Created : July 21 2007 by xueyw * Description: * Copyright: * ============================================================================ */ #ifndef __MGUARDKEYCAPTURE_H__ #define __MGUARDKEYCAPTURE_H__ // Include Files #include <e32base.h> #include <w32std.h> // RWsSession // Forward declarations class RWindowGroup; class CApaWindowGroupName; class CGuardKeyCapturer : public CActive { public: // New functions /** * Registers itself for the key press events * @leave Symbian standard leave codes */ void StartCapturingL(); void StopCapturingL(); void ResumeCapturingL(); ~CGuardKeyCapturer(); CGuardKeyCapturer(); public: // From CActive /** * Is called by active scheduler when key press happens */ void RunL(); public: TBool IsCatpureStarted() { return iCaptureStarted; } TBool IsCatpurePending() { return iCapturePending; } enum { EMaxKeyCount = 160 }; protected: /** * Is called when event listening should be stopped */ virtual void DoCancel(); private: // Data // Session to the window server RWsSession iWsSession; // Window group created to listed to the events RWindowGroup* iWindowGroup; // Is used to hide window from the task switcher CApaWindowGroupName* iWindowGroupName; // Handle to the capturing request TInt32 iCaptureHandle[EMaxKeyCount]; TInt iCaptureHandleCount; // Handle to the second capturing request TInt32 iLongCaptureHandle; // Number of keypresses already captured TInt iCaptureCounter; TBool iCaptureStarted; TBool iCapturePending; }; #endif // __MGUARDKEYCAPTURE_H__ // End of file
锁屏的实现文件 |
/* * ============================================================================ * Name : MGuardKeyCapture.cpp * Part of : MGuard * Created : July 21 by xueyw * Description: * Copyright: * ============================================================================ */ // Include Files #include "MGuardKeyCapture.h" #include <e32base.h> #include <e32std.h> #include <apgwgnam.h> // CApaWindowGroupName #include <aknkeylock.h> #include <apacmdln.h> #include <eikdll.h> // Capture KNumberOfPressesToCapture key presses const TInt KNumberOfPressesToCapture = 3; //////////////////////////////////// // CGuardKeyCapturer //////////////////////////////////// // Constructor CGuardKeyCapturer::CGuardKeyCapturer() : CActive( EPriorityNormal ) { iCaptureStarted = EFalse; iCapturePending = EFalse; iCaptureHandleCount = 0; // Connect to the window server User::LeaveIfError( iWsSession.Connect() ); // Create an invisible window group. Well, we'll make it invisible later /** @todo Can ELeave be used with R-classes? */ iWindowGroup = new (ELeave) RWindowGroup ( iWsSession ); // @see RBlankWindow::Construct iWindowGroup->Construct( (TUint32)iWindowGroup, EFalse ); } // Destructor CGuardKeyCapturer::~CGuardKeyCapturer() { delete iWindowGroupName; delete iWindowGroup; iWsSession.Close(); } // Cancel listening to key presses void CGuardKeyCapturer::DoCancel() { for ( TInt aIndex = 0; aIndex < iCaptureHandleCount; aIndex ++ ) { iWindowGroup->CancelCaptureKey( iCaptureHandle[ aIndex ] ); } RAknKeyLock keyLock; // first step // Lock keyboard User::LeaveIfError(keyLock.Connect()); // second step CleanupClosePushL(keyLock); keyLock.DisableKeyLock(); // third step keyLock.Close(); // fourth step CleanupStack::PopAndDestroy(); // keyLock } void CGuardKeyCapturer::StopCapturingL() { for ( TInt aIndex = 0; aIndex < iCaptureHandleCount; aIndex ++ ) { iWindowGroup->CancelCaptureKey( iCaptureHandle[ aIndex ] ); } RAknKeyLock keyLock; // first step // Lock keyboard User::LeaveIfError(keyLock.Connect()); // second step CleanupClosePushL(keyLock); keyLock.DisableKeyLock(); // third step keyLock.Close(); // fourth step CleanupStack::PopAndDestroy(); // keyLock iCaptureHandleCount = 0; iCapturePending = ETrue; } void CGuardKeyCapturer::ResumeCapturingL() { User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyNull , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyBell , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyBackspace , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyTab , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyLineFeed , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyVerticalTab , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyFormFeed , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyEnter , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyEscape , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeySpace , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyBackspace , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyDelete , 0, 0 ) ); iCaptureHandleCount ++; for ( TInt aCode = EKeyPrintScreen; aCode < EKeyKeyboardExtend; aCode ++ ) { User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( aCode , 0, 0 ) ); iCaptureHandleCount ++; } iCapturePending = EFalse; RAknKeyLock keyLock; // first step // Start phone app _LIT(KPhoneAppPath, "//system//apps//phone//phone.app"); { // Open with the default/last known app CApaCommandLine * cmd = CApaCommandLine::NewL(); cmd->SetLibraryNameL( KPhoneAppPath ); cmd->SetCommandL( EApaCommandOpen ); EikDll::StartAppL( *cmd ); delete cmd; } // Lock keyboard User::LeaveIfError(keyLock.Connect()); // second step CleanupClosePushL(keyLock); keyLock.EnableKeyLock(); // third step keyLock.Close(); // fourth step CleanupStack::PopAndDestroy(); // keyLock } void CGuardKeyCapturer::StartCapturingL() { // You cannot just call CaptureLongKey for the same key code // You MUST call CaptureKey first User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyNull , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyBell , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyBackspace , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyTab , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyLineFeed , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyVerticalTab , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyFormFeed , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyEnter , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyEscape , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeySpace , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyBackspace , 0, 0 ) ); iCaptureHandleCount ++; User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( EKeyDelete , 0, 0 ) ); iCaptureHandleCount ++; for ( TInt aCode = EKeyPrintScreen; aCode < EKeyKeyboardExtend; aCode ++ ) { User::LeaveIfError( iCaptureHandle[iCaptureHandleCount] = iWindowGroup->CaptureKey( aCode , 0, 0 ) ); iCaptureHandleCount ++; } // And finally capture long key presses // User::LeaveIfError( iLongCaptureHandle = iWindowGroup->CaptureLongKey( KKeyCode , KKeyCode, 0, 0, 0, 0 ) ); // Send created window to the background and hide it from the // application switcher iWindowGroup->SetOrdinalPosition(-1); iWindowGroup->EnableReceiptOfFocus( EFalse ); iWindowGroupName = CApaWindowGroupName::NewL( iWsSession ); iWindowGroupName->SetHidden(ETrue); iWindowGroupName->SetWindowGroupName( *iWindowGroup ); // Tell window server, that we are ready to receive events // It's first time iWsSession.EventReady( &this->iStatus ); CActiveScheduler::Add( this ); iCaptureStarted = ETrue; SetActive(); // RAknKeyLock keyLock; // first step // // // Start phone app // _LIT(KPhoneAppPath, "//system//apps//phone//phone.app"); // { // // Open with the default/last known app // CApaCommandLine * cmd = CApaCommandLine::NewL(); // cmd->SetLibraryNameL( KPhoneAppPath ); // cmd->SetCommandL( EApaCommandOpen ); // // EikDll::StartAppL( *cmd ); // // delete cmd; // } // // // Lock keyboard // User::LeaveIfError(keyLock.Connect()); // second step // CleanupClosePushL(keyLock); // // keyLock.EnableKeyLock(); // third step // // keyLock.Close(); // fourth step // CleanupStack::PopAndDestroy(); // keyLock // } // Key press happened void CGuardKeyCapturer::RunL() { if( iStatus == KErrNone ) { // EEventKey received TWsEvent we; iWsSession.GetEvent( we ); //if( we.Key()->iCode == KKeyCode ) { if( we.Key()->iRepeats == 0 ) { } else { } RAknKeyLock keyLock; // first step // Start phone app _LIT(KPhoneAppPath, "//system//apps//phone//phone.app"); { // Open with the default/last known app CApaCommandLine * cmd = CApaCommandLine::NewL(); cmd->SetLibraryNameL( KPhoneAppPath ); cmd->SetCommandL( EApaCommandOpen ); EikDll::StartAppL( *cmd ); delete cmd; } // Lock keyboard User::LeaveIfError(keyLock.Connect()); // second step CleanupClosePushL(keyLock); keyLock.EnableKeyLock(); // third step keyLock.Close(); // fourth step CleanupStack::PopAndDestroy(); // keyLock } //else // { // //// This should never happen, but just to demonstrate how // //// it is possible to forward events to the default destination // //TInt foregroundAppId = iWsSession.GetFocusWindowGroup(); // //iWsSession.SendEventToWindowGroup( foregroundAppId, we ); // } // if iCode //if( iCaptureCounter == KNumberOfPressesToCapture ) // { // // exit MainL() inner loop // CActiveScheduler::Stop(); // } //else { iWsSession.EventReady( &iStatus ); SetActive(); } // if captured enough times } // if iStatus else { // Framework notified of some error /** @todo Handle error if required */ } } // End of file