View a printable version of the current page.
  Wiki > Symbian Developer Network Public Wiki > ... > Example source code > Simulate D-Pad with Accelerometer
  Simulate D-Pad with Accelerometer
Added by simonfrost, last edited by rodders on Feb 15, 2008  (view change)
Labels: 
(None)

Header File:
#include <RDAccelerometerObserver.h>
class CRDAccelerometer;

const TInt KThresholdDown = 20;
const TInt KThresholdUp = -10;
const TInt KThresholdRight = 15;
const TInt KThresholdLeft = -15;
const TInt KPadding = 2;


class CMyAppView : public CCoeControl, public MRDAccelerometerObserver {
	public:
		void ConstructL(const TRect& aRect);
		void HandleAccelerationL(TInt aX, TInt aY, TInt aZ);

	private:
		CRDAccelerometer* iAccelerometer;
		TBool iLeft, iRight, iUp, iDown;
};
CPP File:
void CMyAppView::ConstructL(const TRect& aRect)
	{
	iAccelerometer = CRDAccelerometer::NewL(*this);
	iLeft = iRight = iUp = iDown = false;
	}

void CMyAppView::HandleAccelerationL(TInt aX, TInt aY, TInt aZ)
	{
	TRawEvent keyX;
	keyX.Set(TRawEvent::ENone);
	if(aX < KThresholdRight - KPadding && aX > KThresholdLeft + KPadding)
		{
		if(iRight)
			keyX.Set(TRawEvent::EKeyUp, EStdKeyRightArrow);
		else if(iLeft)
			keyX.Set(TRawEvent::EKeyUp, EStdKeyLeftArrow);
		iRight = iLeft = false;
		}
	else
		{
		if(!iRight && aX > KThresholdRight + KPadding)
			{
			keyX.Set(TRawEvent::EKeyDown, EStdKeyRightArrow);
			iRight = true;
			}
		else if(!iLeft && aX < KThresholdLeft - KPadding)
			{
			keyX.Set(TRawEvent::EKeyDown, EStdKeyLeftArrow);
			iLeft = true;
			}
		}
	UserSvr::AddEvent(keyX);


	TRawEvent keyY;
	keyY.Set(TRawEvent::ENone);
	if(aY < KThresholdDown - KPadding && aY > KThresholdUp + KPadding)
		{
		if(iDown)
			keyY.Set(TRawEvent::EKeyUp, EStdKeyDownArrow);
		else if(iUp)
			keyY.Set(TRawEvent::EKeyUp, EStdKeyUpArrow);
		iDown = iUp = false;
		}
	else
		{
		if(!iDown && aY > KThresholdDown + KPadding)
			{
			keyY.Set(TRawEvent::EKeyDown, EStdKeyDownArrow);
			iDown = true;
			}
		else if(!iUp && aY < KThresholdUp - KPadding)
			{
			keyY.Set(TRawEvent::EKeyDown, EStdKeyUpArrow);
			iUp = true;
			}
		}
	UserSvr::AddEvent(keyY);
	}
Notes:

This is not a complete code sample, and only shows the relevant parts.
Tested on the N82, N95 and N95 8GB, checks may be needed to prevent crashing on other devices.

Interactive Services Terms & Conditions of use | Terms of use | Privacy policy | Media Center | Contact us | © 2008 Symbian