Exemplo n.º 1
0
        private string GetLastWord(bool isSpaceKey)
        {
            string txt = InputTextBox.Text;

            int offset = 1;

            if (isSpaceKey && txt[InputTextBox.CaretIndex - 1] == ' ')
            {
                offset = 2;
            }

            int wordStart = txt.LastIndexOf(' ', InputTextBox.CaretIndex - offset);

            if (wordStart == -1)
            {
                wordStart = 0;
            }

            int wordEnd = txt.IndexOf(' ', wordStart + 1);

            if (wordEnd == -1)
            {
                if (InputTextBox.Text.Length > InputTextBox.CaretIndex)
                {
                    wordEnd = InputTextBox.Text.Length;
                }
                else
                {
                    wordEnd = InputTextBox.CaretIndex;
                }
            }
            else if (isSpaceKey)
            {
                wordEnd++;
            }

            string lastWord = txt.Substring(wordStart, wordEnd - wordStart);

            if (!isSpaceKey)
            {
                lastWord = lastWord.TrimStart();
            }

            int i = 0;

            for (; i < lastWord.Length; i++)
            {
                if (lastWord[i] < 256)
                {
                    break;
                }
            }

            if (i > 0)
            {
                return(HindiProcessor.GetReadableWord(lastWord.Substring(0, i)) + lastWord.Substring(i));
            }

            return(lastWord);
        }
Exemplo n.º 2
0
        private void InputTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox txtBox = sender as TextBox;

            if ((txtBox == null) || (txtBox.CaretIndex == 0))
            {
                return;
            }


            bool isSpaceKey = System.Windows.Input.Key.Space == e.Key;

            string lastWord = GetLastWord(isSpaceKey);

            if (e.Key == Key.Back)
            {
                if (lastWord.Any() && lastWord.Last() == ' ')
                {
                    return;
                }
                else if (HindiProcessor.IsHindiWord(lastWord))
                {
                    lastWord = HindiProcessor.GetReadableWord(lastWord.TrimStart());
                }
            }

            if (e.Key == Key.OemPipe)
            {
                string hindiWord = HindiProcessor.GetHindiWord(lastWord);
                ReplaceLastWordWithSelection(lastWord, hindiWord);

                string trans = GetTranslatedText(txtBox.Text);

                return;
            }

            if (isSpaceKey)
            {
                if (!HindiProcessor.IsHindiWord(lastWord))
                {
                    string hindiWord = HindiProcessor.GetHindiWord(lastWord);
                    ReplaceLastWordWithSelection(lastWord, hindiWord);
                }
                else
                {
                    string selectedItem = SuggestionsListBox.SelectedItem?.ToString();
                    ReplaceLastWordWithSelection(lastWord, selectedItem);
                }

                PopupSuggestions.IsOpen = false;
                e.Handled = true;

                return;
            }

            if (System.Windows.Input.Key.Escape == e.Key)
            {
                PopupSuggestions.IsOpen = false;
                return;
            }

            if (PopupSuggestions.IsOpen)
            {
                if (System.Windows.Input.Key.Enter == e.Key)
                {
                    string selectedItem = SuggestionsListBox.SelectedItem?.ToString();
                    ReplaceLastWordWithSelection(lastWord, selectedItem);
                    PopupSuggestions.IsOpen = false;
                    e.Handled = true;
                    return;
                }
                else if (System.Windows.Input.Key.Down == e.Key && !SuggestionsListBox.IsFocused)
                {
                    SuggestionsListBox.SelectionChanged -= SuggestionsListBox_SelectionChanged;

                    SuggestionsListBox.Focus();
                    //SuggestionsListBox.SelectedIndex = 0;

                    SuggestionsListBox.SelectionChanged += SuggestionsListBox_SelectionChanged;
                    return;
                }
            }
            else
            {
            }

            // InputTextBox.KeyUp -= InputTextBox_KeyUp;

            if (!string.IsNullOrEmpty(lastWord))
            {
                var allSuggestions = Shabdkosh.GetSuggestions(lastWord);

                if (allSuggestions.Count > 0)
                {
                    PopupSuggestions.PlacementTarget    = InputTextBox;
                    PopupSuggestions.PlacementRectangle = InputTextBox.GetRectFromCharacterIndex(InputTextBox.CaretIndex, true);
                    PopupSuggestions.IsOpen             = true;
                    PopulateSuggestions(lastWord);
                }
            }

            SuggestionsListBox.SelectedIndex = 0;
            //SuggestionsListBox.Focus();
        }