Here I will post problems I and my colleagues met and solutions we found.

Thursday, September 04, 2008

Keyboard.Modifiers sometimes doesn't work

There are many examples how to implement keyboard hooks in .NET. The one I used as example is http://blogs.vertigo.com/personal/ralph/Blog/archive/2007/02/12/wpf-low-level-keyboard-hook-sample.aspx

There is very important comment there that says Keyboard.Modifier property does not return correct values. First, I wanted to avoid linking to System.Windows.Form assembly and didn't pay much attention go this comments. Well, it didn't work. I had come back to using System.Windows.Forms.Control.ModifierKeys. To make interface more WPF compatible I just converted value this way:

System.Windows.Forms.Keys m = System.Windows.Forms.Control.ModifierKeys;
ModifierKeys m2 = ModifierKeys.None;
if ((m & System.Windows.Forms.Keys.Control) != 0)
m2 = m2 | ModifierKeys.Control;
if ((m & System.Windows.Forms.Keys.Alt) != 0)
m2 = m2 | ModifierKeys.Alt;
if ((m & System.Windows.Forms.Keys.Shift) != 0)
m2 = m2 | ModifierKeys.Shift;
if ((m & System.Windows.Forms.Keys.Apps) != 0)
m2 = m2 | ModifierKeys.Windows;

1 comment:

Anonymous said...

This is good.

http;//shareyourgyan.blogspot.com