将键盘手势分配给按钮时出现问题 [英] Problem in assigning keyboard shourtcuts to button

查看:92
本文介绍了将键盘手势分配给按钮时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我使用下面的代码将快捷键分配给我的表格



 私有  Sub  UserControl_KeyDown( ByVal 发​​件人作为系统。对象 ByVal  e  As  System.Windows.Input.KeyEventArgs)句柄  MyBase  .KeyDown 
如果 e.Key = Key.F1 那么
Button1_Click(发件人,e)
ElseIf e.Key = Key.F2 然后
btnSaveF2_Click(发件人,e)
ElseIf e.Key = Key.F3 然后
btnFind F3_Click(发件人,e)
ElseIf e.Key = Key.F4 然后
btnEditF4_Click(发件人,e)
ElseIf e.Key = Key.F5 然后
btnDeleteF5_Click(发件人,e)
ElseIf e.Key = Key.F7 然后
btnCancelF7_Click(发件人,e)
ElseIf e.Key = Key.Escape 然后
btnExitEsc_Click(发件人,e)
结束 如果





事情工作正常但问题是在用户控件加载后我必须点击任何一个按钮然后只有键盘快捷键工作...



请告诉我是什么问题。

决方案
当加载用户控件将焦点设置到它。 Key Press事件是一个气泡事件,从当前聚焦的控件沿着控制树传播。因此,如果用户控件已加载但没有焦点,则其下方的控件将接收按键旁边的按键。



至少基于其背后的理论所有发生了什么。



另外,我认为你应该看看PreviewKeyDown事件而不是KeyDown事件,因为只要有东西使用KeyDown它停止冒泡我认为继续冒泡,除非你在事件args中取消它。这里有点错误,但读了这篇文章以了解WPF中的路由事件



http://joshsmithonwpf.wordpress.com / 2007/06/12 / overview-of-routed-events-in-wpf / [ ^ ]


Hi all

I used the below code to assign the shortcut keys to my form

Private Sub UserControl_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
        If e.Key = Key.F1 Then
            Button1_Click(sender, e)
        ElseIf e.Key = Key.F2 Then
            btnSaveF2_Click(sender, e)
        ElseIf e.Key = Key.F3 Then
            btnFindF3_Click(sender, e)
        ElseIf e.Key = Key.F4 Then
            btnEditF4_Click(sender, e)
        ElseIf e.Key = Key.F5 Then
            btnDeleteF5_Click(sender, e)
        ElseIf e.Key = Key.F7 Then
            btnCancelF7_Click(sender, e)
        ElseIf e.Key = Key.Escape Then
            btnExitEsc_Click(sender, e)
        End If



things are working fine but the problem is after the usercontrol loads once i have to click to any one of the button then only the keyboard shortcuts are working...

Please tell me what is the problem.

解决方案

When you load the UserControl set the focus to it. The Key Press event is a bubble event that travels from the currently focused control down the control tree. So if the user control is loaded but not has focus then a control beneath it will receive the key press bypassing it.

At least based on the theory behind it all that is what happens.

Also, I think you should look at the "PreviewKeyDown" event rather than the KeyDown event because as soon as something uses the KeyDown it stops bubbling where as PreviewKeyDown I believe continues to bubble unless you cancel it in the event args. Was slighlty wrong here, but have a read of this to understand routed events in WPF

http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-routed-events-in-wpf/[^]


这篇关于将键盘手势分配给按钮时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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