コード例 #1
0
ファイル: Switcher.cs プロジェクト: vrd/AutoMova
        private void OnKeyPress(KeyboardEventArgs evtData)
        {
            var vkCode = evtData.KeyCode;

            Debug.WriteLine($"\nOnKeyPress: KeyCode={vkCode.ToString("x")}, KeyData={evtData.KeyData.ToString("x")}, KeyData={evtData.KeyData.ToString()}");
            //return;
            if (evtData.Equals(settings.SwitchLayoutHotkey))
            {
                readyToSwitch = true;
                return;
            }

            readyToSwitch = false;

            if (evtData.Equals(settings.ConvertLastHotkey))
            {
                Debug.WriteLine("ConvertLastHotkey detected!");
                manualSwitchingIsGoing = true;
                ConvertLast("next");
                manualSwitchingIsGoing = false;
                evtData.Handled        = true;
                return;
            }

            if (evtData.Equals(settings.ConvertSelectionHotkey))
            {
                Debug.WriteLine("ConvertSelectionHotkey detected!");
                //return;
                ConvertSelection();
                evtData.Handled = true;
                return;
            }

            if (evtData.Equals(settings.ToggleAutoSwitchingHotkey))
            {
                Debug.WriteLine("ToggleAutoSwitchingHotkey detected!");
                settings.AutoSwitching = !settings.AutoSwitching;
                evtData.Handled        = true;
                return;
            }

            if (this.KeepTrackingKeys(evtData))
            {
                return;
            }

            var notModified = !this.HaveModifiers(evtData);

            if (vkCode == Keys.Back && notModified)
            {
                RemoveLast();
                return;
            }

            if (IsPrintable(evtData) || (vkCode == Keys.Space && notModified))
            {
                var currentLayout = layoutToLang[LowLevelAdapter.GetCurrentLayout()];
                if (WordEnded())
                {
                    langStatistics[currentLayout] += 1;
                }
                if (settings.SmartSelection == false && GetPreviousVkCode() == Keys.Space)
                {
                    BeginNewSelection();
                }
                AddToCurrentSelection(evtData);
                if (IsPunctuation(evtData, currentLayout))
                {
                    return;
                }
                if (!autoSwitchingIsGoing && !manualSwitchingIsGoing && currentSelection.Count > 1)
                {
                    var suggestedLayout = SuggestedLang();
                    if (suggestedLayout == null)
                    {
                        suggestedLayout = currentLayout;
                    }
                    var detectedLayout = layoutDetector.Decision(lastWord, suggestedLayout);
                    Debug.WriteLine($"Current layout: {currentLayout}, detected layout: {detectedLayout}");
                    if (settings.AutoSwitching == true && detectedLayout != currentLayout)
                    {
                        autoSwitchingIsGoing = true;
                        evtData.Handled      = true;
                        ConvertLast(detectedLayout);
                        autoSwitchingIsGoing = false;
                    }
                }

                return;
            }

            // default:
            BeginNewSelection();
            ClearLangStatistics();
        }