簡體   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