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.