Detecting if ASCII Control Characters are pressed (C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Recursive
    New Member
    • Oct 2009
    • 5

    Detecting if ASCII Control Characters are pressed (C#)

    I am seeking a method to detect if ASCII Control Characters are pressed. My code currently shown here is not working too well i suspect.

    Code:
     
    private void Transmitted_KeyDown(object sender, KeyEventArgs e)
    {
              if (e.Modifiers==Keys.Control)
               {
                   test = (char)e.KeyCode;              
               }
    }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    "ASCII control characters" such as an ASCII 05 "Enquiry", or an ASCII 23 "End of transmission block" ??

    How would someone type one of these?

    Comment

    • Recursive
      New Member
      • Oct 2009
      • 5

      #3
      ASCII 05 is Ctrl+E and ASCII 23 is Ctrl+W as seen from http://nemesis.lonestar.org/referenc...des/ascii.html.

      I would like to find a more elegant way to do this rather than manually defining each block like this :

      Code:
      if (e.KeyData == (Keys.Control | Keys.A))
                  {
                      test = (char)01;
                      
      
                  }
      
                  if (e.KeyData == (Keys.Control | Keys.B))
                  {
                      test = (char)02;
                      
      
                  }
      
      
                  if (e.KeyData == (Keys.Control | Keys.C))
                  {
                      test = (char)03;
                      
      
                  }

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        How do you feel about this logic? (suedo code, you can flush out)

        On keypress

        if control key pressed
        {
        switch e.KeyData
        {
        Case E
        ...
        Case W
        ...
        Case R
        ...
        }
        else
        {
        // No control key so we don't care
        }
        }

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Out of curiosity, do you need this globally (from outside the application), or just inside the particular control?

          Comment

          • Recursive
            New Member
            • Oct 2009
            • 5

            #6
            Not exactly what I was looking for. I would prefer grabbing the KeyCode of the keypress and then putting it in a char.

            Comment

            • Recursive
              New Member
              • Oct 2009
              • 5

              #7
              Originally posted by GaryTexmo
              Out of curiosity, do you need this globally (from outside the application), or just inside the particular control?
              Just inside the particular control (textbox).

              Comment

              • GaryTexmo
                Recognized Expert Top Contributor
                • Jul 2009
                • 1501

                #8
                I tried a google search and came up with this...


                You don't need to worry about the ASync method in there. The particular lines of code that should interest you are...

                Code:
                        lbl.Text = "Key Down: " + e.KeyValue.ToString();
                        lbl.Text += "\nKey Code: " + e.KeyCode.ToString();
                        lbl.Text += "\nKey Data: " + e.KeyData.ToString();
                ... which would be in your KeyDown event.

                Does that help any?

                Comment

                • Recursive
                  New Member
                  • Oct 2009
                  • 5

                  #9
                  Originally posted by GaryTexmo
                  I tried a google search and came up with this...


                  You don't need to worry about the ASync method in there. The particular lines of code that should interest you are...

                  Code:
                          lbl.Text = "Key Down: " + e.KeyValue.ToString();
                          lbl.Text += "\nKey Code: " + e.KeyCode.ToString();
                          lbl.Text += "\nKey Data: " + e.KeyData.ToString();
                  ... which would be in your KeyDown event.

                  Does that help any?
                  Not exactly, I already know about key modifiers, codes , values and such. The challenge is simplifying my existing code and enhancing it to include all keyboard modifiers.

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #10
                    Like what? I tried that little demo program on the site and it was trapping ctrl-e, which is one of the things you wanted.

                    Oooh, you mean you want the actual ascii value of it? I see... well that changes everything :D Sorry for the confusion.

                    I'm at a loss here. I tried a few console programs in C# and QBasic and I was able to get the functionality you want by reading the input stream, but I can't figure out how to do it on a form. I even tried some code I had that would read a global input stream that used all sorts of dllimports thinking maybe there was something in there, but it returns the same as the event arguments.

                    At this point, I think I agree with Thlintoq's method. While it's not exactly what you wanted, it will give you what you're looking for.

                    Comment

                    • guimel
                      New Member
                      • Oct 2009
                      • 7

                      #11
                      It looks like the KeyValue of the event is the Ascii value of the key (in uppercase), thus the following works:
                      Code:
                      if (e.Modifiers == Keys.Control && e.KeyValue >='A' && e.KeyValue <='Z')
                          int test = e.KeyValue - 'A' + 1;
                      Be aware that this relies on undefined behaviour as it's never specified what the KeyValue of a specific key is.

                      Edit:
                      To make it a bit safer, you could have:
                      Code:
                          int test = e.KeyValue - (int)Keys.A + 1;
                      That's guaranted to work as long as the values of Keys.A to Keys.Z are all consecutive and in the right order in the Keys enum.

                      Comment

                      Working...