在拦截条形码扫描仪的键盘事件后,Msgbox消失 [英] Msgbox dissapears after intercepting keyboard events for a barcode scanner

查看:143
本文介绍了在拦截条形码扫描仪的键盘事件后,Msgbox消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



首先,我要提前感谢所有人并道歉,因为我很少使用wndproc和消息。



我正在使用以下链接的代码(转换为Visual Basic并稍加修改)来截取条形码扫描器中的键并将它们传递给实例化收到回车键时的类,不允许使用prefiltermessage处理keydown,以免在重点控制/表单中处理密钥。



在WinForms中区分条形码扫描器和键盘



目前我没有条形码扫描仪,但出于测试目的,我正在使用连接到计算机的第二个USB键盘,这应该可以解决问题。



以下子/功能是条形码扫描器截取的基本核心。有一个C ++互操作库由前一个链接的作者开发,用于制作GetRawInputInfo和处理设备,虽然我认为它们与问题无关。

Hi everyone,

First of all I want to thank everyone in advance and to apologize because I have very little experience playing with wndproc and messages.

I'm using the code of the following link (converted to Visual Basic and a bit modified) to intercept the keys from a barcode scanner and pass them to the instantiating class when the enter key is received, not letting the keydown be processed using prefiltermessage to don't let the key be processed in the focuseed control/form.

Distinguishing Barcode Scanners from the Keyboard in WinForms

At the moment I don't have a barcode scanner, but for testing purposes, I'm using a second USB keyboard attached to the computer, that should do the trick.

The following subs/functions are the basic core of the barcode scanner intercepter. There is a C++ interop library developed by the author of the previous link for making the GetRawInputInfo and handling the devices, although I think they have nothing to do with the problem.

Public Class BarcodeScannerListener
    Private keystrokeBuffer As StringBuilder
    Friend Event BarcodeScanned(BarCode As String)
    Protected Overrides Sub WndProc(ByRef m As Message)
        'Debug.Print(m.Msg)
        Select Case m.Msg
            Case WM_INPUT
                Dim deviceInfo As BarcodeScannerDeviceInfo = Nothing
                Dim handled As Boolean = False
                Dim keystroke As Boolean = False
                Dim localBuffer As String = String.Empty
                Dim rawInputDeviceHandle As IntPtr = IntPtr.Zero

                Me.interopHelper.GetRawInputInfo(m.LParam, rawInputDeviceHandle, keystroke, localBuffer)

                If Me.devices.TryGetValue(rawInputDeviceHandle, deviceInfo) AndAlso keystroke Then

                    Me.filter.FilterNext = True
                    If localBuffer.Length = 1 AndAlso Asc(localBuffer(0)) = 13 Then
                        Dim barcode As String = Me.keystrokeBuffer.ToString()

                        If barcode IsNot Nothing AndAlso barcode.Length > 0 Then
                            Me.keystrokeBuffer = New StringBuilder()
                            RaiseEvent BarcodeScanned(barcode)
                        End If
                    Else
                        Me.keystrokeBuffer.Append(localBuffer)
                    End If
                Else
                    Me.filter.FilterNext = False
                End If
                Exit Select
        End Select
        MyBase.WndProc(m)
    End Sub
End Class




Public Class BarcodeScannerKeyDownMessageFilter
    Implements IMessageFilter
    Public Property FilterNext() As Boolean
    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
        Dim filter As Boolean = False
        If Me.FilterNext AndAlso m.Msg = WM_KEYDOWN Then
            filter = True
            Me.FilterNext = False
        End If
        Return filter
    End Function
End Class




Class UsingBarcode
    Private WithEvents CodeReaderHandler As New BarcodeScannerListener
    Private Sub OnBarcodeScanned(Barcode As String) Handles CodeReaderHandler.BarcodeScanned
        MsgBox(barcode)
    End Sub
End Class



从键盘接收到Enter键后,使用BarcodeScanned事件可以完美地发送条形码字符串,但msgbox没有显示,但我可以听到扬声器中的消息框。如果我在第一个msgbox之后放置第二个msgbox,那么第二个msgbox可以正常工作。



看起来来自Enter键的一些消息正在传递给msgbox,并且在用户执行之前点击了默认按钮,因此第一个msgbox消失并且第二个工作正常。对于我所看到的,从来没有为Enter键触发keydown事件,并且正在触发一个keyup事件并且它将转到实例化表单。我已经尝试使用keyup消息在prefiltermessage中停止并在按Enter键后手动将过滤器设置为true,但仍然是键到达表单的keyup事件,所以我上传到了messasgebox。 br />


作为一种解决方法我正在解雇2个msgbox而不是一个,但这不是一个优雅的解决方案。



任何人都知道答案?我一直在努力为自己和谷歌寻找解决方案,但我找不到任何解决方案,所以任何帮助都会非常感激。


After receiving the Enter key from the keyboard the barcode string is perfectly sent using the BarcodeScanned event, but the msgbox is not shown, but I can hear the bip os the messagebox in the speakers. If I put a second msgbox just after the first one, the second msgbox works perfectly.

It looks like some message from the Enter key is passing through to the msgbox and the default button is being clicked before the user does, so the first msgbox dissapears and the second works fine. For what I can see, a keydown event is never fired for the Enter key, and a keyup event is being fired and it gets to the instantiating form. I've tried to put an stop in the prefiltermessage with the keyup message and setting the filter to true manually after pressing the Enter key, but still the key gets to the form's keyup event, so I supossed is getting to the messasgebox too.

As a workaround I'm firing 2 msgbox instead of one, but is not an elegant solution.

Anyone knows the answer? I've been trying to look for a solution for myself and in Google, but I couldn't find any, so any help will be really appreciated.

推荐答案

这篇关于在拦截条形码扫描仪的键盘事件后,Msgbox消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆