tags:

views:

286

answers:

6

I have 2 keyboards hooked up to my machine (one on PS/2, the other on USB). How would I determine which keyboard is sending the keystrokes in C#?

+1  A: 

You won't be able to.

The .NET CLR sits above these services in the Windows stack. I wouldn't even be convinced that at driver level you work this out. It may be occluded by the hardware/bios.

-Oisin

x0n
COME ON GUYS! it can be done somehow ...
metro
see ? :D .. everything is possible
metro
glad to be wrong. p/invoke to the rescue!
x0n
A: 

I think the only way you could do that would be if you were monitoring the USB traffic. I've seen programs that do that, but I have no idea how to do it in C#. Thus, If there's USB traffic and a keystroke, it's the USB keyboard. If there's no traffic, it's the PS/2 one.

Naturally the other caveat with this is that it doesn't generalize to arbitrary or future keyboard connection types.

Omniwombat
+2  A: 

If one is on USB, then you should be able to figure it out - at least on driver level. However, higher up the stack I haven't heard of such an option. I wouldn't be surprised if it was not possible at all.

One avenue you might want to check out are the dancing simulators. You know, the ones with the "dance mat" (or "dance pad"). These dance mats are actually nothing else than but big keyboards. Check it out yourself - open up notepad and jump on the dance mat. :)

Now, some of these programs also offer competitive mode where you can hook up two dance mats and compete with a partner. In this case they would need to distinguish between the mats in some way - which is the same thing as you're trying to do.

Vilx-
A: 

I'm guessing the problem is that when the barcode scanner is used, the number is going into the active field rather than the field for the bar code ID?

One solution would be to hook the other fields' OnChange events and look for a barcode number inserted in them (via RegEx). If found, move the barcode out of that field and move the value over to the barcode field.

No device access required, this could even be done on a Javascript-driven web page.

richardtallent
I'm the one with the barcode scanner; that wasn't part of the original question.
Mark Ransom
+1  A: 

Ok, here's a really kludgy method that would only work with a very limited subset of use cases:

  1. Tape, glue down, or short out a set of modifier keys (Right Ctrl, for example) on one of the keyboards.
  2. On each key press, look at the modifiers. If the modifiers are set as they would be on the glued-down keyboard, assume that keyboard is the source. If not, assume it's the other keyboard.

Limitations:

  1. You can't use the glued keyboard outside your app, since only it knows to ignore the modifier keys.
  2. You can't use those modifier keys within your own app as part of keyboard shortcuts.
  3. The other keyboard can impersonate yours (accidentally or not) if the user holds down the correct buttons.
  4. You'll need to disable StickyKeys.
  5. Using Alt or Ctrl has a down side: if you press the other one, Windows may have a use for that combination that your app won't catch.

Unknown #1: whether ModifierKeys is only populated from the keyboard that was the source of the key click, or comes from the sum of all modifiers across all connected keyboards. I suspect each keyboard internally sends the modifiers attached to the keystroke and that Windows doesn't aggregate them.

Unknown #2: whether or not System.Windows.Forms.Control.ModifierKeys provides only Ctrl/Shift/Alt bits, or provides enough detail to, say, distinguish the Right Ctrl key from the left. Here are the keys you could theoretically test for, I don't know how many are provided via ModifierKeys:

http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

richardtallent
+3  A: 

Here's a sample, very dirty code, showing how to use windows input hook from .NET. Works best with Snippet Compiler :>

EDIT: turns out there's a length limit for posts,

grab the source from code here

.

arul
thanks a lot ! this work fine, but it will take me a lot of time to understand how it works :)
metro
I found this one too http://www.codeproject.com/KB/system/rawinput.aspx
metro