简体   繁体   中英

How to compare KeyEventArgs to a character?

I have a TextEdit.PreviewKeyDown method where I want to check if an input character is equal to a special character of my choosing.

For an instance I want to check if the user's input character is '#'. I'd use:

if(e.Key == Key.D3 && (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)

The problem is I don't know what the special character will be at the time and I can't possibly program all the cases, so I wanted something like:

string strSpecChar = GetSpecialCharacter();

if(strSpecChar = e.Key) {do_sth();}

Which doesn't work at all, because if I press, for example '3', the e.Key value will be equal to 'D3', so if my strSpecChar value is '3' I can't get to the do_sth()

I have browsed through the System.Windows.Input.KeyEventArgs docs and I've found nothing and I've also read some discouraging posts on other forums.

Is there any way I would be able to compare the PreviewKeyDown e and string strSpecChar?

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    string strNewLigneSign = String.Empty;
    if (InsertTextDialogHelper != null)
        strNewLigneSign = InsertTextDialogHelper.GetNewLineSign();

    if (e.Key.Equals(strNewLigneSign))
    {
        if (!String.IsNullOrEmpty(strNewLigneSign))
        {
            int strCaretPosition = TextBox.CaretIndex;
            e.Handled = true;

            string strNewText = "\r\n";
            CurrentDialogData.TextValue = CurrentDialogData.TextValue.Insert(strCaretPosition, strNewText);
            TextBox.CaretIndex = strCaretPosition + strNewText.Length;
        }
    }
}

EDIT:

As per @mm8's suggestion I tried to implement it in the TextEdit.PreviewTextInput property as follows:

XAML

<dxe:TextEdit Name="TextBox"
    PreviewTextInput="TextBox_PreviewTextInput" \">
</dxe:TextEdit>

C#

  private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  {
     string strNewLigneSign = String.Empty;
     if(InsertTextDialogHelper != null)
        strNewLigneSign = InsertTextDialogHelper.GetNewLineSign();

     if(!String.IsNullOrEmpty(strNewLigneSign))
     {
        if(e.Text.Contains(strNewLigneSign))
        {
           int strCaretPosition = TextBox.CaretIndex;
           e.Handled = true;

           string strNewText = Unhashify(e.Text);
           CurrentDialogData.TextValue =  CurrentDialogData.TextValue.Insert(strCaretPosition, strNewText);
           TextBox.CaretIndex = strCaretPosition + strNewText.Length;
        }
     }
  }

However when I run the app and set the breakpoints anywhere inside that method it seems never to go inside it. The exact same solution worked for me back when I used it in wpf's TextBox, but as soon as I switched to devexpress, the TextBox_PreviewTextInput method is never called.

A key is a key and not a character. It's eventually mapped to a character depending on the input device.

You may want to consider handling PreviewTextInput and check the value of Text[0] of the TextCompositionEventArgs instead of handling PreviewKeyDown .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM