Click here to Skip to main content
15,891,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I Have One Tree View, One Media Element and one Custom Menu Tabs In My WPF Program.
I want My Keyboard have Different Function in Each of them. For Example Up and Down key in Tree view Should Move Between the Item and In The Media Element Should Control the Volume. In Other World I want When the user click on each tab the keyboard Function change?
I use this Code right Know But I cannot control Program with this Correctly. for example if I press Down key the program first change the treeview Item, Then change the valume.

What I have tried:

private void Window_KeyDown(object sender, KeyEventArgs e)
       {
           switch (e.Key)
           {
               case Key.Space: PlayPause(); break;
               case Key.Enter: FullScreenFunction(); break;
               case Key.Up: VolumeBar.Value = VolumeBar.Value + 0.01; break;
               case Key.Down: VolumeBar.Value = VolumeBar.Value - 0.01; break;
               case Key.Right: mediaPlayer.Position = TimeSpan.FromSeconds(VideoPosBar.Value + 5); break;
               case Key.Left: mediaPlayer.Position = TimeSpan.FromSeconds(VideoPosBar.Value - 5); break;
               case Key.M: MuteSoundfunction(); break;
           }
       }
Posted
Updated 11-Oct-20 22:37pm

1 solution

You need to check some other item for each key to see which function to perform. As it stands your cases each perform the same function always. You need something like:
C#
private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (inTreeView) // set this value somewhere to know which cases to use
    {
        switch (e.Key)
        {
        // cases for moving between tree items
        }
    }
    else
    {
        switch (e.Key)
        {
        // cases for volume controls
        }
    }
}
 
Share this answer
 
Comments
link1234 12-Oct-20 8:21am    
Thank you. But What should I Write in the if Body to check if the user click on the treeview or media element?
Richard MacCutchan 12-Oct-20 8:33am    
I have no idea. It is your application so you know what the user is trying to do at any point.
link1234 12-Oct-20 9:51am    
for now when I choose the item in treeview and then click on the play button with mouse the control pass to the play button and the arrow key work correctly for change volume. But when I Click space for run Video, video is run but the control remain in treeview, so the arrow key not worked correctly. how can pass the control from the treeview to the button or window after click space or enter?
Richard MacCutchan 12-Oct-20 9:54am    
As I said above, I have no idea. I do not know anything about how your application works, or is supposed to work. You need to track the focus of your application to know which control is supposed to handle a keypress at any time.
link1234 12-Oct-20 10:34am    
Now i Find My answer with the search of Focus word. thank you.

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