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

Join 117,278 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,975 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Problem with RegisterHotKey

 
Reply to this topicStart new topic

Problem with RegisterHotKey

MrIQ
post 2 Jul, 2008 - 07: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

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/6/08 10:36PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month