Click here to Skip to main content
15,896,111 members
Home / Discussions / C#
   

C#

 
AnswerRe: Get and set properties Pin
Luc Pattyn14-Jul-07 3:39
sitebuilderLuc Pattyn14-Jul-07 3:39 
AnswerRe: Get and set properties Pin
Urs Enzler14-Jul-07 4:41
Urs Enzler14-Jul-07 4:41 
GeneralRe: Get and set properties Pin
User 665814-Jul-07 7:07
User 665814-Jul-07 7:07 
GeneralRe: Get and set properties Pin
Mike Dimmick14-Jul-07 7:38
Mike Dimmick14-Jul-07 7:38 
AnswerRe: Get and set properties Pin
Guffa14-Jul-07 5:40
Guffa14-Jul-07 5:40 
Questionevents in interface Pin
Maddie from Dartford14-Jul-07 2:06
Maddie from Dartford14-Jul-07 2:06 
AnswerRe: events in interface Pin
mav.northwind14-Jul-07 2:37
mav.northwind14-Jul-07 2:37 
QuestionDifferentiating between different hotkeys in c# Pin
Luke Dyer14-Jul-07 1:55
Luke Dyer14-Jul-07 1:55 
Hi,

I'm making a hotkey app and don't know how to know which hotkey has been pressed. Here's the relevant part of my source:

My hotkey class
class Hotkey : IMessageFilter
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private const int WMHotkey = 0x0312;

    #region Properties
    private IntPtr _Handle;
    public IntPtr Handle
    {
        get { return _Handle; }
        set { _Handle = value; }
    }
    private int _Key;
    public int Key
    {
        get { return _Key; }
        set
        {
            UnregisterHotkey();
            try
            {
                RegisterHotKey(value, _Mod);
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not register Hotkey  - there is probably a conflict.  " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            _Key = value;
        }

    }
    private byte _Mod;
    public byte Mod
    {
        get { return _Mod; }
        set
        {
            UnregisterHotkey();
            try
            {
                RegisterHotKey(Key, value);
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not register Hotkey  - there is probably a conflict.  " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            _Mod = value;
        }

    }
    private bool _isPressed;
    public bool isPressed
    {
        get { return _isPressed; }
        set { _isPressed = value; }
    }
    #endregion

    private event EventHandler HotkeyPressed;
    // Alt = 1, Ctrl = 2, Shift = 4, Win = 8
    public Hotkey(Keys key, byte modifier, EventHandler hotKeyPressed)
    {
        if (key != Keys.None)
        {
            HotkeyPressed = hotKeyPressed;
            try
            {
                RegisterHotKey(CharCodeFromKeys(key), modifier);
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not register Hotkey " + key.ToString() + " - there is probably a conflict.  " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Application.AddMessageFilter(this);
        }
    }
    public Hotkey(Keys key, Keys modifier, EventHandler hotKeyPressed)
    {
        if (key != Keys.None)
        {
            HotkeyPressed = hotKeyPressed;
            try
            {
                RegisterHotKey(CharCodeFromKeys(key),  ModifiersFromKeys(modifier));
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not register Hotkey " + key.ToString() + " - there is probably a conflict.  " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Application.AddMessageFilter(this);
        }
    }


    // Get the Char Code from Keys input
    public static byte CharCodeFromKeys(Keys k)
    {
        byte charCode = 0;
        if ((k.ToString().Length == 1) || ((k.ToString().Length > 2) && (k.ToString()[1] == ',')))
            charCode = (byte)k.ToString()[0];
        else if ((k.ToString().Length > 3) && (k.ToString()[0] == 'D') && (k.ToString()[2] == ','))
            charCode = (byte)k.ToString()[1];
        return charCode;
    }
    private const byte ModAlt = 1, ModControl = 2, ModShift = 4, ModWin = 8;

    // Get the modifier value from Keys
    public static byte ModifiersFromKeys(Keys k)
    {
        byte total = 0;

        if (((int)k & (int)Keys.Shift) == (int)Keys.Shift)
            total += ModShift;
        if (((int)k & (int)Keys.Control) == (int)Keys.Control)
            total += ModControl;
        if (((int)k & (int)Keys.Alt) == (int)Keys.Alt)
            total += ModAlt;
        if (((int)k & (int)Keys.LWin) == (int)Keys.LWin)
            total += ModWin;

        return total;
    }

    ~Hotkey()
    {
        Application.RemoveMessageFilter(this);
        UnregisterHotKey(this._Handle, this.GetType().GetHashCode());
    }


    // Unregisters the hotkey
    public void UnregisterHotkey()
    {
        Application.RemoveMessageFilter(this);
        UnregisterHotKey(this._Handle, this.GetType().GetHashCode());
    }

    // Registers the hotkey
    private void RegisterHotKey(int key, int modifier)
    {
        //if (key == Keys.None)
        //    return;

        bool isKeyRegisterd = RegisterHotKey(this._Handle, this.GetType().GetHashCode(), modifier, key);
        if (!isKeyRegisterd)
            throw new ApplicationException("Hotkey already in use");
    }

    public bool PreFilterMessage(ref Message m)
    {
        switch (m.Msg)
        {
            case WMHotkey:
                {
                    if ((int)m.LParam != 1114112) //this is when control gets pressed, I don't know why it makes a case of WMHotkey but it needs to be ignored
                    {
                        isPressed = true;
                        HotkeyPressed(this, new EventArgs());
                        //TODO collect all LPARM then check if LPARAM of message == hotkey and fire that event/method
                        // this is a bad way of doing it but only way I can think of
                        return true;
                    }
                    else
                        return false;
                }
        }
        return false;
    }
}

The class works fine for 1 hotkey but when I try and implement multiple instances of the class I can't tell which hotkey is pressed. For example:
<br />
Hotkeys[0] = new Hotkey(Properties.Settings.Default.Play, Properties.Settings.Default.PlayMod, OnHotkeyPressed);//Play<br />
            Hotkeys[1] = new Hotkey(Properties.Settings.Default.Next, Properties.Settings.Default.NextMod, OnHotkeyPressed);//Next<br />

When I check the isPressed properties the Hotkeys[0](Play hotkey) isPressed property is always true and the Hotkeys[1] is always false(next hokey) even when that hotkey is pressed.

Also another problem I have is that I can only create a hotkey for the letters on the keyboard(keys A-Z) but not PageDown, the F1-F2 keys, the Arrow keys. Does anyone know a work around for this?

Can anyone help me with these problems

Thanks a lot

Luke Dyer
AnswerRe: Differentiating between different hotkeys in c# Pin
Luc Pattyn14-Jul-07 2:30
sitebuilderLuc Pattyn14-Jul-07 2:30 
QuestionKeystroke events on a Form? Pin
kbalias14-Jul-07 1:38
kbalias14-Jul-07 1:38 
AnswerRe: Keystroke events on a Form? Pin
Luc Pattyn14-Jul-07 2:32
sitebuilderLuc Pattyn14-Jul-07 2:32 
Questionkey sensitive Problem Pin
ytubis14-Jul-07 0:40
ytubis14-Jul-07 0:40 
AnswerRe: key sensitive Problem Pin
kubben14-Jul-07 1:17
kubben14-Jul-07 1:17 
AnswerRe: key sensitive Problem Pin
Luc Pattyn14-Jul-07 2:35
sitebuilderLuc Pattyn14-Jul-07 2:35 
QuestionC# Transelation :blush: Pin
Muammar©13-Jul-07 23:38
Muammar©13-Jul-07 23:38 
AnswerRe: C# Transelation :blush: Pin
Guffa13-Jul-07 23:57
Guffa13-Jul-07 23:57 
GeneralRe: C# Transelation :blush: Pin
Muammar©14-Jul-07 0:25
Muammar©14-Jul-07 0:25 
GeneralRe: C# Transelation :blush: [modified] Pin
Luc Pattyn14-Jul-07 2:46
sitebuilderLuc Pattyn14-Jul-07 2:46 
GeneralRe: C# Transelation :blush: Pin
Muammar©17-Jul-07 23:41
Muammar©17-Jul-07 23:41 
QuestionCompiling C# application using another C# code Pin
satsumatable13-Jul-07 22:12
satsumatable13-Jul-07 22:12 
QuestionC# COM entry point function Pin
George_George13-Jul-07 21:56
George_George13-Jul-07 21:56 
AnswerRe: C# COM entry point function Pin
Mike Dimmick14-Jul-07 7:52
Mike Dimmick14-Jul-07 7:52 
GeneralRe: C# COM entry point function Pin
George_George15-Jul-07 2:56
George_George15-Jul-07 2:56 
QuestionFree Audio/Video Library for C# Pin
Sukhjinder_K13-Jul-07 21:42
Sukhjinder_K13-Jul-07 21:42 
AnswerRe: Free Audio/Video Library for C# Pin
Thomas Stockwell14-Jul-07 7:39
professionalThomas Stockwell14-Jul-07 7:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.