捕获按Ctrl-X在WPF的文本框的KeyDown事件 [英] Capturing Ctrl-X with the KeyDown event of a textbox in WPF
本文介绍了捕获按Ctrl-X在WPF的文本框的KeyDown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想,当用户按下<大骨节病> CTRL 骨节病>触发一个事件 - <大骨节病> X 骨节病>使用的KeyDown
事件。这工作正常<大骨节病> CTRL 骨节病> - <大骨节病> D 骨节病>但事件不会触发时<大骨节病> CTRL 骨节病> - <大骨节病> X 骨节病>被按下。我猜这是因为<大骨节病> CTRL 骨节病> - <大骨节病> X 骨节病>是剪切命令。有什么办法来触发一个事件时,<大骨节病> CTRL 骨节病> - <大骨节病> X 骨节病>按
私人无效textBox_KeyDown(对象发件人,发送KeyEventArgs E)
{
如果(e.KeyboardDevice.IsKeyDown(Key.LeftCtrl)|| e.KeyboardDevice.IsKeyDown(Key.RightCtrl))
{
开关(e.Key)
{
情况下Key.D:
//手柄D键
中断;
情况下Key.X:
//手柄X键
中断;
}
}
}
解决方案
您可以覆盖现有的剪切命令:
<预类=郎咸平的XML prettyprint-覆盖>
<文本框>
< TextBox.InputBindings>
<键绑定键为X修饰符=控制命令={结合TestCommand}/>
< /TextBox.InputBindings>
< /文本框>
您需要创建一个的命令虽然。
I'm trying to trigger an event when the user presses ctrl-x using the KeyDown
event. This works fine for ctrl-D but the event doesn't trigger when ctrl-x is pressed. I'm guessing this is because ctrl-x is the "cut" command. Is there any way to trigger an event when ctrl-X is pressed?
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl))
{
switch (e.Key)
{
case Key.D:
//handle D key
break;
case Key.X:
//handle X key
break;
}
}
}
解决方案
You can override the existing cut command:
<TextBox>
<TextBox.InputBindings>
<KeyBinding Key="X" Modifiers="Control" Command="{Binding TestCommand}" />
</TextBox.InputBindings>
</TextBox>
You need to create a command though.
这篇关于捕获按Ctrl-X在WPF的文本框的KeyDown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文