简体   繁体   English

如何模拟物理键盘输入

[英]How to simulate physical keyboard input

I'm working with an C# Winforms project that read from multiple specified barcode scanner (app need to seperate data from scanner to have correct action)我正在使用从多个指定的条形码扫描仪读取的 C# Winforms 项目(应用程序需要从扫描仪中分离数据才能采取正确的行动)

I'm used raw input to filter by device ID and it works fine.我使用原始输入按设备 ID 过滤,它工作正常。

Now I want to develop a test app that simulate scanner (keyboard) input (to simulate daily working)现在我想开发一个模拟扫描仪(键盘)输入的测试应用程序(模拟日常工作)

I've tried with some articles which tell me to use Windows API such as SendInput, SendMessage... but it's not contain real device ID (physical device ID), so my test app not recognized these input.我试过一些文章告诉我使用 Windows API 例如 SendInput、SendMessage ......但它不包含真实的设备 ID(物理设备 ID),所以我的测试应用程序无法识别这些输入。

The Hid device ID look like that: \??\HID#Vid_0536&Pid_01c1&MI_01#9&25ca5370&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}隐藏设备 ID 如下所示:\??\HID#Vid_0536&Pid_01c1&MI_01#9&25ca5370&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

Is there anyway to simulate physical input device using C# and Winforms?有没有使用 C# 和 Winforms 模拟物理输入设备? or send key to active app with physical (or fake) device ID?或使用物理(或假)设备 ID 将密钥发送到活动应用程序?

Thank you.谢谢你。

We work with scanners to.我们与扫描仪合作。 If we scan, then we use a TextControl for our old WinForms programs.如果我们进行扫描,那么我们对旧的 WinForms 程序使用 TextControl。 In that TextControl input can be selected by keyboard but also with any scanner.在该 TextControl 输入中,可以通过键盘选择,也可以通过任何扫描仪选择。 Sometimes you will need special scanner controls to accept that data, the scanner documentation tells you how to (add) send them with the barcode too.有时您需要特殊的扫描仪控件来接受该数据,扫描仪文档也会告诉您如何(添加)将它们与条形码一起发送。

So when a scanner sends its data to the TextControl the scanner also sends the 'Return' key for acceptance.因此,当扫描仪将其数据发送到 TextControl 时,扫描仪也会发送“返回”键以供接受。 And yes you can send additive keyboard input with Windows API to.是的,您可以将带有 Windows API 的附加键盘输入发送到。 If you use it that way then the technical sh1t of the scanner is decoupled because then its just an imput device for a TextControl and that can be anything as long as its recognized as an (usb) input device.如果您以这种方式使用它,那么扫描仪的技术 sh1t 将被解耦,因为它只是 TextControl 的输入设备,只要它被识别为(usb)输入设备,它就可以是任何东西。

I've ended this issue by create a fake keyboard, set id, name,.. etc to nothing, In raw input handler, I check key pressed, if there are no hardware ID, assume that is from fake ID and append fake device id to that message, now the simulator is working.我已经通过创建一个假键盘结束了这个问题,将 id、name 等设置为空,在原始输入处理程序中,我检查按下的键,如果没有硬件 ID,假设是来自假 ID 和 append 假设备该消息的 ID,现在模拟器正在运行。 Thank you for support.谢谢你的支持。

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

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