0

I have an application that can receive key strokes from a barcode scanner (which to Windows just looks like a USB keyboard).

My requirement is for the user to be able to use the barcode scanner anywhere in the application (any window, any tab) and have it react accordingly. I already have a version of this working by monitoring for PreviewTextInput events in my App.xaml.cs and firing off my own custom BarcodeArrived event. The problem with this is that if the user has put focus on a control that does not accept Text Input, then PreviewTextInput never fires.

PreviewKeyDown does always fire, but the data it presents is ugly and I can't seem to find anyway of translating KeyDown events to a normalized string. I found a Stack Overflow article at "Convert received keys in PreviewKeyDown to a string" seemed promising, but it does not appear that I can wire this in my App.xaml.cs (no dependency object).

Any thoughts or suggestions would be great. Or, as an alternative, at least a way of detecting which keyboard the input is coming from, at which point using PreviewKeyDown might be a viable option because I can assume a "dumbed down" input stream.

Community
  • 1
  • 1
Jim
  • 2,034
  • 1
  • 22
  • 43
  • I have suffered this situation as obviously many others had, since there is this post - http://stackoverflow.com/questions/615036/reading-a-barcode-using-a-usb-barcode-scanner-along-with-ignoring-keyboard-data - One other possible solution if you have control over scanners used is to set a 'framing character' on the scanner output and look for that in your code. – BillH Jul 22 '13 at 18:21
  • And here is a second link that might help http://stackoverflow.com/questions/1495092/convert-received-keys-in-previewkeydown-to-a-string - I suggest that you filter on all keystrokes, I found trying to restrict or force focus to be unreliable. – BillH Jul 22 '13 at 18:29
  • I had the same problem and solved it with an attached behaviour on the shell and a handler that passed the ViewModel a POCO class that described the event in NON-hardware/NON-windows nomenclature and required only the System namespace to compile. That may be overkill for your app, but the POCO class approach definitely answers your problem of conversion. I put the app (which is big) in Codeplex at the Codeplex site. – Gayot Fow Jul 22 '13 at 18:36
  • @BillH, yes, I am already doing a framing character, but I wanted it to be something that the user should never type in, so I made it a or Ctrl-^. While that works, it makes using KeyDown nearly impossible. I will look at the other links next. – Jim Jul 22 '13 at 18:42
  • @GarryVass. Interesting. Can you provide a link or the name of the Codeplex project/class so I can examine it? – Jim Jul 22 '13 at 18:44
  • Issue. The spamming rules here frown heavily upon self-oriented links and I worry about getting spanked by the higher ups. You can get by searching Codeplex for a photo browser. And of course if you think it would be of any help, I can post the attached behaviour class as an answer and you might be able to get the hang if it from there. – Gayot Fow Jul 22 '13 at 18:57
  • @BillH, I reviewed the articles and the second one is the one I already mentioned in my posting. As far as the first one, I am already taking _those_ steps, but my problem is PreviewTextInput vs. PreviewKeyDown where I don't always get a PreviewTextInput depending on what is in focus and PreviewKeyDown being difficult to work with. Thanks. – Jim Jul 22 '13 at 19:03
  • @GarryVass. Yes, I understand, but I also just found Prism. Can you either tell me which module to look at or, as you suggested, post the relevant code as an answer for me to look at. Thanks. – Jim Jul 22 '13 at 19:04
  • If you found the Prism Photo Browser, that's what implements it. I can put some code in an answer to help you know where to look. Take note: the Codeplex app is *HUGE* and purposefully so... – Gayot Fow Jul 22 '13 at 19:06
  • The key place to look is in the Poco's namespace and then files: KeyAdapter.cs, NavigationShortcut.cs, and PointerAdapter.cs. They translate hardware specific info into ordinary strings and numbers for consumption by a ViewModel – Gayot Fow Jul 22 '13 at 19:14
  • @GarryVass, thanks. I will need to take a look at it and see if it helps. – Jim Jul 22 '13 at 20:09

0 Answers0