USB/IP PROJECT를 이용해 C#으로 USB Keyboard + Mouse 가상 장치 만들기
이미 지난 글에서 설명한,
USB/IP PROJECT를 이용해 C#으로 USB Keyboard 가상 장치 만들기
; https://www.sysnet.pe.kr/2/0/12216
내용에다 Raspberry PI Zero로 다뤘던 키보드와 마우스의 Report Descriptor를 그대로 가져다 합치면,
Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠)
; https://www.sysnet.pe.kr/2/0/11369
아래와 같은 C# 소스 코드로 가상 마우스와 키보드를 구현할 수 있습니다.
using HelperExtension;
using System;
using System.Diagnostics;
using System.Threading;
using UsbipDevice;
namespace cs_hid_keyboardmouse
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("cs-hid-keyboardmouse");
            bool waitLocalHost = true;
            if (args.Length >= 1)
            {
                if (args[0] == "-w")
                {
                    waitLocalHost = false;
                }
            }
            int cxScreen = SafeMethods.GetSystemMetrics(SystemMetric.SM_CXVIRTUALSCREEN);
            int cyScreen = SafeMethods.GetSystemMetrics(SystemMetric.SM_CYVIRTUALSCREEN);
            Console.WriteLine($"CX: {cxScreen}");
            Console.WriteLine($"CY: {cyScreen}");
            byte[] reportBuffer = KeyboardMouseDescriptors.Report;
            using (Usbip device = new Usbip(UsbDescriptors.Device, KeyboardMouseDescriptors.Hid, reportBuffer))
            {
                MouseDevice mouse = new MouseDevice(device, reportBuffer, cxScreen, cyScreen);
                KeyboardDevice keyboard = new KeyboardDevice(device, reportBuffer);
                device.Run();
                if (waitLocalHost == true)
                {
                    Thread usbipServer = new Thread(usbipServer_Run);
                    usbipServer.IsBackground = true;
                    usbipServer.Start();
                }
                KeyboardMouseTest(mouse, keyboard);
            }
        }
        private static void usbipServer_Run(object obj)
        {
            foreach (Process process in Process.GetProcessesByName("usbip"))
            {
                process.Kill();
            }
            // usbip attach -r 127.0.0.1 -b 1-1
            Process.Start("usbip", "attach -r 127.0.0.1 -b 1-1");
        }
        private static void KeyboardMouseTest(MouseDevice mouse, KeyboardDevice keyboard)
        {
            Console.WriteLine("Wait for usbip...");
            while (true)
            {
                Console.Write(".");
                if (mouse.Connected == true)
                {
                    break;
                }
                Thread.Sleep(1000);
            }
            bool mouseMode = false;
            while (true)
            {
                Console.Write(((mouseMode == true) ? "Mouse" : "Keyboard") + "> ");
                string text = Console.ReadLine();
                if (text == "quit")
                {
                    break;
                }
                if (text == "--mode")
                {
                    mouseMode ^= mouseMode;
                    continue;
                }
                Thread.Sleep(2000);
                if (mouseMode == true)
                {
                    mouse.SendText(text);
                }
                else
                {
                    keyboard.SendText(text);
                }
            }
        }
    }
}
오~~~ 멋지죠? ^^ 관련 소스 코드는 다음의 github repo에 모두 올려놓았습니다.
USBIP-Virtual-USB-Device/dotnet/cs-hid-keyboardmouse/
; https://github.com/stjeong/USBIP-Virtual-USB-Device/tree/master/dotnet/cs-hid-keyboardmouse
아울러 UsbipDevice 타입은 NuGet에도 등록했으니 참고하시고!
UsbipDevice
; https://www.nuget.org/packages/UsbipDevice/
PM> Install-Package UsbipDevice -Version 1.0.4
간간이 다음의 글도 한 번 보는 것도 도움이 될 것입니다. ^^
USB in a NutShell
; https://www.beyondlogic.org/usbnutshell/usb1.shtml
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]