首页 > 解决方案 > 如何在 UWP 中使用 TextBox 的 KeyDown 事件捕获 Ctrl-V 或启用右键单击粘贴选项

问题描述

我正在尝试在用户按下 Ctrl + V 快捷方式或右键单击并选择粘贴选项时将图像粘贴到 TextBox 中。我尝试了这种方法来捕获键盘键,但它只有在我输入 V 时才有效

if(e.Key == Windows.System.VirtualKey.V)

如何捕获 Ctrl + V 两个键盘键。就我而言,这不起作用

if (e.Key == Windows.System.VirtualKey.V && e.Key == Windows.System.VirtualKey.Control)  

标签: c#uwpkeyboard-shortcuts

解决方案


为了捕获控制键,您需要检查HasFlag属性。

var controlDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
    
if (controlDown & e.Key == Windows.System.VirtualKey.V)
{
    
}

推荐阅读