Currently I have the following code inside my app
CODE
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.ComponentModel;
public class MyHotKeys
{
#region fields
public const int WM_HOTKEY = 0x312;
public const uint MOD_KEYUP = 0x1000;
#endregion
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public static void RegisterHotKey(Form f, uint key)
{
Func ff = delegate()
{
if(!RegisterHotKey(f.Handle, (int)key, MOD_KEYUP, key))
{
Win32Exception w32e = new Win32Exception();
MessageBox.Show(w32e.ToString());
}
};
f.Invoke(ff); // this should be checked if we really need it (InvokeRequired), but it's faster this way
}
private delegate void Func();
public static void UnregisterHotKey(Form f, int key)
{
try
{
Func ff = delegate()
{
UnregisterHotKey(f.Handle, key);
};
f.Invoke(ff); // this should be checked if we really need it (InvokeRequired), but it's faster this way
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
Then I do
CODE
MyHotKeys.RegisterHotKey(this, VK_MEDIA_PLAY_PAUSE);
MyHotKeys.RegisterHotKey(this, VK_MEDIA_STOP);
MyHotKeys.RegisterHotKey(this, VK_MEDIA_PREV_TRACK);
MyHotKeys.RegisterHotKey(this, VK_MEDIA_NEXT_TRACK);
Without the MOD_KEYUP it works perfectly, but with it does print me Error code 1004 "Invalid attributes" (if that's the correct translation from the german "Unzulässige Attribute")
I have absolutely no clue why it does not work this way (if I put in MOD_CONTROL and declare that it works just fine)