I have this piece of code that works on Winform
:
public class GlobalHotkey
{
private int modifier;
private int key;
private IntPtr hWnd;
private int id;
public GlobalHotkey(int modifier, Keys key, Form form)
{
this.modifier = modifier;
this.key = (int)key;
this.hWnd = form.Handle;
id = this.GetHashCode();
}
public bool Register()
{
return RegisterHotKey(hWnd, id, modifier, key);
}
public bool Unregiser()
{
return UnregisterHotKey(hWnd, id);
}
public override int GetHashCode()
{
return modifier ^ key ^ hWnd.ToInt32();
}
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
And after InitializeComponent
I just initiate it this way:
ghk = new GlobalHotkey(Constants.CTRL + Constants.SHIFT, Keys.A, this);
I have a WPF project and I want to use the same class, so I try to change the constructor this way:
public GlobalHotkey(int modifier, Keys key, System.Windows.Window form)
{
this.modifier = modifier;
this.key = (int)key;
this.hWnd = form.Handle;
id = this.GetHashCode();
}
But I have a compile-time error at this line:
this.hWnd = form.Handle;
Severity Code Description Project File Line Suppression State Error CS1061 'Window' does not contain a definition for 'Handle' and no accessible extension method 'Handle' accepting a first argument of type 'Window' could be found (are you missing a using directive or an assembly reference?)