As suggested by Mark Hall, it seemed that the CoreWindow.CharacterReceived event could help solve this problem.
So, I found the final answer here .
This is the code from this link:
public Foo() { this.InitializeComponent(); Window.Current.CoreWindow.CharacterReceived += KeyPress; } void KeyPress(CoreWindow sender, CharacterReceivedEventArgs args) { args.Handled = true; Debug.WriteLine("KeyPress " + Convert.ToChar(args.KeyCode)); return; }
But this event will fire anywhere regardless of the current active page. Therefore, I have to delete this event when the user goes to another page, and add it again when the user returns.
Update: I also had to move the text field cursor to the end of the text so that the user could write naturally. Here is my last code:
private void KeyPress(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.CharacterReceivedEventArgs args) { if (SearchBox.FocusState == Windows.UI.Xaml.FocusState.Unfocused) { SearchBox.Text = Convert.ToChar(args.KeyCode).ToString(); SearchBox.SelectionStart = SearchBox.Text.Length; SearchBox.SelectionLength = 0; SearchBox.Focus(FocusState.Programmatic); } } private void pageRoot_GotFocus(object sender, RoutedEventArgs e) { Window.Current.CoreWindow.CharacterReceived += KeyPress; } private void pageRoot_LostFocus(object sender, RoutedEventArgs e) { Window.Current.CoreWindow.CharacterReceived -= KeyPress; }
source share