Anders
Anders

Reputation: 499

Read keys pressed into string

Edit: I rewrote the question to make it understandable.

I'm trying to capture certain pressed keys in my WPF application so I've registered an eventhandler in my main window.

public MainWindow()
{
    InitializeComponent();
    this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
}

The eventhandler itself looks like this.

private void OnButtonKeyDown(object sender, KeyEventArgs e)
{
    // I'd like the char value here.
}

My problem is that I'm not able to extract the char of the key that is pressed. Only the key code. KeyEventArgs obviously does not have such a property.

Is there a solution?

Upvotes: 0

Views: 653

Answers (1)

Beaker
Beaker

Reputation: 2884

I believe you need to use the line e.Key.ToString() to get the character value of the key pressed. Then you can use Char.TryParse(string input, out charValue) to verify you have a valid character and not something like "Numpad0."

Upvotes: 3

Related Questions