Exemple #1
0
        void LoadRegisteredKeys()
        {
            if (Settings.Default.RegisteredKeys == "")
            {
                return;
            }

            for (int i = 0; i < keysets.Count; i++)
            {
                UnregisterHotKey(this.Handle, i);
            }
            keysets = new Dictionary <int, string>();
            foreach (string elementset in Settings.Default.RegisteredKeys.Split(';'))
            {
                Console.WriteLine(elementset);
                if (elementset == "")
                {
                    return;
                }
                string[] split = elementset.Split('|');
                Console.WriteLine("test: '" + split[0] + "'");
                KeyConfig conf = new KeyConfig(Int32.Parse(split[0]));
                keysets.Add(conf.keycode, split[1]);
                lbKeys.Items.Add(conf.ToString("- " + split[1]));
                RegisterHotKey(this.Handle, keysets.Count - 1, conf.modifiers, (int)conf.key);
            }
        }
        private void CheckKey(KeyConfig config, bool isKeyDown)
        {
            if (Settings.Current.KeyActions == null)
            {
                return;
            }
            if (isKeyDown)
            {
                //CurrentKeyConfigs.Clear();
                CurrentKeyConfigs.Add(config);
                Debug.Log("押:" + config.ToString());
                var doKeyActions = new List <KeyAction>();
                foreach (var action in Settings.Current.KeyActions?.OrderBy(d => d.KeyConfigs.Count()))
                {//キーの少ない順に実行して、同時押しと被ったとき同時押しを後から実行して上書きさせる
                 //if (action.KeyConfigs.Count == CurrentKeyConfigs.Count)
                 //{ //別々の機能を同時に押す場合もあるのでキーの数は見てはいけない
                    var enable = true;
                    foreach (var key in action.KeyConfigs)
                    {
                        if (CurrentKeyConfigs.Where(d => d.IsEqualKeyCode(key) == true).Any() == false)
                        {
                            //キーが含まれてないとき
                            enable = false;
                        }
                    }
                    if (enable)
                    {//現在押してるキーの中にすべてのキーが含まれていた
                        if (action.IsKeyUp)
                        {
                            //キーを離す操作の時はキューに入れておく
                            CurrentKeyUpActions.Add(action);
                        }
                        else
                        {
                            doKeyActions.Add(action);
                        }
                    }
                    //}
                }
                if (doKeyActions.Any())
                {
                    var tmpActions = new List <KeyAction>(doKeyActions);
                    foreach (var action in tmpActions)
                    {
                        foreach (var target in tmpActions.Where(d => d != action))
                        {
                            if (target.KeyConfigs.ContainsArray(action.KeyConfigs))
                            {//更に複数押しのキー設定が有効な場合、少ないほうは無効(上書きされてしまうため)
                                doKeyActions.Remove(action);
                            }
                        }
                    }
                    foreach (var action in doKeyActions)
                    {//残った処理だけ実行
                        controlWPFWindow.DoKeyAction(action);
                    }
                }
            }
            else
            {
                CurrentKeyConfigs.RemoveAll(d => d.IsEqualKeyCode(config)); //たまに離し損ねるので、もし押しっぱなしなら削除
                Debug.Log("離:" + config.ToString());
                //キーを離すイベントのキューチェック
                var tmpActions = new List <KeyAction>(CurrentKeyUpActions);
                foreach (var action in tmpActions)
                {//1度押されたキーなので、現在押されてないキーなら離れたことになる
                    var enable = true;
                    foreach (var key in action.KeyConfigs)
                    {
                        if (CurrentKeyConfigs.Where(d => d.IsEqualKeyCode(key) == true).Any() == true) //まだ押されてる
                        {
                            enable = false;
                        }
                    }
                    if (enable)
                    {
                        //手の操作の場合、別の手の操作キーが押されたままだったらそちらを優先して、離す処理は飛ばす
                        var skipKeyUp = false;

                        var doKeyActions = new List <KeyAction>();
                        //手の操作時は左手と右手は分けて処理しないと、右がおしっぱで左を離したときに戻らなくなる
                        foreach (var downaction in Settings.Current.KeyActions?.OrderBy(d => d.KeyConfigs.Count()).Where(d => d.FaceAction == action.FaceAction && d.HandAction == action.HandAction && d.Hand == action.Hand && d.FunctionAction == action.FunctionAction))
                        {//キーの少ない順に実行して、同時押しと被ったとき同時押しを後から実行して上書きさせる
                         //if (action.KeyConfigs.Count == CurrentKeyConfigs.Count)
                         //{ //別々の機能を同時に押す場合もあるのでキーの数は見てはいけない
                            var downenable = true;
                            foreach (var key in downaction.KeyConfigs)
                            {
                                if (CurrentKeyConfigs.Where(d => d.IsEqualKeyCode(key) == true).Any() == false)
                                {
                                    //キーが含まれてないとき
                                    downenable = false;
                                }
                            }
                            if (downenable)
                            {//現在押してるキーの中にすべてのキーが含まれていた
                                if (downaction.IsKeyUp)
                                {
                                }
                                else
                                {
                                    doKeyActions.Add(downaction);
                                }
                            }
                            //}
                        }
                        if (doKeyActions.Any())
                        {
                            skipKeyUp = true; //優先処理があったので、KeyUpのActionは無効
                            var tmpDownActions = new List <KeyAction>(doKeyActions);
                            foreach (var downaction in tmpDownActions)
                            {
                                foreach (var target in tmpActions.Where(d => d != downaction))
                                {
                                    if (target.KeyConfigs.ContainsArray(downaction.KeyConfigs))
                                    {//更に複数押しのキー設定が有効な場合、少ないほうは無効(上書きされてしまうため)
                                        doKeyActions.Remove(downaction);
                                    }
                                }
                            }
                            foreach (var downaction in doKeyActions)
                            {//残った処理だけ実行
                                controlWPFWindow.DoKeyAction(downaction);
                            }
                        }
                        if (skipKeyUp == false)
                        {
                            controlWPFWindow.DoKeyAction(action);
                        }
                        CurrentKeyUpActions.Remove(action);
                    }
                }
            }
        }