Code for MCE remote (1 Viewer)

A

Anonymous

Guest
Fond this code, havent tested it do I don't got my MCE remote yeat.

Code:
Imports System.Runtime.InteropServices

Public Enum MCERemoteButtons
    Unknown
    '-- WM_KEYDOWN type buttons (easy)
    OK
    MoveUp
    MoveLeft
    MoveRight
    MoveDown
    Back
    Number1
    Number2
    Number3
    Number4
    Number5
    Number6
    Number7
    Number8
    Number9
    Number0
    Clear
    Enter
    '-- WM_APPCOMMAND type buttons
    Pause
    Play
    Record
    Replay
    Rewind
    Forward
    Skip
    ChannelDown
    ChannelUp
    [Stop]
    '-- WM_INPUT (HID) type buttons
    MoreInfo
    DVDAngle
    DVDAudio
    DVDMenu
    DVDSubtitle
    Guide
    Mute
    MyTV
    MyMusic
    MyPictures
    MyVideos
    RecordedTV
    OEM1
    OEM2
    LiveTV
    VolumeDown
    VolumeUp
End Enum

Public Class MCERemote
    Inherits System.ComponentModel.Component

#Region " Windows Form Designer generated code "

    Public Sub New(ByVal Container As System.ComponentModel.IContainer)
        MyClass.New()
        'Required for Windows.Forms Class Composition Designer support
        Container.Add(Me)
    End Sub

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
    End Sub

    'Component overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

#End Region

    Private Const WM_KEYDOWN As Integer = &H100
    Private Const WM_APPCOMMAND As Integer = &H319
    Private Const WM_INPUT As Integer = &HFF
    Private Const RID_INPUT As Integer = &H10000003
    Private Const RIM_TYPEMOUSE As Integer = 0
    Private Const RIM_TYPEKEYBOARD As Integer = 1
    Private Const RIM_TYPEHID As Integer = 2

    Private _blnDebugMode As Boolean = True
    Private _button As MCERemoteButtons
    Private _intChar As Integer

    Public Event ButtonPressed(ByVal Button As MCERemoteButtons)

#Region "Win32 API Declarations "

    'BOOL RegisterRawInputDevices {
    '    PCRAWINPUTDEVICE pRawInputDevices,
    '    UINT uiNumDevices,
    '    UINT cbSize
    ');
    <DllImportAttribute("user32.dll")> _
    Private Shared Function RegisterRawInputDevices _
            (ByVal RIDs() As RAWINPUTDEVICE, _
            ByVal uiNumDevices As Integer, _
            ByVal cbSize As Integer) As Boolean
    End Function

    'UINT GetRawInputData {
    '    HRAWINPUT hRawInput,
    '    UINT uiCommand,
    '    LPVOID pData,
    '    PUINT pcbSize,
    '    UINT cbSizeHeader
    '};
    <DllImportAttribute("user32.dll")> _
    Private Shared Function GetRawInputData _
        (ByVal hRawInput As IntPtr, _
        ByVal uiCommand As Integer, _
        ByVal pData As IntPtr, _
        ByRef byRefpcbSize As Integer, _
        ByVal cbSizeHeader As Integer) As Integer
    End Function

    'typedef struct tagRAWINPUTDEVICE {
    '    USHORT usUsagePage;
    '    USHORT usUsage;
    '    DWORD dwFlags;
    '    HWND hwndTarget;
    '} RAWINPUTDEVICE, *PRAWINPUTDEVICE, *LPRAWINPUTDEVICE;
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure RAWINPUTDEVICE
        <MarshalAs(UnmanagedType.U2)> _
        Public usUsagePage As Short
        <MarshalAs(UnmanagedType.U2)> _
        Public usUsage As Short
        <MarshalAs(UnmanagedType.U4)> _
        Public dwFlags As Integer
        Public hwndTarget As IntPtr
    End Structure

    'typedef struct tagRAWINPUT { 
    '    RAWINPUTHEADER    header; 
    '    union {
    '             RAWMOUSE    mouse; 
    '             RAWKEYBOARD keyboard; 
    '             RAWHID      hid; 
    '            } data;
    '} RAWINPUT, *PRAWINPUT; *LPRAWINPUT;
    Private Structure RAWINPUT
        Dim header As RAWINPUTHEADER
        Dim hid As RAWHID
    End Structure

    'typedef struct tagRAWINPUTHEADER {
    '   DWORD dwType;
    '   DWORD dwSize;
    '   HANDLE hDevice;
    '   WPARAM wParam;
    '} RAWINPUTHEADER, *PRAWINPUTHEADER;
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure RAWINPUTHEADER
        <MarshalAs(UnmanagedType.U4)> _
        Dim dwType As Integer
        <MarshalAs(UnmanagedType.U4)> _
        Dim dwSize As Integer
        Dim hDevice As IntPtr
        <MarshalAs(UnmanagedType.U4)> _
        Dim wParam As Integer
    End Structure

    'typedef struct tagRAWHID {
    '    DWORD dwSizeHid;
    '    DWORD dwCount;
    '    BYTE bRawData;
    '} RAWHID, **LPRAWHID;
    Private Structure RAWHID
        Dim dwSizeHid As Integer
        Dim dwCount As Integer
        ' Followed by bRawData As Byte() (variable size)
    End Structure

#End Region

#Region "Button Lookups"

    Private Function LookupKey(ByVal intKeyCode As Integer) As MCERemoteButtons
        Select Case intKeyCode
            Case 9
                Return MCERemoteButtons.Unknown
            Case 13
                Return MCERemoteButtons.OK
            Case 27
                Return MCERemoteButtons.Clear
            Case 37
                Return MCERemoteButtons.MoveLeft
            Case 38
                Return MCERemoteButtons.MoveUp
            Case 39
                Return MCERemoteButtons.MoveRight
            Case 40
                Return MCERemoteButtons.MoveDown
            Case 48
                Return MCERemoteButtons.Number0
            Case 49
                Return MCERemoteButtons.Number1
            Case 50
                Return MCERemoteButtons.Number2
            Case 51
                Return MCERemoteButtons.Number3
            Case 52
                Return MCERemoteButtons.Number4
            Case 53
                Return MCERemoteButtons.Number5
            Case 54
                Return MCERemoteButtons.Number6
            Case 55
                Return MCERemoteButtons.Number7
            Case 56
                Return MCERemoteButtons.Number8
            Case 57
                Return MCERemoteButtons.Number9
            Case 58
                Return MCERemoteButtons.Number0
            Case Else
                Debug.WriteLine("unknown keycode = " + intKeyCode.ToString)
                Return MCERemoteButtons.Unknown
        End Select
    End Function

    Private Function LookupAppCommand(ByVal intAppCode As Integer) As MCERemoteButtons
        Select Case intAppCode
            Case 65536
                Return MCERemoteButtons.Back
            Case 720896
                Return MCERemoteButtons.Skip
            Case 786432
                Return MCERemoteButtons.Replay
            Case 851968
                Return MCERemoteButtons.[Stop]
            Case 271450112
                Return MCERemoteButtons.Play
            Case 271515648
                Return MCERemoteButtons.Pause
            Case 271581184
                Return MCERemoteButtons.Record
            Case 271646720
                Return MCERemoteButtons.Forward
            Case 271712256
                Return MCERemoteButtons.Rewind
            Case 271777792
                Return MCERemoteButtons.ChannelUp
            Case 271843328
                Return MCERemoteButtons.ChannelDown
            Case Else
                'Debug.WriteLine("unknown appcommand " + intAppCode.ToString)
                Return MCERemoteButtons.Unknown
        End Select
    End Function

    Private Function LookupRaw(ByVal intRawByte1 As Byte, ByVal intRawByte2 As Byte) As MCERemoteButtons
        Select Case intRawByte2
            Case 9
                Return MCERemoteButtons.MoreInfo
            Case 36
                If intRawByte1 = 1 Then
                    Return MCERemoteButtons.DVDMenu
                Else
                    Return MCERemoteButtons.Unknown
                End If
            Case 37
                Return MCERemoteButtons.LiveTV
            Case 70
                Return MCERemoteButtons.MyTV
            Case 71
                Return MCERemoteButtons.MyMusic
            Case 72
                Return MCERemoteButtons.RecordedTV
            Case 73
                Return MCERemoteButtons.MyPictures
            Case 74
                Return MCERemoteButtons.MyVideos
            Case 75
                Return MCERemoteButtons.DVDAngle
            Case 76
                Return MCERemoteButtons.DVDAudio
            Case 77
                Return MCERemoteButtons.DVDSubtitle
            Case 128
                Return MCERemoteButtons.OEM1
            Case 129
                Return MCERemoteButtons.OEM2
            Case 141
                Return MCERemoteButtons.Guide
            Case 226
                Return MCERemoteButtons.Mute
            Case 233
                Return MCERemoteButtons.VolumeUp
            Case 234
                Return MCERemoteButtons.VolumeDown
            Case Else
                'If intRawByte2 > 0 Then
                'Debug.WriteLine("Unknown rawcode " + intRawByte1.ToString + ":" + intRawByte2.ToString)
                'End If
                Return MCERemoteButtons.Unknown
        End Select
    End Function

#End Region

    Public Sub Initialize(ByVal hWnd As IntPtr)

        Dim objRID(1) As RAWINPUTDEVICE
        '-- vendor-defined
        objRID(0).usUsagePage = &HFFBCS
        objRID(0).usUsage = &H88
        objRID(0).dwFlags = 0
        objRID(0).hwndTarget = hWnd
        '-- consumer controls
        objRID(1).usUsagePage = &HC
        objRID(1).usUsage = &H1
        objRID(1).dwFlags = 0
        objRID(1).hwndTarget = hWnd
        If RegisterRawInputDevices(objRID, objRID.Length, Marshal.SizeOf(objRID(0))) Then
            Debug.WriteLine("raw input devices registered OK")
        Else
            Debug.WriteLine("Error registering raw input devices!")
        End If

    End Sub

    Public Function WndProcHandler(ByRef m As System.Windows.Forms.Message) As Boolean

        Select Case m.Msg
            Case WM_KEYDOWN
                _button = LookupKey(m.WParam.ToInt32)
                If _button <> MCERemoteButtons.Unknown Then
                    RaiseEvent ButtonPressed(_button)
                    Return True
                    If _blnDebugMode Then
                        Debug.WriteLine("code " + m.WParam.ToString + " = key " + _button.ToString)
                    End If
                End If

            Case WM_APPCOMMAND
                _button = LookupAppCommand(m.LParam.ToInt32)
                If _button <> MCERemoteButtons.Unknown Then
                    RaiseEvent ButtonPressed(_button)
                    Return True
                    If _blnDebugMode Then
                        Debug.WriteLine("code " + m.LParam.ToString + " = app " + _button.ToString)
                    End If
                End If

            Case WM_INPUT
                Dim pcbSize As Integer
                '-- get the size of the data returned
                GetRawInputData(m.LParam, RID_INPUT, IntPtr.Zero, pcbSize, Marshal.SizeOf(GetType(RAWINPUTHEADER)))
                Dim buffer As IntPtr
                Try
                    '-- copy the data to a buffer
                    buffer = Marshal.AllocHGlobal(pcbSize)
                    GetRawInputData(m.LParam, RID_INPUT, buffer, pcbSize, Marshal.SizeOf(GetType(RAWINPUTHEADER)))
                    '-- convert the buffer to header data structure
                    Dim raw As RAWINPUT
                    raw = CType(Marshal.PtrToStructure(buffer, GetType(RAWINPUT)), RAWINPUT)
                    If raw.header.dwType <> RIM_TYPEHID Then
                        Exit Try
                    End If
                    '-- get raw data to array of bytes
                    Dim bRawData(raw.hid.dwSizeHid - 1) As Byte
                    Dim pRawData As Integer
                    pRawData = buffer.ToInt32() + Marshal.SizeOf(GetType(RAWINPUT))
                    Marshal.Copy(New IntPtr(pRawData), bRawData, 0, raw.hid.dwSizeHid)
                    '-- look up button code from byte array
                    _button = LookupRaw(bRawData(0), bRawData(1))
                    If _button <> MCERemoteButtons.Unknown Then
                        RaiseEvent ButtonPressed(_button)
                        Return True
                        If _blnDebugMode Then
                            Debug.WriteLine("code " + bRawData(0).ToString + ":" + bRawData(1).ToString + " = " + _button.ToString)
                        End If
                    End If
                Finally
                    If Not buffer.Equals(IntPtr.Zero) Then Marshal.FreeHGlobal(buffer)
                End Try
        End Select
        Return False
    End Function

End Class
 

MrMario64

Retired Team Member
  • Premium Supporter
  • April 22, 2004
    822
    1
    48
    Home Country
    Netherlands Netherlands
    MCE remote allready works in MP
    And as we do not know where you found this code we won't put it in also.
    How do we know we may use it?
     

    jameson_uk

    Retired Team Member
  • Premium Supporter
  • January 27, 2005
    7,258
    2,528
    Birmingham
    Home Country
    United Kingdom United Kingdom
    There are two MS resources which are quite good
    http://msdn.microsoft.com/library/d...ml/usingtheremotecontroldevicewithwindows.asp
    and
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/altinputdevices.asp

    The second has some code similar to that shown. I have been trying to accomodate the MCE keyboard into Meedio and came across this thread whilst searching.

    BTW I have found out that the two keys on the keyboard which are not defined (My Radion and Messenger) are 0x50 and 0x69 respectively.

    If you have setup the full remote and keyboard mappings in MP, can you tell me the usage page and usage HID codes for the green start button and the teletext + colour buttons as can not find these anywhere.
     

    Users who are viewing this thread

    Top Bottom