Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Controlling Hardware Buttons on Pocket PC

0.00/5 (No votes)
12 Feb 2006 1  
Controlling hardware buttons, the easy way...

Introduction

Have you ever tried to control the hardware buttons on your PPC? Seen the Input example in the article on MSDN and still have no idea what's going on? Well then, here I come to the rescue.

Background

Quite why MS can not make their examples simple enough for anyone to use I have no idea. I Googled for a simple example, but even after looking at every single link, I found no such thing, and so decided that maybe I should write about it. I did see a few cryptic responses in the newsgroups which pointed in the right direction, but actual examples are so much easier to digest.

I was going to put this article up on my site, but decided that CodeProject gets so many more users that it would be more beneficial here. The code is so simple that I don't believe it is necessary for me to convert it to C#, but if I do, then I will probably find a home for it on my site.

Using the Code

Well, it couldn't be any easier to use. Firstly, you'll need to add a reference to Microsoft.WindowsCE.Forms. Next, create an instance of the class:

Private WithEvents KeyMessages As New HardwareKeyMessageWindow()

Then respond to the HardwareKeyPressed() messages:

Private Sub KeyMessages_HardwareKeyPressed(ByVal sender As Object, _
            ByVal ke As HardwareKeyPressEventArgs) _
            Handles KeyMessages.HardwareKeyPressed
      Select Case ke.KeyID
          Case HardwareKey1
              'Do something
          Case HardwareKey2
              'Do something else
          Case HardwareKey3
              'Do some other thing
          Case HardwareKey4
              'Do nothing
          Case HardwareKey5
              'Throw General error stating that this button should not be pressed under any circumstances ;)
      End Select
End Sub

Points of Interest

Just to make the sample complete, I built a simple Etch-A-Sketch application. It's just a pity that there's no method to detect shaking :)

This is written in VS2003, but I expect it should work in VS2002.

I have tested the app on a Pocket PC running Windows for PocketPC 2002 (free gift of Viewsonic V37 from those nice people at MS) and Windows for PocketPC 2003 (NavMan PIN 570).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here