1

Im trying to make something happen when a key is pressed but my keyboard doesn't seem to be registering. Im trying to make something happen with any key not specific ones.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace KeyDown
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static IEnumerable<Key> KeysDown()
        {
            foreach (Key key in Enum.GetValues(typeof(Key)))
            {
                if (Keyboard.IsKeyDown(key))
                    yield return key;
            }
        }

        private void MainGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if(KeysDown().Any())
            {
                MessageBox.Show("Key pressed");
            }
        }
    }
}

Nothing happens, the MainGrid is focusable.

Tried this solution: How to detect if any key is pressed but still nothing happens.

Community
  • 1
  • 1
Simon
  • 385
  • 4
  • 19

2 Answers2

1

There is also some discussion on this topic here. There is some issue with focus, try this:

XAML

<Grid x:Name="myGrid" Width="Auto" Height="Auto" KeyDown="MainGrid_KeyDown" Focusable="True" />

Code Behind

namespace KeyDown
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            myGrid.Focus(); // focus programmatically, better put it in OnLoaded event
        }

        private void MainGrid_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show("Key pressed");
        }
    }
}

My opinion is maybe Grid is hollow and is not intended for having focus, insert other controls which are better focusable and add KeyDown event to them. Also check there is some difference between KeyDown and PreviewKeyDown.

Community
  • 1
  • 1
Bad
  • 4,967
  • 4
  • 34
  • 50
0

You need to create a event handler in the constructor of the mainwindow. Something like this:

MainGrid.KeyDown += new RouteEventHandler(MainGrid_KeyDown); 

https://msdn.microsoft.com/en-us/library/ms743596%28v=vs.100%29.aspx

remove the if(KeysDown().Any()) in the event handler. And look what you get from the object sender and KeyEventArgs e.

marko
  • 10,684
  • 17
  • 71
  • 92
  • The type or namespace name 'RouteEventHandler' could not be found (are you missing a using directive or an assembly reference?) System.Windows is being used. – Simon Apr 02 '16 at 07:53
  • Also isn't using the events list in the designer the same? as i made the event handler through there? – Simon Apr 02 '16 at 07:57
  • I think I'm remembering wrong - RouteEventHandler - it may be a just a eventhandler. Yeah, you can declare the events in the designer too. – marko Apr 02 '16 at 08:06
  • I assume you wanted me to use a breakpoint, but the breakpoint never gets hit. – Simon Apr 02 '16 at 08:15
  • But if the if condition is gone, then would the messageBox show. – marko Apr 02 '16 at 08:17
  • Put a another simple event like a click in there for testing – marko Apr 02 '16 at 08:18
  • If condition gone, doesn't work. MouseDown doesn't work.. Added an Ellipse to use the handlers with, MouseDown works KeyDown still doesn't. – Simon Apr 02 '16 at 08:24