November 22, 2014
Hot Topics:

Using Palm Device Hardware Buttons in Your Application

  • June 18, 2003
  • By Alex Gusev
  • Send Email »
  • More Articles »

No doubts, almost each developer has faced the problem of impossibility to put as many controls on the screen as he might need to due to screen size limit or some other reasons. Fortunately, the situaltion may be significantly improved, because each Palm device has five hardware buttons (four ones are used to shortly call standard applications plus Up/Down button) and four "silk" buttons (Find, Calc, Menu and AppLauncher). Good news is that you may hook them in your own programs making application more powerful and convenient. There are two ways to achieve our target: either intercept keyboard events or process user input. Below we will discuss how to do it.

Method I: Event interception

The very first candidate to be selected to implement what we're dreaming is EventLoop function. As you remember, standard PilotMain calls AppEventLoop:

UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
	Err error;
	error = RomVersionCompatible (ourMinVersion, launchFlags);
	if (error) return (error);
	switch (cmd)
	{
		case sysAppLaunchCmdNormalLaunch:
			error = AppStart();
			if (error)
				return error;
			FrmGotoForm(MainForm);
			AppEventLoop();
			AppStop();
			break;
	}
	return errNone;
}

where AppEventLoop usually looks like

static void AppEventLoop(void)
{
	UInt16 error;
	EventType event;

	do
	{
		/* change timeout if you need periodic nilEvents */
		EvtGetEvent(&event, evtWaitForever);

		if (! SysHandleEvent(&event))
		{
			if (! MenuHandleEvent(0, &event, &error))
			{
				if (! AppHandleEvent(&event))
				{
					FrmDispatchEvent(&event);
				}
			}
		}
	} while (event.eType != appStopEvent);
}

As you may see, after EvtGetEvent has returned next event you may manage it as you want to, i.e. it's totally your responsibility to deliver it to currently active form or handle it by some another way. Here is the point. All we should do is to 'preprocess' keyDownEvent to get desired functionality as it is done in the following sample:

static void AppEventLoop(void)
{
	UInt16 error;
	EventType event;

	do
	{
		/* change timeout if you need periodic nilEvents */
		EvtGetEvent(&event, evtWaitForever);

                if ( PreprocessEvent(&event) )
                   continue;

		if (! SysHandleEvent(&event))
		{
			if (! MenuHandleEvent(0, &event, &error))
			{
				if (! AppHandleEvent(&event))
				{
					FrmDispatchEvent(&event);
				}
			}
		}
	} while (event.eType != appStopEvent);
}

Now let's consider PreprocessEvent function. Our goal is to hook keyboard events, so following sample does deals with one event only. Actually, such approach may be used to handle all events occuring during application's life.

// Return true if event was processed and false otherwise
Boolean PreprocessEvent (EventPtr event)
{
	if (event->eType == keyDownEvent)
	{
		if (event->data.keyDown.modifiers & commandKeyMask)
		{
			if (TxtCharIsHardKey(event->data.keyDown.modifiers,event->data.keyDown.chr))
			{
				switch (event->data.keyDown.chr)
				{
				case vchrHard1:
				case vchrHard2:
				case vchrHard3:
				case vchrHard4:
					FrmDispatchEvent (event);
					return true;
				default:
					return false;
				}
			}
			if ( event->data.keyDown.chr == vchrFind )
			{
				FrmDispatchEvent(event);
				return true;
			}
			if ( event->data.keyDown.chr == vchrCalc )
			{
				FrmDispatchEvent(event);
				return true;
			}

			if (event->data.keyDown.chr == pageUpChr ||
				event->data.keyDown.chr == sendDataChr ||
				event->data.keyDown.chr == pageDownChr)
			{
				FrmDispatchEvent (event);
				return true;
			}
			
			return false;
		}
	}
	return false;
}

As a result all events will be dispatched to current form, where you may process them as needed.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel