I need to listen for keyboard events from a 开发者_如何学运维specific keyboard device & know which key is pressed in a (C#) WPF application. Ideally, this shouldn't be window dependant and work so long as the application has focus.
unfortunately I can't think of / find any way to do this.
any ideas?
D.R
Edit: I've been looking into the OpenTK.Input library, which has a nice interface for keys... Does anybody know how to get a KeyboardDevice without creating a GameWindow
Info: Just by the way, this is for a barcode scanner which emulates a keyboard... who's bright idea was that, eh?
I'm actually working on a project that does exactly that. Check out Kaptivate. It installs a global keyboard hook, and ties together (using magic) the raw input api, and then invokes a callback function so that YOU can decide (1) is this the device I'm after?, and (2) should I allow other apps to see it, or just keep the keystroke for myself. Right now it's only C++ but one of the goals is eventually to have C# wrappers.
For keyboards, each generated event tells you the vkey, scan code, and source device.
Have a look at the below 2 articles.
Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C#
https://gist.github.com/471698
both should be exactly what you want...
Managed to find this tutorial on msdn. along with a Sample Scanner object which comes with the POS.Net SDK
I haven't really had the time to pick apart how it works yet to give a proper overview, but it seems I should be able to write a custom service object abstraction for any "keyboard wedge" HID device.
精彩评论