using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using HidSharp;
using HidSharp.Utility;
using HidSharp.Reports;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
HidSharpDiagnostics.EnableTracing = true;
HidSharpDiagnostics.PerformStrictChecks = true;
DeviceList list = DeviceList.Local;
list.Changed += (sender, e) => Console.WriteLine("Device list changed.");
HidDevice[] hidDeviceList = list.GetHidDevices().ToArray();
foreach (HidDevice dev in hidDeviceList)
{
if (!dev.ToString().Contains("gamepad"))
{
Console.WriteLine(dev.GetProductName() + " is not a gamepad.");
continue;
}
try
{
ReportDescriptor reportDescriptor = dev.GetReportDescriptor();
Debug.Assert(dev.GetMaxInputReportLength() == reportDescriptor.MaxInputReportLength);
Debug.Assert(dev.GetMaxOutputReportLength() == reportDescriptor.MaxOutputReportLength);
Debug.Assert(dev.GetMaxFeatureReportLength() == reportDescriptor.MaxFeatureReportLength);
foreach (DeviceItem deviceItem in reportDescriptor.DeviceItems)
{
{
HidStream hidStream;
if (dev.TryOpen(out hidStream))
{
Console.WriteLine(dev.GetProductName() + " opened.");
hidStream.ReadTimeout = Timeout.Infinite;
using (hidStream)
{
byte[] inputReportBuffer = new byte[dev.GetMaxInputReportLength()];
HidSharp.Reports.Input.HidDeviceInputReceiver inputReceiver =
reportDescriptor.CreateHidDeviceInputReceiver();
HidSharp.Reports.Input.DeviceItemInputParser inputParser =
deviceItem.CreateDeviceItemInputParser();
inputReceiver.Start(hidStream);
while (true)
{
if (inputReceiver.WaitHandle.WaitOne(1000))
{
if (!inputReceiver.IsRunning) { break; } // Disconnected?
Report report;
while (inputReceiver.TryRead(inputReportBuffer, 0, out report))
{
if (inputParser.TryParseReport(inputReportBuffer, 0, report) && inputParser.HasChanged)
{
//inputParser.HasChanged는 버튼을 누르거나 놓는 등 상태가 변경되는 경우를 뜻한다.
//Console.WriteLine(BitConverter.ToString(inputReportBuffer));
if (inputReportBuffer[3] == 0x00)
{
Console.WriteLine("Left");
}
else if (inputReportBuffer[3] == 0xFF)
{
Console.WriteLine("Right");
}
else if (inputReportBuffer[4] == 0x00)
{
Console.WriteLine("Up");
}
else if (inputReportBuffer[4] == 0xFF)
{
Console.WriteLine("Down");
}
else if (inputReportBuffer[5] == 0x1F)
{
Console.WriteLine("X");
}
else if (inputReportBuffer[5] == 0x2F)
{
Console.WriteLine("A");
}
else if (inputReportBuffer[5] == 0x4F)
{
Console.WriteLine("B");
}
else if (inputReportBuffer[5] == 0x8F)
{
Console.WriteLine("Y");
}
else if (inputReportBuffer[6] == 0x01)
{
Console.WriteLine("L Shoulder");
}
else if (inputReportBuffer[6] == 0x02)
{
Console.WriteLine("R Shoulder");
}
else if (inputReportBuffer[6] == 0x10)
{
Console.WriteLine("Select");
}
else if (inputReportBuffer[6] == 0x20)
{
Console.WriteLine("Start");
}
}
}
}
if (Console.KeyAvailable)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
if (consoleKeyInfo.Key == ConsoleKey.Escape)
{
Console.WriteLine("User stopped.");
break;
//hidStream.Close();
}
}
}
}
Console.WriteLine("Gamepad closed.");
}
else
{
Console.WriteLine("Failed to open device.");
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}