0

In my C# application, I handle CTRL and SHIFT in OnKeyDown as shown below. It works as intented. However, when I press the middle button on my Logitech M705 I get a sequence of unwanted key events. Both foo() and bar() get called, which clearly is unwanted behavior. (I suppose the driver generates several key presses to activate some special zoom tool?)

Q: How can I detect these simulated key presses? Or, how can I prevent OnKeyDown from being called in this particular case?

protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyCode == Keys.ShiftKey)
        foo();
    else if (e.KeyCode == Keys.ControlKey)
        bar();
}
Yuck
  • 49,664
  • 13
  • 105
  • 135
l33t
  • 18,692
  • 16
  • 103
  • 180

1 Answers1

2

Sounds to me like your logitec driver has a certain key combination macro bound to your middle button click. Since the driver intentionally simulates keyboard input, I'm not sure it's possible to filter out "fake" keypresses.

Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
  • I suppose it can be done using an application-wide `WH_KEYBOARD_LL` hook and check for the `LLKHF_INJECTED` flag. Ugh... :/ – l33t Aug 08 '12 at 14:42
  • If the mouse presents itself as multiple HID devices, this may actually be being done in hardware - in which case the LLKHF_INJECTED flag won't be present. I know this is certainly the case with the G9 and similar. – PhonicUK Aug 08 '12 at 14:45
  • Thanks for the info. No solution then... :/ – l33t Aug 08 '12 at 14:48