locked
How Do You Make A Keyboard Key Trigger An Event? RRS feed

  • Question

  • I'm creating a basic prototype, and I need for one of the elements on the screen to become visible when the "H" key is pushed on the keyboard, and for that same element to become hidden again when the "H" key is pushed once more. Basically I need the "H" key to work as a toggle button, that turns this element on and off (makes it visible and hidden).

    Is there an easy way of doing this in Blend? If writing code is required, is there anyone who can put the basics of that code in amazingly easy-to-understand language? :)...I'm in no way a C+ expert and dry code usually makes me stare glassy eyed at my monitor.

    Thanks in advance.
    Monday, July 6, 2009 3:26 AM

Answers

  • If its a simple throw away prototype then you could do add the event handler for key down to the layout root and check which key was pressed. You will need to name the element(s) that you want to change the visibility of, in my example i have used the layout root, so you will need to change this to suit your requirements. You could also use a behavior to accomplish the same results which would be a more reusable option.


    <Rich />


    in your Xaml
    <Grid x:Name="LayoutRoot"
              Background="{x:Null}" KeyUp="LayoutRoot_KeyUp">

    in your code behind
    private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
            {
                switch (e.Key)
                {
                        case Key.H:
                        {
                           LayoutRoot.Visibility = LayoutRoot.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
                            break;
                        }
                }
            }

    • Marked as answer by dzinermachine Tuesday, July 7, 2009 9:23 AM
    Monday, July 6, 2009 8:59 AM

All replies

  • If its a simple throw away prototype then you could do add the event handler for key down to the layout root and check which key was pressed. You will need to name the element(s) that you want to change the visibility of, in my example i have used the layout root, so you will need to change this to suit your requirements. You could also use a behavior to accomplish the same results which would be a more reusable option.


    <Rich />


    in your Xaml
    <Grid x:Name="LayoutRoot"
              Background="{x:Null}" KeyUp="LayoutRoot_KeyUp">

    in your code behind
    private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
            {
                switch (e.Key)
                {
                        case Key.H:
                        {
                           LayoutRoot.Visibility = LayoutRoot.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
                            break;
                        }
                }
            }

    • Marked as answer by dzinermachine Tuesday, July 7, 2009 9:23 AM
    Monday, July 6, 2009 8:59 AM
  • I think I understand what you've listed there as far as code...also, I'm making the prototype as a WPF, if that matters.

    Thanks!
    Tuesday, July 7, 2009 9:23 AM
  • Ok, when I try using this code you suggested, I keep getting an error message that says "the name "Key" does not exist in the current context", and it lists the line that has the code "case Key.H:"

    What am I doing wrong?
    Wednesday, July 15, 2009 2:34 AM
  • Hi Byron,

    Can you send me your Xaml and cs files or a zipped up copy of your project please, so that i can try and repro the error. 

    I can then try and fix the issue as i dont see the problem on my machine.

    thanks

    <Rich />
    Tuesday, July 21, 2009 8:40 AM