Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get text input from wpf window

I'm using a key_up event on my window to append the data typed to a string builder.

<Grid x:Name="grdMain" KeyUp="grdMain_KeyUp">
        <Grid.RowDefinitions>...

    private StringBuilder buffer = new StringBuilder();
    private void Window_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Enter)
        {
            buffer.Append(e.Key);
        }

the thing is that the data applied to the buffer is "NumPad4" instead of "4" and "D3" instead of "3"... am i missing something? is there a way to append the data as if it was typed into a textbox? i know that i can convert the data my self but it seems weird that there is no built in way to do so.

like image 678
Yoav Avatar asked Nov 19 '25 04:11

Yoav


1 Answers

You could use TextCompositionManager.TextInput Attached Event.

<Grid x:Name="grdMain"
      TextCompositionManager.TextInput="grid_TextInput">

In TextCompositionEventArgs you will find what you want.

private void grid_TextInput(object sender, TextCompositionEventArgs e)
{
    MessageBox.Show(e.Text);
}
like image 162
LPL Avatar answered Nov 20 '25 19:11

LPL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!