【问题标题】:keyboard shortcut in UWPUWP 中的键盘快捷键
【发布时间】:2018-09-16 18:21:42
【问题描述】:

我在用户控件中定义了一个 KeyDown 事件。目前,当您按 Enter 时它可以工作。我希望它还可以处理 ctrl + v 组合键,以便粘贴复制的文本并执行一些操作。 说明:我的 KeyDown 事件处理程序接收到一个按下的键,如果是 Enter,则获取该控件中的当前焦点(例如,焦点在某个 textBox 上),检查 TextBox "X" 中是否有任何数据,如果有,另一个 TextBox 是自动完成的。而且我需要在文本框“X”中插入文本时,自动完成文本框的其余部分。如何强制程序执行插入并执行我的自动完成代码?

private async void OnKeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key != VirtualKey.Enter) return;

    var focusedElement = FocusManager.GetFocusedElement();

    if (focusedElement == LocationName || focusedElement == AddressTextBox || 
        focusedElement == Latitude || focusedElement == Longitude)
    {
        e.Handled = true;
    }

    if (IsAddressTextValid)
    {
        var mapLocation = await FindFirstMapLocationAsync(AddressTextBox.Text);

        if (mapLocation != null)
        {
            LatitudeText = mapLocation.Point.Position.Latitude.ToString();
            LongitudeText = mapLocation.Point.Position.Longitude.ToString();
        }
    }
}

【问题讨论】:

  • 这也应该通过鼠标右键下拉菜单的“插入”动作来实现
  • 为什么不订阅 TextChanged 事件?
  • 因为在这个文本框中手动输入文本时,会不断调用这个事件

标签: c# .net uwp caliburn.micro


【解决方案1】:

要处理 Ctrl+V,实际上 Justin 有一个 post 可以提供帮助。

 Window.Current.CoreWindow.KeyDown += (s, e) =>
        {
            var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
            if (ctrl.HasFlag(CoreVirtualKeyStates.Down) && e.VirtualKey == VirtualKey.V)
            {
                // do your stuff
                Debug.WriteLine("ctrl+V has been triggered");
            }
            if(e.VirtualKey==VirtualKey.Enter)
            {
                Debug.WriteLine("Enter triggered");
            }

        };

但是对于如何强制更新其他文本框,不太确定你的逻辑。更改文本不适合您?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多