Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,465 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,560 people online right now. Registration is fast and FREE... Join Now!




Problem with RegisterHotKey

 
Reply to this topicStart new topic

Problem with RegisterHotKey

MrIQ
2 Jul, 2008 - 06:05 AM
Post #1

New D.I.C Head
*

Joined: 18 Dec, 2007
Posts: 3


My Contributions
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)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:49PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month