将输入从类似键盘的设备重定向到windows环境中的后台进程



我使用简单的RFID阅读器设备,该设备通过usb电缆连接到PC,并被识别为类似键盘的设备。我想通过我的应用程序读取该设备的输入,但它正在后台运行,即其他应用程序处于焦点。如何在Windows7环境中将stdin重定向到我的应用程序?我无法将焦点更改为我的应用程序我无法在前端应用程序中进行更改(例如,捕获stdin并通过管道等将其发送到我的应用程序)我的应用程序是用C#编写的,但我可以将其重写为Java/C电脑运行Win 7操作系统。

谢谢y

这里是我的解决方案:

我已通过USB将一个普通的键盘和一个读卡器连接到我的计算机
两个设备都写入windows键盘缓冲区。

我想将读卡器的输入重定向到另一个应用程序或文件,并将其从键盘缓冲区中删除(这样该输入就不会显示在任何编辑器中)。

我所知道的/先决条件:

  • 读卡器的输入仅包含十六进制字母(0-9,A-F),并以换行符结束
  • 读卡器输入将在"块上"接收,这意味着在两个收到的信件之间只有几毫秒
  • 人类不可能在70毫秒内输入两个或多个数字
    (我已经尝试过了)

我的工作:

  • 我听键盘缓冲区,取出每一个输入字母/键。任何不是0-9或A-F的输入都将立即放回键盘缓冲区
  • 如果出现0-9或a-F输入,我会将其存储在字符串缓冲区(可能来自读卡器)
  • 如果超过70ms没有进一步的输入,并且缓冲区中至少有4个字节包含0-9或A-F,那么我假设/知道它来自读卡器,并以我自己的方式使用它。这些字节已从键盘缓冲区中取出
  • 如果我的缓冲区中只有一个/两个/三个字母0-9/A-F,那么70毫秒后,它们将被放回windows键盘缓冲区。在打字时,你不会意识到"有些"字母对人类来说有点延迟

这里是我的程序(用脚本语言AutoHotkey编写):

KeybRedir.ahk

; KeybRedir
; Programmiert von Michael Hutter - Mai 2018
#NoEnv ;Avoids checking empty variables to see if they are environment variables
#SingleInstance force
Process, Priority, , Normal
#Warn All
SaveKeyList=
0::
1::
2::
3::
4::
5::
6::
7::
8::
9::
+A::
+B::
+C::
+D::
+E::
+F::
Return::
{ ; If one of these characters was typed => take it out of windows key buffer...
    if A_ThisHotkey = Return
        Hotkey:=Chr(13)
    else
        Hotkey:=A_ThisHotkey
    SaveKeyList = %SaveKeyList%%Hotkey%
    SetTimer , DoKeyPlay, 70 ; Wait 70ms for another key press of charlist (re-trigger the timer in case it already runs)
}
return
DoKeyPlay:
    SetTimer , , Off
    SaveText:=RegExReplace(SaveKeyList, "+", "")
    StringReplace, SaveText, SaveText, `r, , All
    if StrLen(SaveText) < 4 ; Accumulated text size < 4 letters => normal key input
    {
        SendPlay %SaveKeyList% ; put captured text back in windows key buffer
    }
    else ; Now we have the input of the card reader
    {
        SplashTextOn, , , %SaveText% ; Do something with the input or the card reader ...
    }
    SaveKeyList=
    SaveText=
    SetTimer , SplashOff, 2000
return
SplashOff:
    SplashTextOff
    SetTimer , , Off
return

您可以使用"自动热键编译器"将此脚本编译为exe文件(327KB)。

最新更新