protected override void OnKeyDown(KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Space) { FormatText(); Document.GetText(TextGetOptions.NoHidden, out string text); if (MessageHelper.IsValidUsername(text)) { ViewModel.ResolveInlineBot(text); } } else if ((e.Key == VirtualKey.Up || e.Key == VirtualKey.Down || e.Key == VirtualKey.PageUp || e.Key == VirtualKey.PageDown || e.Key == VirtualKey.Tab)) { var alt = Window.Current.CoreWindow.GetKeyState(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down); var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down); var shift = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down); if (e.Key == VirtualKey.Up && !alt && !ctrl && !shift && IsEmpty) { ViewModel.MessageEditLastCommand.Execute(); e.Handled = true; } if ((e.Key == VirtualKey.Up && alt) || (e.Key == VirtualKey.PageUp && ctrl) || (e.Key == VirtualKey.Tab && ctrl && shift)) { ViewModel.Aggregator.Publish("move_up"); e.Handled = true; } else if ((e.Key == VirtualKey.Down && alt) || (e.Key == VirtualKey.PageDown && ctrl) || (e.Key == VirtualKey.Tab && ctrl && !shift)) { ViewModel.Aggregator.Publish("move_down"); e.Handled = true; } else if (e.Key == VirtualKey.Up || e.Key == VirtualKey.Down) { if (UsernameHints != null && ViewModel.UsernameHints != null) { UsernameHints.SelectionMode = ListViewSelectionMode.Single; var index = e.Key == VirtualKey.Up ? -1 : 1; var next = UsernameHints.SelectedIndex + index; if (next >= 0 && next < ViewModel.UsernameHints.Count) { UsernameHints.SelectedIndex = next; UsernameHints.ScrollIntoView(UsernameHints.SelectedItem); } e.Handled = true; } else if (BotCommands != null && ViewModel.BotCommands != null) { BotCommands.SelectionMode = ListViewSelectionMode.Single; var index = e.Key == VirtualKey.Up ? -1 : 1; var next = BotCommands.SelectedIndex + index; if (next >= 0 && next < ViewModel.BotCommands.Count) { BotCommands.SelectedIndex = next; BotCommands.ScrollIntoView(BotCommands.SelectedItem); } e.Handled = true; } } } else if (e.Key == VirtualKey.Escape && ViewModel.Reply is TLMessagesContainter container && container.EditMessage != null) { ViewModel.ClearReplyCommand.Execute(); e.Handled = true; } if (!e.Handled) { base.OnKeyDown(e); } }
//protected override async void OnKeyDown(KeyRoutedEventArgs e) //{ // if (e.Key == VirtualKey.Enter) // { // // Check if CTRL or Shift is also pressed in addition to Enter key. // var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control); // var shift = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift); // // If there is text and CTRL/Shift is not pressed, send message. Else allow new row. // if (!ctrl.HasFlag(CoreVirtualKeyStates.Down) && !shift.HasFlag(CoreVirtualKeyStates.Down) && !IsEmpty) // { // e.Handled = true; // await SendAsync(); // } // } // base.OnKeyDown(e); //} private async void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args) { if (args.VirtualKey == VirtualKey.Enter && FocusState != FocusState.Unfocused) { // Check if CTRL or Shift is also pressed in addition to Enter key. var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control); var shift = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift); var key = Window.Current.CoreWindow.GetKeyState(args.VirtualKey); if (UsernameHints != null && ViewModel.UsernameHints != null) { var send = key.HasFlag(CoreVirtualKeyStates.Down) && !ctrl.HasFlag(CoreVirtualKeyStates.Down) && !shift.HasFlag(CoreVirtualKeyStates.Down); if (send) { AcceptsReturn = false; var container = UsernameHints.ContainerFromIndex(Math.Max(0, UsernameHints.SelectedIndex)) as ListViewItem; if (container != null) { var peer = new ListViewItemAutomationPeer(container); var invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider; invokeProv.Invoke(); } } else { AcceptsReturn = true; } return; } if (BotCommands != null && ViewModel.BotCommands != null) { var send = key.HasFlag(CoreVirtualKeyStates.Down) && !ctrl.HasFlag(CoreVirtualKeyStates.Down) && !shift.HasFlag(CoreVirtualKeyStates.Down); if (send) { AcceptsReturn = false; var container = BotCommands.ContainerFromIndex(Math.Max(0, BotCommands.SelectedIndex)) as ListViewItem; if (container != null) { var peer = new ListViewItemAutomationPeer(container); var invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider; invokeProv.Invoke(); } } else { AcceptsReturn = true; } return; } // If there is text and CTRL/Shift is not pressed, send message. Else allow new row. if (ApplicationSettings.Current.IsSendByEnterEnabled) { var send = key.HasFlag(CoreVirtualKeyStates.Down) && !ctrl.HasFlag(CoreVirtualKeyStates.Down) && !shift.HasFlag(CoreVirtualKeyStates.Down); if (send) { AcceptsReturn = false; await SendAsync(); } else { AcceptsReturn = true; } } else { var send = key.HasFlag(CoreVirtualKeyStates.Down) && ctrl.HasFlag(CoreVirtualKeyStates.Down) && !shift.HasFlag(CoreVirtualKeyStates.Down); if (send) { AcceptsReturn = false; await SendAsync(); } else { AcceptsReturn = true; } } } }