[Windows Mobile Development and Hacking General] petevick:
Hi,

Paste the following code into a module, and call in in the form_load as follows:-

Dim messageWindow As Hwbuttons.hwMessageWindow

Me.messageWindow = New Hwbuttons.hwMessageWindow(Me)

RegisterHKeys.RegisterRecordKey(Me.messageWindow.Hwnd)

The original code for this comes from www.opennetcf.org - I modified it for my needs, so kudos to the original poster(s).

I have still not found a way to trap the 'green and red' keys :(

Pete

Imports Microsoft.WindowsCE.Forms

Imports System.Runtime.InteropServices

Public Enum KeyModifiers As Integer

None = 0

Alt = 1

Control = 2

Shift = 4

Windows = 8

Modkeyup = &H1000

End Enum

Public Enum KeysHardware As Integer

Hardware1 = 193

Hardware2 = 194

Hardware3 = 195

Hardware4 = 196

Hardware5 = 202

End Enum

Public Module RegisterHKeys

<DllImport("coredll.dll", Entrypoint:="RegisterHotKey", setLastError:=True)> _

Public Function RegisterHotKey( _

ByVal hWnd As IntPtr, _

ByVal id As Integer, _

ByVal Modifiers As KeyModifiers, _

ByVal key As Integer) As Boolean

End Function

<DllImport("coredll.dll")> _

Private Function UnregisterFunc1( _

ByVal modifiers As KeyModifiers, _

ByVal keyID As Integer) As Boolean

End Function

Public Sub RegisterRecordKey(ByVal hWnd As IntPtr)

UnregisterFunc1(KeyModifiers.Windows, CType(KeysHardware.Hardware1, Integer))

RegisterHotKey(hWnd, CType(KeysHardware.Hardware1, Integer), KeyModifiers.Windows, CType(KeysHardware.Hardware1, Integer))

UnregisterFunc1(KeyModifiers.Windows, CType(KeysHardware.Hardware2, Integer))

RegisterHotKey(hWnd, CType(KeysHardware.Hardware2, Integer), KeyModifiers.Windows, CType(KeysHardware.Hardware2, Integer))

UnregisterFunc1(KeyModifiers.Windows, CType(KeysHardware.Hardware3, Integer))

RegisterHotKey(hWnd, CType(KeysHardware.Hardware3, Integer), KeyModifiers.Windows, CType(KeysHardware.Hardware3, Integer))

UnregisterFunc1(KeyModifiers.Windows, CType(KeysHardware.Hardware4, Integer))

RegisterHotKey(hWnd, CType(KeysHardware.Hardware4, Integer), KeyModifiers.Windows, CType(KeysHardware.Hardware4, Integer))

UnregisterFunc1(KeyModifiers.Windows, CType(KeysHardware.Hardware5, Integer))

RegisterHotKey(hWnd, CType(KeysHardware.Hardware5, Integer), KeyModifiers.Windows, CType(KeysHardware.Hardware5, Integer))

End Sub

Public Class Hwbuttons

Inherits System.Windows.Forms.Form

Public Shared messageWindow As hwMessageWindow

Public Class hwMessageWindow

Inherits messageWindow

Public Const WM_HOTKEY = &H312

Dim example As frmComms

Public Sub New(ByVal example As frmComms)

Me.example = example

End Sub

Protected Overrides Sub WndProc(ByRef msg As Message)

Select Case msg.Msg

Case WM_HOTKEY

'ButtonPressed(msg.WParam.ToInt32())

Return

End Select

MyBase.WndProc(msg)

End Sub

Public Sub ButtonPressed(ByVal button As Integer)

Select Case button

Case KeysHardware.Hardware1

MessageBox.Show("Button 1 pressed!")

Exit Sub

Case KeysHardware.Hardware2

MessageBox.Show("Button 2 pressed!")

Exit Sub

Case KeysHardware.Hardware3

MessageBox.Show("Button 3 pressed!")

Exit Sub

Case KeysHardware.Hardware4

MessageBox.Show("Button 4 pressed!")

Exit Sub

Case KeysHardware.Hardware5

MessageBox.Show("Button 5 pressed!")

Exit Sub

End Select

End Sub

End Class

End Class

End Module