Click here to Skip to main content
15,888,610 members
Articles / Mobile Apps / Windows Mobile

Controlling Hardware Buttons on Pocket PC

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
12 Feb 2006CPOL1 min read 36.7K   245   21   2
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:

VB
Private WithEvents KeyMessages As New HardwareKeyMessageWindow()

Then respond to the HardwareKeyPressed() messages:

VB
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSuggestion Pin
Shaun Wilde12-Feb-06 9:30
Shaun Wilde12-Feb-06 9:30 
GeneralRe: Suggestion Pin
Mick Doherty12-Feb-06 11:32
Mick Doherty12-Feb-06 11:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.