繁体   English   中英

在C#中获取USB设备句柄

[英]Getting USB device handle in C#

我正在尝试为KSP创建一个插件,以允许在游戏中使用X52 Pro游戏杆的MFD功能。

但是我遇到了一个问题,我不知道如何操纵操纵杆的设备手柄。

有谁知道我该如何处理设备?

您可以在Window C#Winforms类中继承WinProc类,因为message.LParam和message.RParam是HID句柄,其语义类型类似于MFC。

使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Windows.Forms; 使用System.Runtime.InteropServices; 使用Microsoft.Win32;

命名空间WindowsFormsApplicationJoyStick {公共类Program:RichTextBox {

    /// <summary>
    /// Function to retrieve raw input data.
    /// </summary>
    /// <param name="hRawInput">Handle to the raw input.</param>
    /// <param name="uiCommand">Command to issue when retrieving data.</param>
    /// <param name="pData">Raw input data.</param>
    /// <param name="pcbSize">Number of bytes in the array.</param>
    /// <param name="cbSizeHeader">Size of the header.</param>
    /// <returns>0 if successful if pData is null, otherwise number of bytes if pData is not null.</returns>

    [DllImport("User32.dll")]
    extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);


    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)WindowMessages.RawInput)  // WindowMessages.RawInput = 0x00FF (WM_INPUT)
        {
            RAWINPUT input = new RAWINPUT();
            int outSize = 0;
            int size = Marshal.SizeOf(typeof(RAWINPUT));

            outSize = Win32API.GetRawInputData(m.LParam, RawInputCommand.Input, out input, ref size, Marshal.SizeOf(typeof(RAWINPUTHEADER)));
            if (outSize != -1)
            {
                if (input.Header.Type == RawInputType.Joystick)
                {
                          // Output X and Y coordinates of Joystick movements                  
               }
        }
        base.WndProc(ref m);
    }

}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM