如何阻止 Control-I 在 CoreWindow 范围内的 UWP TextBox 中插入选项卡? [英] How to stop Control-I from inserting a tab in a UWP TextBox at CoreWindow scope?

查看:20
本文介绍了如何阻止 Control-I 在 CoreWindow 范围内的 UWP TextBox 中插入选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 UWP 应用中使用 TextBox 时的奇怪行为(对我而言).

Strange behavior (to me) when I have a TextBox in a UWP app.

  • 在 Windows 10 中创建一个通用的空白应用 UWP 应用.
  • 使用以下代码将文本框添加到默认网格:
    <TextBox Text="里面没有太多垃圾邮件" Name="textBox"/>
  • 构建并运行应用程序.
  • 点击文本框内部
  • 输入 Control-I.

插入了一个制表符.

是什么导致了这种行为?

What causes this behavior?

我试图通过将处理程序连接到 Window.Current.CoreWindow.KeyDown 来在 Windows 10 应用程序中设置窗口级快捷方式,但我认为在事件游戏中为时已晚捕获 Control-I 的选项卡插入并调用它处理.那,我认为在 CoreWindow 的事件发生之前,TextBox 的 KeyDown 已经完全处理了.

I'm trying to set up window level shortcuts in a Windows 10 application by hooking up a handler to Window.Current.CoreWindow.KeyDown, but I think that's too late in the event game to catch the Control-I's tab insertion and call it handled. That, I think the TextBox's KeyDown is completely handled before the CoreWindow's event occurs.

所以现在我正在处理每个 TextBox 上的事件,每当有人选择 Control-I like this...

So for now I'm settling for handling the event on each TextBox whenever someone selects Control-I like this...

将 XAML 编辑为:

Edit XAML to this:

<TextBox Text="There's not much spam in it" 
    Name="textBox" KeyDown="textBox_KeyDown" AcceptsReturn="True" />

添加事件处理程序:

private void textBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.OriginalKey.Equals(VirtualKey.I) 
        && Window.Current.CoreWindow.GetKeyState(VirtualKey.Control)
        .HasFlag(CoreVirtualKeyStates.Down))
    {
        this.textBox.SelectedText += "X";
        e.Handled = true;
    }
}

...这似乎工作正常,除了现在我必须将 Control-I 的窗口"范围事件处理程序打到每个 TextBox 中(或扩展 TextBox,这两个看起来都很疯狂似乎是一个非标准的键盘快捷键).

... which seems to work okay, except that now I have to slap my "window" scope event handler for Control-I into each TextBox (or extend TextBox, both of which seem crazy for what seems to be a non-standard keyboard shortcut).

所以我主要是问在按下 Control-I 时将 Tab 插入到 TextBox 是什么操作,以及是否有更好的方法来避免在 CoreWindow 级别执行该操作.在 CoreWindow 级别捕获键盘快捷键的更好方法(例如,Ctrl-K 表示插入链接)也将受到赞赏,但不会真正回答真实"问题.

So primarily I'm asking what action it is that inserts a Tab into a TextBox when Control-I is pressed, and if there's a better way to avoid that action at the CoreWindow level. A better means of capturing keyboard shortcuts at the CoreWindow level (eg, Ctrl-K means insert a link) would also be appreciated, but wouldn't really answer the "real" question.

推荐答案

迄今为止我发现的最佳解决方案是为 KeyDown 插入一个事件处理程序,它会吃掉 Ctrl-I.

The best solution I've found to date is to insert an event handler for KeyDown that will eat Ctrl-I.

我在 GitHub 上有一个更完整的解决方案来解决这个问题和其他一些问题这里,但这里是操作 Ctrl-I 进食代码:

I have a fuller solution on GitHub that addresses this and some other issues here, but here's the operative Ctrl-I eating code:

public UWPBox() : base()
{
    this.KeyDown += this.KeyDownHandler;
}

public virtual async void KeyDownHandler(object sender, KeyRoutedEventArgs e)
{
    bool isCtrlDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control)
        .HasFlag(CoreVirtualKeyStates.Down);

    try
    {
        switch (e.OriginalKey)
        {
            case VirtualKey.I:
                // First, kill the default "Ctrl-I inserts a tab" action.
                if (isCtrlDown)
                {
                    e.Handled = true;
                    this.HandleCtrlI(); // Just in case we want to do 
                                        // something different with Ctrl-I
                }
                break;

            // "Fixes" for Ctrl-V and Tab removed.
            // Fuller solution here: https://github.com/ruffin--/UWPBox
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(
            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " 
            + ex.Message);
    }
}

public virtual void HandleCtrlI()
{
    System.Diagnostics.Debug.WriteLine("Ctrl-I pressed.");
}

这篇关于如何阻止 Control-I 在 CoreWindow 范围内的 UWP TextBox 中插入选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆