如何防止特定键盘输入到WPF中的TextBox

发布于 2025-01-09 18:06:19 字数 1937 浏览 2 评论 0原文

我有一个带有两个 HID 键盘的系统(实际上,一个是条形码扫描仪。)。我注册设备('条形码扫描仪')来读取 原始输入数据

我设置了 RIDEV_NOLEGACY | RIDEV_INPUTSINK 到 RAWPUTDEVICE 的 dwFlags。然后我可以在代码中读取和处理原始输入数据。

但是,因为我设置了 RIDEV_NOLEGACY dwFlags,所以另一个通用键盘无法在 XAML 上的 TextBox 中输入任何内容。那不是我想要的。我只是想阻止条形码扫描仪直接输入文本框,而不是所有键盘。

以下是代码示例(需要安装nuget包“sharplibhid”):

private SharpLib.Hid.Handler iHidHandler;
    
    private void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
        source.AddHook(WndProc);

        RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1];
        rid[0].usUsagePage = 0x01;

        rid[0].usUsage = 0x06;

        rid[0].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK | RawInputDeviceFlags.RIDEV_NOLEGACY;

        rid[0].hwndTarget = source.Handle;

        iHidHandler = new SharpLib.Hid.Handler(rid);

        iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
    }

    public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
    {
        if (aHidEvent.IsStray)
        {
            //Stray event just ignore it
            return;
        }
        else
        {
            // do something...
            if (aHidEvent.Device.ProductId == xxx && aHidEvent.Device.VendorId == xxx)
            {
                Debug.WriteLine(aHidEvent.VirtualKey.ToString());
            }
        }
    }

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        Message message = Message.Create(hwnd, msg, wParam, lParam);

        switch (msg)
        {
            case Const.WM_INPUT:

                iHidHandler.ProcessInput(ref message);
                handled = true;
                break;
        }

        return IntPtr.Zero;
    }

I have a system with two HID keyboards (actually, one's a barcode scanner.). I Register the device('barcode scanner') for reading raw input data.

I set the RIDEV_NOLEGACY | RIDEV_INPUTSINK to the dwFlags of the RAWINPUTDEVICE. Then I can read and process raw input data in my code.

But, because I set the RIDEV_NOLEGACY dwFlags, another generial keyboard cannot input anything into the TextBox on XAML. That's not what I want. I just want to prevent the barcode scanner from entering directly into the text box, instead of all keyboards.

The following is the code sample(you need to install the nuget package 'sharplibhid'):

private SharpLib.Hid.Handler iHidHandler;
    
    private void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
        source.AddHook(WndProc);

        RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1];
        rid[0].usUsagePage = 0x01;

        rid[0].usUsage = 0x06;

        rid[0].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK | RawInputDeviceFlags.RIDEV_NOLEGACY;

        rid[0].hwndTarget = source.Handle;

        iHidHandler = new SharpLib.Hid.Handler(rid);

        iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
    }

    public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
    {
        if (aHidEvent.IsStray)
        {
            //Stray event just ignore it
            return;
        }
        else
        {
            // do something...
            if (aHidEvent.Device.ProductId == xxx && aHidEvent.Device.VendorId == xxx)
            {
                Debug.WriteLine(aHidEvent.VirtualKey.ToString());
            }
        }
    }

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        Message message = Message.Create(hwnd, msg, wParam, lParam);

        switch (msg)
        {
            case Const.WM_INPUT:

                iHidHandler.ProcessInput(ref message);
                handled = true;
                break;
        }

        return IntPtr.Zero;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在风中等你 2025-01-16 18:06:19

我建议使用简单的文本框来读取各种键盘,并在条形码读取器设备中激活在解码条形码后传递Enter,这样您就可以读取键盘和条形码读取器数据。

I suggest using simple textBox to read all kinds of keyboards and in barcode reader device active to pass Enter after decoding the barcode So you can read both of them keyboard and barcode reader data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文