1

I just want to get the pressed char in WPF RichtTextbox.

Private Sub rtb_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs) Handles rtb.KeyDown
        Dim KeyConverter As New Forms.KeysConverter
        Dim S As String = KeyConverter.ConvertToString(e.Key)

    End Sub

But I only have keyDown event. On Keydown it becomes to be very complicated to catch the right character. Uppercase/Lowercase and special keys like "SHIFT" are fired by keydown, I first need to calculate to the pressed key. Thats complicated. Because WPF is less complicated in most cases, I will ask if you know any way to get the pressed key on keyboard as string or char?

Regards

H.B.
  • 166,899
  • 29
  • 327
  • 400
Nasenbaer
  • 4,810
  • 11
  • 53
  • 86

2 Answers2

2

Try to use the PreviewTextInput event:

public Window7() {
  this.InitializeComponent();
  this.richTextBox.PreviewTextInput += new TextCompositionEventHandler(this.rtb_PreviewTextInput);
}

private void rtb_PreviewTextInput(object sender, TextCompositionEventArgs e) {
  var text = e.Text;
  var systemText = e.SystemText;
  var controlText = e.ControlText;
  var kbm = Keyboard.Modifiers;
}


Private Sub richTextBox_PreviewTextInput(sender As System.Object , e As System.Windows.Input.TextCompositionEventArgs)
    Dim text As String = e.Text
    Dim controlText As String = e.ControlText
    Dim systemText As String = e.SystemText
    Dim keybm As ModifierKeys = Keyboard.Modifiers
End Sub

hope that helps

punker76
  • 14,326
  • 5
  • 58
  • 96
0

There's a TextInput event which might help you here.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • new to me. But `TextInput` event does not fire. I found http://stackoverflow.com/questions/1374710/wpf-textbox-not-firing-ontextinput-event but I wonder if you know how to get the text with TextInput. – Nasenbaer Dec 21 '11 at 12:25
  • Ok, you was on the right way. Thanks for your help. The `PreviewTextInput` solved it! – Nasenbaer Dec 21 '11 at 12:30