0

I'm working ona code editor based on Windows Presentation Foundation(WPF). I wanna automatically add close bracket when the user add an open brakcet. But in System.Windows.Input.Key, I can't find a keycode for brackets. I also need a few more character codes mentioned below in the code section.

//  Brackets, square brackets and curly brackets "("  ")" "[" "]" "{" "}" 
//  Lower/Greater: "<" ">"
//  Equal, quote and single quote " ' =

private void htmlcode_KeyDown(object sender, KeyEventArgs e)
{
   if(e.Key = /* the keycode that I need */)
   {
      htmlcode.Text = htmlcode.Text + "<close variant of key>";
   } 
} 

I need the names of these characters in System.Windows.Input.Key enum.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Burak Yeniçeri
  • 91
  • 2
  • 15
  • 1
    I'd say you haven't tried to search for this in Google... I have just search for these {} and got the solution from SO. Please before posting a question try to find if it's already answered – Juan Carlos Rodriguez Jul 18 '18 at 11:58
  • 2
    @JuanCarlosRodriguez But you won't bother telling OP what you found? I'd also be curious to see the single keycode of e.g. `{`. On my keyboard, it's three consecutive key codes... – Clemens Jul 18 '18 at 12:02
  • I will try to google. – Burak Yeniçeri Jul 18 '18 at 12:04
  • @Clemens https://stackoverflow.com/questions/19937122/c-sharp-keycode-for-and here he asks for {} . There people suggests to have keypress event instead of keydow, take a look – Juan Carlos Rodriguez Jul 18 '18 at 12:07
  • @JuanCarlosRodriguez There's no KeyPress event in WPF, and no KeyCode property in KeyEventArgs. – Clemens Jul 18 '18 at 12:10
  • Possible duplicate of [C# KeyCode for { and }](https://stackoverflow.com/questions/19937122/c-sharp-keycode-for-and) – ASh Jul 18 '18 at 12:11
  • @BurakYeniçeri There are no Key enum values in WPF for these characters. Better handle a TextChanged or similar event. – Clemens Jul 18 '18 at 12:17
  • @Clemens I know there is no `KeyPress` on WPF. I answered you for the "three consecutive key codes". Here, I think he should use TextInput event. – Juan Carlos Rodriguez Jul 18 '18 at 12:20
  • @JuanCarlosRodriguez You commented on the question with "... and got the solution from SO". So which solution that works in WPF? Provide a link to what you found that works with WPF, otherwise your comment is pointless. – Clemens Jul 18 '18 at 12:22
  • @JuanCarlosRodriguez can you give more help about that? Because still I don't know the value of them in enumeration. – Burak Yeniçeri Jul 18 '18 at 12:29
  • @JuanCarlosRodriguez This is literally the first result on google for what I searched so telling him he didn't google hard enough is very ironic. – AustinWBryan Aug 13 '20 at 13:39

1 Answers1

1

The System.Windows.Input.Key enum has no values for these characters.

You may however handle the PreviewTextInput event instead of KeyDown:

private void htmlcode_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    switch (e.Text)
    {
        case "{":
            // add "}"
            break;
        // etc
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268