栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在C#中区分多个输入设备

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在C#中区分多个输入设备

您可以像我最近一样使用Raw Input
API区分键盘和扫描仪。
连接了多少个键盘或类似键盘的设备都没有关系;您将

WM_INPUT
在击键映射到通常在
KeyDown
事件中通常看到的与设备无关的虚拟键之前看到一个。

执行其他人推荐的操作和配置扫描仪以在条形码前后发送哨兵字符要容易得多。(您通常可以通过扫描扫描仪用户手册背面的特殊条形码来进行此操作。)然后,主窗体的

KeyPreview
事件可以监视那些滚动结束并吞下任何子控件的关键事件(如果该控件处于读取条形码的中间)。或者,如果您想成为更高级的爱好者,则可以使用低级键盘钩
SetWindowsHookEx()
来监视那些哨兵并将其吞入那里(这样做的好处是,即使您的应用程序没有关注焦点,您仍然可以得到该事件)。

除其他外,我无法更改条形码扫描器上的哨兵值,因此我不得不走复杂的路线。绝对是痛苦的。如果可以,请保持简单!

-

七年后的更新: 如果您的用例是从USB条码扫描器读取的,则Windows 10具有一个内置的友好,友好的API

Windows.Devices.PointOfService.BarpreScanner
。它是一个UWP / WinRT
API,但您也可以从常规桌面应用程序中使用它。那就是我现在正在做的。这是一些示例代码,直接从我的应用程序中获取要点:

{    using System;    using System.Linq;    using System.Threading.Tasks;    using System.Windows;    using Windows.Devices.Enumeration;    using Windows.Devices.PointOfService;    using Windows.Storage.Streams;    using PosBarpreScanner = Windows.Devices.PointOfService.BarpreScanner;    public class BarpreScanner : IBarpreScanner, IDisposable    {        private ClaimedBarpreScanner scanner;        public event EventHandler<BarpreScannedEventArgs> BarpreScanned;        ~BarpreScanner()        { this.Dispose(false);        }        public bool Exists        { get {     return this.scanner != null; }        }        public void Dispose()        { this.Dispose(true); GC.SuppressFinalize(this);        }        public async Task StartAsync()        { if (this.scanner == null) {     var collection = await DeviceInformation.FindAllAsync(PosBarpreScanner.GetDeviceSelector());     if (collection != null && collection.Count > 0)     {         var identity = collection.First().Id;         var device = await PosBarpreScanner.FromIdAsync(identity);         if (device != null)         {  this.scanner = await device.ClaimScannerAsync();  if (this.scanner != null)  {      this.scanner.IsDepreDataEnabled = true;      this.scanner.ReleaseDeviceRequested += WhenScannerReleaseDeviceRequested;      this.scanner.DataReceived += WhenScannerDataReceived;      await this.scanner.EnableAsync();  }         }     } }        }        private void WhenScannerDataReceived(object sender, BarpreScannerDataReceivedEventArgs args)        { var data = args.Report.ScanDataLabel; using (var reader = DataReader.FromBuffer(data)) {     var text = reader.ReadString(data.Length);     var bsea = new BarpreScannedEventArgs(text);     this.BarpreScanned?.Invoke(this, bsea); }        }        private void WhenScannerReleaseDeviceRequested(object sender, ClaimedBarpreScanner args)        { args.RetainDevice();        }        private void Dispose(bool disposing)        { if (disposing) {     this.scanner = null; }        }    }}

当然,您需要一台支持USB HID
POS的条形码扫描仪,而不仅仅是键盘楔。如果您的扫描仪只是一个键盘楔子,我建议您以25美元的价格在eBay上购买二手Honeywell
4600G。相信我,您的理智是值得的。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394051.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号