[Python] How Do I resolve the namespace errors in C# when building a simple raw input handler?

Discussão em 'Python' iniciado por Stack, Julho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm building a software in python right now that requires input device differentiation, I've tried many methods including pywinusb.hid and keyboard, but neither library effectively supports detecting the input device along with a keyboard event. Thus, I have resorted to building a Raw Input Handler in C# (per AI recommendation). I have never made anything in C# before, but I'm fairly experienced with several other similar programming languages.

    I set up a C# console app (.NET Framework) in Visual Studio Community edition and asked AI to generate a handler for me. It recommended I use the wrapper library RawInput.Sharp (NuGet) by mfakane to simplify the project. Here is the code AI provided me:

    using System;
    using System.Text;
    using System.Text.Json;
    using System.Windows.Forms;
    using Linearstar.Windows.RawInput;
    using Linearstar.Windows.RawInput.Native;

    public class Program
    {
    [STAThread]
    static void Main()
    {
    Console.OutputEncoding = Encoding.UTF8;
    Console.WriteLine("C# Raw Input Handler starting...");

    using (var receiverWindow = new RawInputReceiverWindow())
    {
    receiverWindow.Input += OnRawInput;

    try
    {
    RawInputDevice.RegisterDevice(
    HidUsageAndPage.Keyboard,
    RawInputDeviceFlags.InputSink | RawInputDeviceFlags.NoLegacy,
    receiverWindow.Handle
    );

    Console.WriteLine("Successfully registered for keyboard raw input.");
    Console.WriteLine("Listening for keyboard events. Press Ctrl+C to stop.");
    Application.Run();
    }
    catch (Exception ex)
    {
    Console.Error.WriteLine($"Error registering raw input devices: {ex.Message}");
    }
    }

    Console.WriteLine("C# Raw Input Handler stopped.");
    }

    private static void OnRawInput(object sender, RawInputEventArgs e)
    {
    if (e.Data is RawInputKeyboardData keyboardData)
    {
    var device = keyboardData.Device;
    string eventType = "unknown";

    if (keyboardData.Keyboard.KeyState.HasFlag(RawKeyboardKeyState.KeyDown) ||
    keyboardData.Keyboard.KeyState.HasFlag(RawKeyboardKeyState.SankKeyDown))
    {
    eventType = "down";
    }
    else if (keyboardData.Keyboard.KeyState.HasFlag(RawKeyboardKeyState.KeyUp) ||
    keyboardData.Keyboard.KeyState.HasFlag(RawKeyboardKeyState.SankKeyUp))
    {
    eventType = "up";
    }

    var eventData = new
    {
    device = new
    {
    handle = device.DeviceHandle.ToInt64(),
    path = device.DevicePath,
    name = device.ProductName,
    vendorId = device.VendorId,
    productId = device.ProductId,
    manufacturer = device.ManufacturerName
    },
    input = new
    {
    type = "keyboard",
    eventType = eventType,
    virtualKeyCode = (ushort)keyboardData.Keyboard.VirtualKey,
    scanCode = (ushort)keyboardData.Keyboard.ScanCode,
    keyName = GetKeyName((ushort)keyboardData.Keyboard.VirtualKey)
    }
    };

    Console.WriteLine(JsonSerializer.Serialize(eventData));
    }
    }

    private static string GetKeyName(ushort virtualKeyCode)
    {
    return ((Keys)virtualKeyCode).ToString();
    }
    }


    Below are the items that were underlined red (namespace) in my editor:

    System.Windows.Forms,
    RawInputReceiverWindow(),
    Application.Run(),
    RawInputEventArgs,
    keyboardData.Keyboard.KeyState,
    RawKeyboardKeyState,
    device.DeviceHandle,
    keyboardData.Keyboard.VirtualKey,
    Keys


    I've worked with AI to try resolving the issue, but all it keeps doing is sending me the same code over and over again and blindly recommending solutions. I've tried looking for resources online with no avail. Does anybody know if there is a better solution for my particular use case or if there is a way to resolve the namespace errors?

    Continue reading...

Compartilhe esta Página