⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 globalkeycapture.cpp

📁 s60平台下捕获全局按键的方法--含源代码。对要捕获symbian下的全局按键有帮助。
💻 CPP
字号:
/*
* ============================================================================
*  Name     :  GlobalKeyCapture.cpp
*  Part of  : GlobalKeyCapture
*  Created  : 11.09.2005 by Artem Marchenko
*  Description:
*     GlobalKeyCapture.cpp - source file
*  Version  : 1.0
*  Copyright: 2005 Artem Marchenko
* ============================================================================
*/


//  Include Files  

#include "GlobalKeyCapture.h"
#include <e32base.h>
#include <e32std.h>
#include <e32cons.h>            // Console
#include <apgwgnam.h>           // CApaWindowGroupName


//  Constants
// Text strings created by wizard
_LIT( KTextConsoleTitle, "Console" );
_LIT( KTextFailed, " failed, leave code = %d" );
_LIT( KTextPressAnyKey, "\n[press any key to exit]\n" );

// Key to listen to 
const TUint KKeyCode = EKeyLeftArrow;

// Capture KNumberOfPressesToCapture key presses
const TInt KNumberOfPressesToCapture = 3;

//  Global Variables
// write all messages to this console
LOCAL_D CConsoleBase* console;  


//  Local Functions
LOCAL_C void MainL(const TDesC& /*aArgs*/)
    {
	console->Write( _L( "Starting key capturing\n" ) );
	// Create capturer
	CGlobalCapturer* capturer = new (ELeave) CGlobalCapturer();
	CleanupStack::PushL( capturer );
	// And start capturing
	capturer->StartCapturingL();

	// In a real application you should use CActiveSchedulerWait,
	// but in this small demo application we know that there is just
	// a single inner loop and no CActiveSchedulerWait "protection" is
	// needed
	CActiveScheduler::Start();
	// Cleanup. Demo completed
	CleanupStack::PopAndDestroy( capturer );
    }

// Main function. Is TRAPD outside, in 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
	// Command line is not really needed for this demo, but
	// why not to demonstrate this either?
    TBuf<256> cmdLine;
    RProcess().CommandLine( cmdLine );
    MainL( cmdLine );

    // Delete active scheduler
    CleanupStack::PopAndDestroy(scheduler);
    }


//  Global Functions

GLDEF_C TInt Start()
    {
    // Create cleanup stack
    __UHEAP_MARK;
    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;
    __UHEAP_MARKEND;
    return KErrNone;
    }

////////////////////////////////////
// CGlobalCapturer
////////////////////////////////////

// Constructor
CGlobalCapturer::CGlobalCapturer() :
	CActive( EPriorityNormal )
	{
	// nothing
	}

// Destructor
CGlobalCapturer::~CGlobalCapturer()
	{
	delete iWindowGroupName;
	delete iWindowGroup;
	iWsSession.Close();
	}

// Cancel listening to key presses
void CGlobalCapturer::DoCancel()
	{
	iWindowGroup->CancelCaptureKey( iCaptureHandle );
	}

void CGlobalCapturer::StartCapturingL() 
	{
	// 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 );

	// Capture a key
	User::LeaveIfError( iCaptureHandle = iWindowGroup->CaptureKey( KKeyCode , 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
	iWsSession.EventReady( &this->iStatus );
	CActiveScheduler::Add( this );
	SetActive();
	}

// Key press happened
void CGlobalCapturer::RunL()
	{
	if( iStatus == KErrNone ) 
		{
		// EEventKey received
		console->Write( _L( "Captured key press\n" ) );
		TWsEvent we;
		iWsSession.GetEvent( we );

		if( we.Key()->iCode == KKeyCode ) 
			{
			console->Printf( _L( "Captured correct key press for the %i time\n" ), ++iCaptureCounter );
			}
		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 */
		}
	}

//  Exported Functions

#ifdef __WINS__
EXPORT_C TInt WinsMain(TAny* /*aParam*/)
    {
    return Start();
    }
#else
GLDEF_C TInt E32Main()
    {
    return Start();
    }
#endif

#ifdef __WINS__
TInt E32Dll(TDllReason /*aReason*/)
    {
    return KErrNone;
    }
#endif


// End of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -