Пример #1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="win">注册窗体</param>
        /// <param name="control">控制键</param>
        /// <param name="key">主键</param>
        public HotKey(Window win, HotKey.KeyFlags control, Keys key)
        {
            Handle     = new WindowInteropHelper(win).Handle;
            Window     = win;
            ControlKey = (uint)control;
            Key        = (uint)key;
            KeyId      = (int)ControlKey + (int)Key * 10;

            if (HotKey.KeyPair.ContainsKey(KeyId))
            {
                throw new Exception("热键已经被注册!");
            }

            //注册热键
            if (false == HotKey.RegisterHotKey(Handle, KeyId, ControlKey, Key))
            {
                throw new Exception("热键注册失败!");
            }

            //消息挂钩只能连接一次!!
            if (HotKey.KeyPair.Count == 0)
            {
                if (false == InstallHotKeyHook(this))
                {
                    throw new Exception("消息挂钩连接失败!");
                }
            }
            if (HotKey.KeyPair.Count > 0)
            {
                HotKey.KeyPair.Clear();
            }
            HotKey.KeyPair.Clear();
            //添加这个热键索引
            HotKey.KeyPair.Add(KeyId, this);
        }
Пример #2
0
        public HotKey(Window win, HotKey.KeyFlags control, Keys key)
        {
            Handle = new WindowInteropHelper(win).Handle;
            window = win;

            Controlkey = (uint)control;
            Key        = (uint)key;
            KeyId      = (int)Controlkey + (int)Key * 10;


            if (HotKey.KeyPair.ContainsKey(KeyId))
            {
                throw new Exception("AlreadyRegisterd");
            }


            if (false == HotKey.RegisterHotKey(Handle, KeyId, Controlkey, Key))
            {
                throw new Exception("RegisterFailed!");
            }


            if (HotKey.KeyPair.Count == 0)
            {
                if (false == InstallHotKeyHook(this))
                {
                    throw new Exception("HookFailed!");
                }
            }

            HotKey.KeyPair.Add(KeyId, this);
        }
Пример #3
0
        //读取以前的注册记录
        public static void ReadKeyHistory()
        {
            if (File.Exists(path))
            {
                StreamReader streamReader = new StreamReader(path);
                string       line         = streamReader.ReadLine();
                if (line != null)
                {
                    string[]        keyInfo = line.Split(':');
                    HotKey.KeyFlags control = HotKey.KeyFlags.MOD_ALT;
                    switch (int.Parse(keyInfo[0]))
                    {
                    case 0: control = HotKey.KeyFlags.MOD_ALT; break;

                    case 1: control = HotKey.KeyFlags.MOD_CONTROL; break;

                    case 2: control = HotKey.KeyFlags.MOD_SHIFT; break;

                    case 3: control = HotKey.KeyFlags.MOD_WIN; break;
                    }
                    RegisterHotKey(control, (System.Windows.Forms.Keys)((int)keyInfo[1].ToCharArray()[0]));
                }
                else
                {
                    RegisterHotKey(HotKey.KeyFlags.MOD_ALT, System.Windows.Forms.Keys.A);
                }
                streamReader.Close();
            }
            else  //注册默认热键 Alt+A
            {
                RegisterHotKey(HotKey.KeyFlags.MOD_ALT, System.Windows.Forms.Keys.A);
            }
        }
Пример #4
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="win">注册窗体</param>
        /// <param name="control">控制键</param>
        /// <param name="key">主键</param>
        public HotKey(Window win, HotKey.KeyFlags control, Keys key)
        //构造函数,注册热键
        {
            Handle     = new WindowInteropHelper(win).Handle;
            window     = win;
            Controlkey = (uint)control;
            Key        = (uint)key;
            KeyId      = (int)Controlkey + (int)Key * 10;
            if (HotKey.KeyPair.ContainsKey(KeyId))
            {
                LogUtils.writeLog("HotKey:59:热键已经被注册");
                throw new Exception("热键已经被注册!");
            }

            //注册热键
            if (false == HotKey.RegisterHotKey(Handle, KeyId, Controlkey, Key))
            {
                LogUtils.writeLog("HotKey:66:热键注册失败!");
                throw new Exception("热键注册失败!");
            }
            if (HotKey.KeyPair.Count == 0)
            {
                //消息挂钩只能连接一次!!
                if (false == InstallHotKeyHook(this))
                {
                    LogUtils.writeLog("HotKey:74:消息挂钩连接失败!");
                    throw new Exception("消息挂钩连接失败!");
                }
            }

            //添加这个热键索引
            HotKey.KeyPair.Add(KeyId, this);
        }
Пример #5
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="win">注册窗体</param>
        /// <param name="control">控制键</param>
        /// <param name="key">主键</param>
        public HotKey(Window win, HotKey.KeyFlags control, Keys key)
        //构造函数,注册热键
        {
            Handle     = new WindowInteropHelper(win).Handle;
            window     = win;
            Controlkey = (uint)control;
            Key        = (uint)key;
            KeyId      = (int)Controlkey + (int)Key * 10;

            //注册热键
            if (false == HotKey.RegisterHotKey(Handle, KeyId, Controlkey, Key))
            {
                throw new Exception("热键注册失败!");
            }
            if (KeyPair.Count == 0)
            {
                //消息挂钩只能连接一次!!
                if (false == InstallHotKeyHook(this))
                {
                    throw new Exception("消息挂钩连接失败!");
                }
            }

            if (HotKey.KeyPair.ContainsKey(KeyId))
            {
                KeyPair[KeyId] = this;
            }
            else
            {
                KeyPair.Add(KeyId, this);
            }
        }
Пример #6
0
        public static void Unregister(IntPtr Handle, HotKey.KeyFlags control, Keys key)
        {
            // IntPtr handle = new WindowInteropHelper(this).Handle;

            uint Controlkey1 = (uint)control;
            uint Key         = (uint)key;
            int  KeyId       = (int)Controlkey1 + (int)Key * 10;

            HotKey.UnregisterHotKey(Handle, KeyId);
        }
Пример #7
0
        private void ok_Button_Click(object sender, RoutedEventArgs e)
        {
            if (hotKey1_ComboBox.SelectedIndex < 0 || hotKey2_ComboBox.SelectedIndex < 0)
            {
                MessageBox.Show("信息选择不完全!", "提醒", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            HotKey.KeyFlags control = HotKey.KeyFlags.MOD_ALT;
            int             index   = 0;

            switch (hotKey1_ComboBox.SelectedIndex)
            {
            case 0: control = HotKey.KeyFlags.MOD_ALT; break;

            case 1: control = HotKey.KeyFlags.MOD_CONTROL; index = 1; break;

            case 2: control = HotKey.KeyFlags.MOD_SHIFT; index = 2; break;

            case 3: control = HotKey.KeyFlags.MOD_WIN; index = 3; break;
            }

            //撤销先前的热键
            HotKeyFactory.UnregisterHotKey();
            //注册新的热键
            HotKeyFactory.RegisterHotKey(control, (System.Windows.Forms.Keys)hotKey2_ComboBox.SelectedItem);

            if (HotKeyFactory.hotKey.IRightRegistered)
            {
                MessageBox.Show("热键注册成功!");
                FileStream   fs           = new FileStream(HotKeyFactory.path, FileMode.Create, FileAccess.Write);
                StreamWriter streamWriter = new StreamWriter(fs);
                streamWriter.Flush();
                streamWriter.WriteLine(index.ToString() + ":" + hotKey2_ComboBox.SelectedItem.ToString());
                streamWriter.Flush();
                streamWriter.Close();
            }
            else
            {
                MessageBox.Show("热键注册失败!");
            }
        }
Пример #8
0
        public void HeadHotkey(string strHotkey)
        {
            Console.WriteLine(strHotkey);

            if (!string.IsNullOrWhiteSpace(strHotkey) && strHotkey.IndexOf("+") > 0)
            {
                string[] keyarr = strHotkey.Split('+');
                if (keyarr.Length >= 2)
                {
                    HotKey.KeyFlags control = HotKey.KeyFlags.MOD_CONTROL;
                    for (int i = 0; i < keyarr.Length - 1; i++)
                    {
                        if (!"shift ctrl alt".Contains(keyarr[i].ToLower()))
                        {
                            Console.WriteLine("错误的热键!");
                            return;
                        }
                        if (i == 0)
                        {
                            control = GetControlKey(keyarr[i]);
                        }
                        else
                        {
                            control = control | GetControlKey(keyarr[i]);
                        }
                    }
                    System.Windows.Forms.Keys mkey = new HotKeyCollection().GetKeys(keyarr[keyarr.Length - 1]);
                    if (hotKey != null)
                    {
                        hotKey.UnHotKey();
                    }
                    hotKey           = new HotKey(this, control, mkey);
                    hotKey.OnHotKey += new HotKey.OnHotKeyEventHandler(hotKey_OnHotKey);
                }
                else
                {
                    Console.WriteLine("热键长度不足");
                }
            }
        }
Пример #9
0
        public HotKey(Window win, HotKey.KeyFlags control, System.Windows.Forms.Keys key)
        //构造函数,注册热键
        {
            Handle     = new WindowInteropHelper(win).Handle;
            window     = win;
            Controlkey = (uint)control;
            Key        = (uint)key;
            KeyId      = num++;

            if (HotKey.KeyPair.ContainsKey(KeyId))
            {
                IRightRegistered = false;
                MessageBox.Show("热键已经被注册!");
                return;
            }

            //注册热键
            if (false == HotKey.RegisterHotKey(Handle, KeyId, Controlkey, Key))
            {
                IRightRegistered = false;
                MessageBox.Show("热键注册失败!");
                return;
            }

            if (HotKey.KeyPair.Count == 0)
            {
                //消息挂钩只能连接一次!!
                if (false == InstallHotKeyHook(this))                       //创建
                {
                    IRightRegistered = false;
                    MessageBox.Show("消息挂钩连接失败!");
                    return;
                }
            }

            //添加这个热键索引
            HotKey.KeyPair.Add(KeyId, this);
        }
Пример #10
0
 //热键注册
 public static void RegisterHotKey(HotKey.KeyFlags control, System.Windows.Forms.Keys key)
 {
     hotKey           = new HotKey(Application.Current.MainWindow, control, key);
     hotKey.OnHotKey += new HotKey.OnHotkeyEventHandeler(hotKey_OnHotKey);
 }