Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all

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

VB
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.
Posted
Comments
KM Perumal 3-May-13 3:58am    
Nice concept ...i try to find problem

1 solution

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/[^]
 
Share this answer
 
v3
Comments
[no name] 3-May-13 4:24am    
how to do that...
Pheonyx 3-May-13 4:26am    
How do you load your user control?
[no name] 3-May-13 4:27am    
i have a home page where i gave the link for the user control..
Pheonyx 3-May-13 4:38am    
What code do you use to load it? Is it embedded within a grid, when you say home page is this a web application?
[no name] 3-May-13 5:24am    
No i have taken a menu in which i gave the links for each user controls..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900