Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Differentiating between different hotkeys in c#

Differentiating between different hotkeys in c#

Scheduled Pinned Locked Moved C#
csharphelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luke Dyer
    wrote on last edited by
    #1

    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)
                {
    
    L 1 Reply Last reply
    0
    • L Luke Dyer

      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)
                  {
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, the id parameter in RegisterHotkey must be unique (your _Mod is not good); it is used internally to discriminate the different hotkeys; I once used the following code:

      int chr=(int)name\[0\];
      if (chr>=(int)'a' && chr<=(int)'z') chr+=(int)'A'-(int)'a';
      // identifier must be unique, so we use a hash code
      int ident=GetType().GetHashCode()+(modifiers<<16)+chr;
      

      but that is just one attempt to get unique numbers for some command based on its name (some textual description) and the modifier keys (similar to Control.ModifierKeys) all inside a form. :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups