简体   繁体   English

如何从 WPF 页面触发 KeyDown 事件

[英]How do i get a KeyDown event to fire from a WPF Page

Goal: From a WPF Page, I want a user to be able to press a pre-determined sequence of keys that will fire an event causing a control(button) to appear.目标:从 WPF 页面,我希望用户能够按下预定的按键序列,这些按键将触发导致控件(按钮)出现的事件。 I have looked at everything I can find for KeyDown and Keyboard.KeyDown events and they don't seem to fire the event in the code behind.我已经查看了我能找到的 KeyDown 和 Keyboard.KeyDown 事件的所有内容,但它们似乎没有在后面的代码中触发该事件。

XAML: XAML:

<Page x:Class="KioskClient.Begin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
xmlns:local="clr-namespace:KioskClient"
xmlns:uc="clr-namespace:KioskClient"

xmlns:controls="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

mc:Ignorable="d" 
d:DesignHeight="450" d:DesignWidth="800"    
Background="Black" 
Title="SIMS Check In"
Keyboard.KeyDown="Page_KeyDown"
>

<DockPanel LastChildFill="True" Background="Black">

<!--Since you always want the Back, Next, Cancel button to be docked to 
the bottom of the screen, put them first in the Dockpanel and dock them, 
the other objects then fill the space.-->
<controls:UniformGrid Margin="100" Rows="1"
HorizontalAlignment="Center"
VerticalAlignment="Bottom" Background="Black" DockPanel.Dock="Bottom">
</DockPanel>
</Page>

C#: C#:

private void Page_KeyDown(object sender, KeyEventArgs e)私有无效 Page_KeyDown(对象发送者,KeyEventArgs e)

I know what to do once I can get the event to fire but the event is not firing.我知道一旦我可以触发事件但事件没有触发该怎么办。 I do not want to have to focus on anything but the Page itself which I set 'this.Focusable = true;'我不想只关注页面本身,我设置了'this.Focusable = true;' in the constructor.在构造函数中。 Obviously, I am missing something.显然,我错过了一些东西。 Can anyone tell me what it is?谁能告诉我它是什么?

Use Keyboard.KeyDown instead.请改用Keyboard.KeyDown That should solve your problem.那应该可以解决您的问题。

<Window ...
    Keyboard.KeyDown="Page_KeyDown">
    ...
</Window>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM