2

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?

Dead.Rabit
  • 1,965
  • 1
  • 28
  • 46
  • 3
    I don't believe this to be possible. Windows will accept and process a key event from any keyboard. Windows is like a blind man when it comes to the keyboard buffer, any device ( a gamepad for example ) could place keys into the buffer. – Security Hound Apr 06 '11 at 11:54
  • @Ramhound: I'd have to agree - short of writing a driver filter, or something, anyway. – Grant Thomas Apr 06 '11 at 11:59
  • Have a look in the answers I got for a keyboard question I asked some time ago. It may help you to find a dll API that fits your requirements http://stackoverflow.com/questions/172353/how-to-push-a-key-and-release-it-using-c – Larry Apr 06 '11 at 12:00
  • @Ramhound, @MrDisappointment: I can pole for device state using the DirectX libraries so it's definitely possible to get device specific input (though I'd prefer not to have to reference something so beefy). – Dead.Rabit Apr 06 '11 at 13:50
  • This is indeed possible (check out HIDMacros), just a pain in the @$$ to implement, which is why I created an open source library to do it. Check it out below. – Chris Eberle Apr 12 '11 at 22:12

3 Answers3

1

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.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • By the way, I'd currently label this as pre-alpha. If I were you I'd go ahead and get a feel for how it works, and maybe base your own off of it. The library still has a ways to go. – Chris Eberle Apr 12 '11 at 22:16
  • it looks like a good project, though I couldn't get the sample to work on my machine unfortunately... I'll take a second look when I get around to playing with Pos.Net custom service objects more =]. – Dead.Rabit Apr 13 '11 at 10:33
  • Yeah the sample is pretty contrived. Actually I need to change it. There's a race condition at the moment so most of the time it works but sometimes it doesn't. The secret is to have a different window in focus (i.e. the console NEEDS to be in the background). Again, pre-alpha. – Chris Eberle Apr 13 '11 at 14:02
  • ah yes, that got it to work... I'll look forward to when it's finished. – Dead.Rabit Apr 14 '11 at 09:45
  • me too :) At this point it's just me and I've got a lot on my plate, so I'm guessing a few months (but don't quote me on that). One of my friends will be doing the C# wrapping. – Chris Eberle Apr 14 '11 at 14:41
0

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

Community
  • 1
  • 1
TBohnen.jnr
  • 5,117
  • 1
  • 19
  • 26
  • How would you go about making this keyboard specific, I've tried making it print internal values, but can't find any in the classes which are device specific. And so far as I know, none of the WinAPI functions used can be limited to a single device either... – Dead.Rabit Apr 06 '11 at 13:39
  • @Dead.Rabit that's my bad, I did not focus on the specific part (you'd think that because it's in bold I would notice it :-() – TBohnen.jnr Apr 06 '11 at 14:40
0

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.

Dead.Rabit
  • 1,965
  • 1
  • 28
  • 46