获取KeyPress角色

时间:2011-12-21 11:28:29

标签: .net wpf richtextbox

我只想在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

但我只有keyDown事件。在Keydown,抓住正确的角色变得非常复杂。大写/小写和特殊键如“SHIFT”由keydown触发,我首先需要计算到按下的键。那太复杂了。 因为在大多数情况下WPF不那么复杂,我会问你是否知道如何将键盘上的按键作为字符串或字符串?

此致

2 个答案:

答案 0 :(得分:2)

尝试使用PreviewTextInput事件:

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

希望有所帮助

答案 1 :(得分:0)

有一个TextInput事件可能对您有帮助。