EonaCat HID https://EonaCat.com
Go to file
EonaCat effcea8b4d Updated README.md 2025-07-14 20:26:13 +02:00
Analyzer Updated README.md 2025-07-14 20:26:13 +02:00
EonaCat.HID Updated README.md 2025-07-14 20:26:13 +02:00
EonaCat.HID.Console Updated README.md 2025-07-14 20:26:13 +02:00
.gitignore Initial commit 2024-06-11 16:54:07 +02:00
EonaCat.HID.sln Added console example 2025-07-12 22:59:27 +02:00
LICENSE Initial version 2024-06-11 16:57:27 +02:00
README.md Updated README.md 2025-07-14 20:26:13 +02:00
icon.png Moved icon 2024-06-11 16:59:09 +02:00

README.md

EonaCat.HID

Cross platform library for HID. Works on Windows, Linux and macOS.

Example Code:

using EonaCat.HID;
using EonaCat.HID.Models;
using System.Globalization;

namespace EonaCat.HID.Example
{
    // This file is part of the EonaCat project(s) which is released under the Apache License.
    // See the LICENSE file or go to https://EonaCat.com/license for full license details.

    public class Program
    {
        static IHidManager _deviceManager;
        static IHid _device;
        static IEnumerable<IHid> _deviceList;

        static async Task Main(string[] args)
        {
            try
            {
                _deviceManager = HidFactory.CreateDeviceManager();
                if (_deviceManager == null)
                {
                    Console.WriteLine("Failed to create HID manager.");
                    return;
                }

                _deviceManager.OnDeviceInserted += (s, e) =>
                {
                    Console.WriteLine($"Inserted Device --> VID: {e.Device.VendorId:X4}, PID: {e.Device.ProductId:X4}");
                };

                _deviceManager.OnDeviceRemoved += (s, e) =>
                {
                    Console.WriteLine($"Removed Device --> VID: {e.Device.VendorId:X4}, PID: {e.Device.ProductId:X4}");
                };

                RefreshDevices();

                if (!_deviceList.Any())
                {
                    Console.WriteLine("No HID found.");
                    return;
                }

                DisplayDevices();

                Console.Write("Select a device index to connect: ");
                int index = int.Parse(Console.ReadLine());
                _device = _deviceList.ElementAt(index);

                _device.OnDataReceived += (s, e) =>
                {
                    Console.WriteLine($"Rx Data: {BitConverter.ToString(e.Report.Data)}");
                };

                _device.OnError += (s, e) =>
                {
                    Console.WriteLine($"Error: {e.Exception.Message}");
                };

                _device.Open();
                Console.WriteLine($"Connected to {_device.ProductName}");

                await _device.StartListeningAsync(default);

                Console.WriteLine("Listening... Press [Enter] to send test output report.");
                Console.ReadLine();

                // Example: Send output report using HidReport
                var data = ByteHelper.HexStringToByteArray("01-02-03");
                var reportId = (byte)0x00; // Report ID
                var outputReport = new HidReport(reportId, data);

                await _device.WriteOutputReportAsync(outputReport);
                Console.WriteLine($"Sent output report: Report ID: {reportId}, Data: {BitConverter.ToString(data)}");

                Console.WriteLine("Press [Enter] to exit...");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[EXCEPTION] {ex.Message}");
            }
        }

        static void RefreshDevices(ushort? vendorId = null, ushort? productId = null)
        {
            _deviceList = _deviceManager.Enumerate(vendorId, productId);
        }

        static void DisplayDevices()
        {
            int i = 0;
            foreach (var dev in _deviceList)
            {
                Console.WriteLine($"[{i++}] {dev.ProductName} | VID:PID = {dev.VendorId:X4}:{dev.ProductId:X4}");
            }
        }
    }

    public static class ByteHelper
    {
        public static byte[] HexStringToByteArray(string hex)
        {
            return hex
                .Replace("-", "")
                .Replace(" ", "")
                .ToUpper()
                .Where(c => Uri.IsHexDigit(c))
                .Select((c, i) => new { c, i })
                .GroupBy(x => x.i / 2)
                .Select(g => byte.Parse($"{g.First().c}{g.Last().c}", NumberStyles.HexNumber))
                .ToArray();
        }
    }
}