[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

Catxhing Key strokes

Hi,

I am trying to construct an eventhandler that allways fires when a key is pressed. I made a try and it fires when the app starts upp. As soon as another control has focus it doesn't fire. What am I doing wrong?

Se the attached code snippet!

Best regards
RTSol
public Window1()
        {
            InitializeComponent();

            lb.Visibility = Visibility.Hidden;
            lb.Items.Add("Value1");
            lb.Items.Add("Value2");
            lb.Items.Add("Value3");
            lb.Items.Add("Value4");

            mainWindow.KeyDown += new System.Windows.Input.KeyEventHandler(keyD);
        }

        void keyD(object sender, System.Windows.Input.KeyEventArgs e)
        {

            switch (e.Key)
            {
                case Key.Down:
                    int ic = lb.Items.Count;
                    if (ic > 0)
                    {
                        if (lb.SelectedIndex == -1 || lb.SelectedIndex == ic - 1)
                        {
                            lb.SelectedIndex = 0;
                            lb.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            lb.SelectedIndex += 1;
                        }
                    }
                    break;
                case Key.Right:
                    int ic3 = lb.Items.Count;
                    if (lb.SelectedIndex != -1)
                    {
                        txtb.Text = lb.SelectedValue.ToString();
                        txtb2.Focus();
                        lb.Visibility = Visibility.Hidden;
                    }
                    break;
                case Key.Up:
                    int ic2 = lb.Items.Count;
                    if (ic2 > 0)
                    {
                        if (lb.SelectedIndex == -1 || lb.SelectedIndex == 0)
                        {
                            lb.SelectedIndex = ic2 - 1;
                            lb.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            lb.SelectedIndex -= 1;
                        }
                    }
                    break;
                default:
                    break;
            }
        }

Open in new window

0
RTSol
Asked:
RTSol
  • 2
  • 2
1 Solution
 
Todd GerbertIT ConsultantCommented:
Set KeyPreview = true on your form, I think is what you want.
0
 
RTSolAuthor Commented:
I cant see how I can get this working in a WPF project. Can you explain?

-RTSol
0
 
Todd GerbertIT ConsultantCommented:
Sorry, didn't catch that it was WPF.  Try handling the PreviewKeyDown event instead.

mainWindow.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(keyD);
0
 
RTSolAuthor Commented:
Thanks!
Just what I needed.
-RTSol
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now