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

时间:2015-12-30 14:34:11

标签: c# redirect windows-7 keyboard stdin

我使用简单的RFID阅读器设备,用USB线连接到PC,并识别为键盘式设备。 我希望通过我的应用程序读取此设备的输入,但它在后台运行,即其他应用程序是焦点。 如何在Windows 7环境中将stdin重定向到我的应用程序? 我无法将焦点转移到我的应用程序上 我无法在前端应用程序中进行更改(例如,捕获标准输入并通过管道将其发送到我的应用程序等) 我的应用程序是用C#编写的,但我可以将其重写为Java / C. PC运行Win 7 OS。

由于 y

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

我已通过USB将普通的键盘读卡器连接到我的电脑。
两个设备都写入Windows键盘缓冲区。

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

我所知道/先决条件:

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

我的所作所为:

  • 我听键盘缓冲区并取出每一个输入字母/键。任何非0-9或A-F的输入都将立即返回键盘缓冲区。
  • 如果输入0-9或A-F出现,我会将其存储在字符串缓冲区中(可能来自读卡器)
  • 如果没有超过70毫秒的进一步输入且缓冲区中至少有4个字节包含0-9或A-F,那么我假设/知道它来自读卡器并以我自己的方式使用它。这些字节已经从键盘缓冲区中取出。
  • 如果我的缓冲区中只有一个/两个/三个字母0-9 / A-F,那么在70ms之后它们将被放回到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

您可以使用Autohotkey Compiler将此脚本编译为exe文件(327KB)。