private void PopulateSuggestions(string lastWord) { if (!string.IsNullOrEmpty(lastWord)) { SuggestionsListBox.SelectionChanged -= SuggestionsListBox_SelectionChanged; SuggestionsListBox.Items.Clear(); foreach (var suggestion in Shabdkosh.GetSuggestions(lastWord)) { SuggestionsListBox.Items.Add(suggestion); } SuggestionsListBox.SelectionChanged += SuggestionsListBox_SelectionChanged; } }
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(); }