Getche - Enter key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Giku Promitt
    New Member
    • Sep 2008
    • 6

    Getche - Enter key

    I know that the following instructions:

    char c;
    c=getche();

    ...read a character. If I type, for example, 'g', then c=='g'. But what is equally c with if I press an arrow, the Enter key, the Del key etc.?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Look in the ASCII table for the values of these keys.

    Or, assign c to an int and display the int.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by weaknessforcats
      Look in the ASCII table for the values of these keys.
      Or, assign c to an int and display the int.
      Better to investigate this yourself by printing out the value of c for various keypresses. That's because:
      1. ASCII encoding may be ubiquitous, but there is no law (or Standard) that requires your system to use ASCII.
      2. Some keys are expressed as escape sequences, where a single keypress generates a stream of characters. To detect this, you want to loop continuously on getche, printing out whatever you get as hexadecimal text.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by donbock
        1. ASCII encoding may be ubiquitous, but there is no law (or Standard) that requires your system to use ASCII.
        2. Some keys are expressed as escape sequences, where a single keypress generates a stream of characters. To detect this, you want to loop continuously on getche, printing out whatever you get as hexadecimal text.
        Yes, ASCII is not required by the standard but I have yet to see a version of C running on Unix/Linux or Windows that doesn't use it. Not to say there isn't a C lurking somewhere with a 5-bit char.

        What is an example of one keypress generating a stream of characters? And I don't mean autorepeat by leaning on the key. Autorepeat is just a fast typist.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by weaknessforcats
          Yes, ASCII is not required by the standard but I have yet to see a version of C running on Unix/Linux or Windows that doesn't use it. Not to say there isn't a C lurking somewhere with a 5-bit char.
          Mobile phones and particularly text messages would be 1 recent example of a platform that may well use a character coding other than ASCII (GSM 03.38)

          However I suspect that as mobile phones transform into computer like PDAs this will be less true.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by weaknessforcats
            What is an example of one keypress generating a stream of characters? And I don't mean autorepeat by leaning on the key. Autorepeat is just a fast typist.
            Any arrow key I think or any other extended keyboard keys that do not directly convert to an ASCII character.

            Comment

            • Giku Promitt
              New Member
              • Sep 2008
              • 6

              #7
              I wrote the following test program:

              #include<iostre am.h>
              #include<conio. h>
              void main()
              { char c;
              c=getch();
              cout<<endl<<c;
              getche(); }

              Here are some results:
              Left Arrow - K
              Right Arrow - M
              Up Arrow - H
              Down Arrow - P
              Delete Key - S
              At <Enter> and <Space> pushing there was no any letter in cout, maybe a space. Why was that?
              PS: I want to do a short instruction like:
              if (c==enter) {...}
              Thanks a lot! G.P.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Originally posted by weaknessforcats
                What is an example of one keypress generating a stream of characters? And I don't mean autorepeat by leaning on the key. Autorepeat is just a fast typist.
                A list of the standard ANSI escape sequences can be found at
                Last edited by Banfa; Sep 25 '08, 08:36 AM. Reason: Added [URL]...[/url] tags round the URL

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by donbock
                  A list of the standard ANSI escape sequences can be found at
                  http://en.wikipedia.or g/wiki/ANSI_escape_cod e
                  Thank you. I will have a look.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by Giku Promitt
                    At <Enter> and <Space> pushing there was no any letter in cout, maybe a space. Why was that?
                    Erm you pressed the <Space> key and you got a space ' ' would you really have wanted a letter instead?

                    Look carefully when you press <Enter> you get an enter (there is an extra blank line on screen).

                    Rather than outputting the character(s) you read to understand what is happening I suggest you output the value(s) (decimal or hexidecimal) of what you read from the keyboard.

                    Also see Dons link.

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      The multi-byte escape sequences can be sent to a terminal to provoke some special action. My recollection from the VT100 days was that the arrow keys on the terminal sent the corresponding cursor-move escape sequences to the computer. I don't know if today's PCs send multi-byte escape sequences or non-ASCII (>= 0x80) single characters.

                      Comment

                      Working...