Winform multi-key combinations boss Alt_Ctrl_Shift

One day, in order to microblogging fans elves increase a boss key feature, used to find a way to start from the network to start looking: keywords similar to "C # boss key", a search, and a bunch of a bunch, but out of the code, basically a kind of:

 

Normally, ultimately, the general boss key: Alt + Ctrl + Shift + XX This multi-way combination, but all kinds of code that is not directly explain, nor a prompt appears to be intentionally hidden, finally, I found them still some unknown hidden attributes:

 

The following look at my modified from common network code:

 

public  delegate  void HotkeyEventHandler( int HotKeyID);

     public  class SystemHotKey : System.Windows.Forms.IMessageFilter
    {
        List<UInt32> keyIDs =  new List<UInt32>();
        IntPtr hWnd;

         public  event HotkeyEventHandler OnHotkey;

         public  enum KeyFlags
        {
            Alt =  0x1,
            Ctrl =  0x2,
            Shift =  0x4,
            = Win  0x8 ,
             // key combination is equal to the value adding
            Alt_Ctrl =  0x3 ,
            Alt_Shift =  0x5 ,
            Ctrl_Shift =  0x6 ,
            Alt_Ctrl_Shift =  0x7
        }
        [the DllImport ( " User32.dll " )]
         public   static   extern  UInt32 RegisterHotKey (IntPtr the hWnd, UInt32 ID, fsModifiers UInt32, UInt32 VK);

        [the DllImport ( " User32.dll " )]
         public  static  extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);

        [DllImport( " kernel32.dll ")]
         public  static  extern UInt32 GlobalAddAtom(String lpString);

        [DllImport( " kernel32.dll ")]
         public  static  extern UInt32 GlobalDeleteAtom(UInt32 nAtom);

         public SystemHotKey(IntPtr hWnd)
        {
             this.hWnd = hWnd;
        }

         public  int RegisterHotkey(KeyFlags keyflags, System.Windows.Forms.Keys Key)
        {
            System.Windows.Forms.Application.AddMessageFilter( this);
            UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
            RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
            keyIDs.Add(hotkeyid);
             return ( int)hotkeyid;
        }

         public  void UnregisterHotkeys()
        {
             if (keyIDs.Count >  0)
            {

                System.Windows.Forms.Application.RemoveMessageFilter( this);
                 foreach (UInt32 key  in keyIDs)
                {
                    UnregisterHotKey(hWnd, key);
                    GlobalDeleteAtom(key);
                }
                keyIDs.Clear();
            }
        }

         public  bool PreFilterMessage( ref   System.Windows.Forms.Message m)
        {
             if (m.Msg ==  0x312)
            {
                 if (OnHotkey !=  null)
                {
                     foreach (UInt32 key  in keyIDs)
                    {
                         if ((UInt32)m.WParam == key)
                        {
                            OnHotkey(( int)m.WParam);
                             return  true;
                        }
                    }
                }
            }
             return  false;
        }
    }
 

There are a few points above talk about:

 

1: System.Windows.Forms.Application.AddMessageFilter (this); phrase needs to correspond System.Windows.Forms.Application.RemoveMessageFilter (this); here exhausted to remember to cancel.
As the original program, only in the constructor added, so after you cancel, and then become invalid, and here to add directly at the time of registration, remove canceled, notice to this effect.
 

2: hotkey combination:

            // key combination is equal to the sum value
            Alt_Ctrl = 0x3,
            Alt_Shift = 0x5,
            Ctrl_Shift = 0x6,
            Alt_Ctrl_Shift = 0x7

 

This is the casual find thinking, the online code is not mentioned, it is estimated turn too many people know, do not write it down.

 

3: The Hastable changed to List <Unint32> way.

 

More recently things are relatively simple to write the text, and everyone forgive me. 

Reproduced in: https: //my.oschina.net/secyaher/blog/274174

Guess you like

Origin blog.csdn.net/weixin_33946020/article/details/91967056