简体   繁体   English

当用户在带有 AcceptsReturn = "True" 的 UWP 文本框中按 CTRL+ENTER 键时如何避免换行

[英]How to avoid newline when user presses CTRL+ENTER keys in a UWP TextBox with AcceptsReturn = "True"

I'm working on a text editing UwP Desktop application that needs to add a line when the user presses ENTER, and perform another procedure when the user presses CTRL + ENTER.我正在开发一个文本编辑 UwP 桌面应用程序,它需要在用户按下 ENTER 时添加一行,并在用户按下 CTRL + ENTER 时执行另一个过程。 The problem is to delete the new line that is also created in the second case.问题是删除在第二种情况下也创建的新行。 How to prevent this from happening?如何防止这种情况发生?

    KeyEventHandler keyeventHandler = new KeyEventHandler(rtbText_KeyDown);

    rtbText.AddHandler(TextBox.KeyDownEvent, keyeventHandler, true);

    private static bool IsCtrlKeyPressed()
    {
        var ctrlState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Control);
        return (ctrlState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
    }

    private void rtbText_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (IsCtrlKeyPressed())
        {
            switch (e.Key)
            {
                case VirtualKey.Enter: 
                    NviNew_Tapped(nviNew, null);
                    e.Handled = true; 
                    break;
            }
        }
    }

You can use PreviewKeyDown Event as keydown event will not fire for system handled keys您可以使用PreviewKeyDown事件,因为keydown事件不会为系统处理的键触发

    private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down)&& e.Key == Windows.System.VirtualKey.Enter)
        {
            e.Handled = false;
        }
    }

As @Vignesh said, you can use PreviewKeyDown Event instead of keydown event and set the event as "handled" like e.Handled = true to prevent adding the new line.正如@Vignesh 所说,您可以使用PreviewKeyDown事件而不是 keydown 事件,并将事件设置为“已处理”,如e.Handled = true以防止添加新行。

KeyEventHandler keyeventHandler = new KeyEventHandler(rtbText_KeyDown);
rtbText.AddHandler(TextBox.PreviewKeyDownEvent, keyeventHandler, true);

Or based on this document , you can change how your TextBox reacts to key input by overriding Control.OnKeyDown .或者基于此文档,您可以通过覆盖Control.OnKeyDown来更改 TextBox 对键输入的反应方式。 First, declare a custom class inherits from TextBox and override OnKeyDown event.首先,声明一个继承自 TextBox 的自定义类并覆盖 OnKeyDown 事件。 Then you can continue to use keydown event to do something.然后你可以继续使用keydown事件来做一些事情。

public class MyTextBox : TextBox
{
    protected override void OnKeyDown(KeyRoutedEventArgs e)
    {
        if (Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down) && e.Key == VirtualKey.Enter)
        {
            e.Handled = true;
        }
        else
        {
            base.OnKeyDown(e);
        }
    }
}

.xaml: .xaml:

<local:MyTextBox Width="400" AcceptsReturn="True" x:Name="rtbText"></local:MyTextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当AcceptsReturn为true时,如何使用TextBox处理Ctrl + Enter? - How to handle Ctrl + Enter with TextBox when AcceptsReturn is true? 如何模拟ctrl+enter - How to simulate ctrl+enter 在WPF中处理文本框上的Enter事件时,如何接受Ctrl + Enter作为返回? - How to accept Ctrl+Enter as return while processing Enter event on a Textbox in WPF? 具有AcceptsReturn = true的文本框键绑定 - Textbox KeyBinding with AcceptsReturn = true 使用C#StandardInput重定向时如何将Ctrl + Enter命令传递给Process - How to pass Ctrl+Enter command to Process when using C# StandardInput redirection C# winform 上的 AcceptButton 属性是否可以从 Enter 键更改为使用 Ctrl+Enter 而不使用 TextBox? - Can the AcceptButton property on a C# winform be changed from the Enter key to using Ctrl+Enter without using a TextBox? 在文本框中按 Enter 时如何在 UWP 中执行命令? - How to execute a command in UWP when pressing enter in a textbox? 如果设置为true,AcceptsReturn会禁用多行文本框中Key.Return的检测? - If set to true, AcceptsReturn disables detection of Key.Return in a multiline textbox? 为什么/ n在具有多行且AcceptsReturn设置为True的文本框中不起作用 - Why does /n not function on a TextBox with Multiline and AcceptsReturn set to True WinForms ListBox间歇地更改CTRL + ENTER上的选择 - WinForms ListBox intermittently changes the selection on CTRL+ENTER
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM