Determining which keyboard is sending keystrokes

后端 未结 5 871
梦如初夏
梦如初夏 2021-01-22 12:04

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#?

相关标签:
5条回答
  • 2021-01-22 12:26

    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

    0 讨论(0)
  • 2021-01-22 12:32

    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.

    0 讨论(0)
  • 2021-01-22 12:35

    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.

    0 讨论(0)
  • 2021-01-22 12:36

    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

    .

    0 讨论(0)
  • 2021-01-22 12:45

    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.

    0 讨论(0)
提交回复
热议问题