简体   繁体   English

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

[英]redirect input from keyboard like device to background process in windows environment

I use simple RFID reader device which connected to PC with usb cable and identified as keyboard like device. 我使用简单的RFID读取器设备,该设备通过USB电缆连接到PC,并被识别为类似键盘的设备。 I want to read input from this device by my application but it is running in background ie other application is in focus. 我想通过我的应用程序从此设备读取输入,但是它在后台运行,即其他应用程序处于焦点。 How can I redirect stdin to my application in windows 7 environment? 如何在Windows 7环境中将stdin重定向到我的应用程序? I can't change focus to my application I can't make change in front application (eg catch stdin and send it to my application via pipe etc.) My application is written in C# but I can rewrite it to Java / C The PC runs Win 7 OS. 我无法将重点转移到我的应用程序上,也无法在前端应用程序中进行更改(例如,捕获标准输入并通过管道将其发送到我的应用程序中。)我的应用程序是用C#编写的,但是我可以将其重写为Java / C。运行Win 7 OS。

Thanks y 谢谢你

Here my solution: 这是我的解决方案:

I have connected a normal Keyboard and a Card Reader via USB to my computer. 我已经通过USB将普通的键盘读卡器连接到计算机上。
Both devices write to the windows keyboard buffer. 两种设备均写入Windows键盘缓冲区。

I want to redirect the input of the Card Reader to another application or file and remove it from the keyboard buffer (so that this input will not show up in any editor). 我想读卡器的输入重定向到另一个应用程序或文件, 并将其从键盘缓冲区中删除 (这样该输入将不会在任何编辑器中显示)。

What I know / Prerequisites: 我所知道的/先决条件:

  • The input of the Card Reader only contains hexadecimal letters (0-9, AF) and is finished by a newline. 读卡器的输入仅包含十六进制字母(0-9,AF),并以换行符结束。
  • The Card Reader input will be received "on-block", this means between two received letters there are only a few milliseconds 读卡器的输入将“按块接收”,这意味着在收到的两个字母之间只有几毫秒的时间
  • It is not possible for a human to enter two or more numbers within less than 70ms 人类不可能在70毫秒内输入两个或多个数字
    (I have tried it) (我已经尝试过)

What I do: 我所做的:

  • I listen to the keyboard buffer and take out every single input letter/key. 我听键盘缓冲,取出每个输入字母/键。 Any input which is not 0-9 or AF will be put back to the keyboard buffer immediately. 任何非0-9或AF的输入都将立即返回键盘缓冲区。
  • If a input 0-9 or AF comes up I will store it in a string buffer (it might be from the card reader) 如果出现输入0-9或AF,我会将其存储在字符串缓冲区中(可能来自读卡器)
  • If there is no further input for more than 70ms and there are at least 4 Bytes containing 0-9 or AF in the buffer, then I assume/know it was from the Card Reader and use it in my own way. 如果在70毫秒内没有其他输入,并且缓冲区中至少有4个字节包含0-9或AF,那么我假定/知道它来自读卡器,并以自己的方式使用。 These Bytes have been already taken out of the keyboard buffer. 这些字节已从键盘缓冲区中取出。
  • If there are only one/two/three letter(s) 0-9/AF in my buffer, then after 70ms they will be put back to the windows keyboard buffer. 如果我的缓冲区中只有1/2/3个字母0-9 / AF,则70毫秒后它们将被放回Windows键盘缓冲区。 While typing you can't realise that "some" letters show up a little postponed for a human being. 在打字时,您不会意识到“某些”字母的出现会稍微延迟一些。

Here my program (written in the script language AutoHotkey ): 这是我的程序(用脚本语言AutoHotkey编写):

KeybRedir.ahk 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

You can compile this script to a exe file (327KB) using the Autohotkey Compiler . 您可以使用Autohotkey编译器将此脚本编译为exe文件(327KB)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM