Read.Key reads a key when none is pressed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzz13
    New Member
    • Jun 2010
    • 110

    #1

    Read.Key reads a key when none is pressed

    I've got the following code trying to learn how to use the read.key method:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
        ConsoleKeyInfo keypress;
    
        Console.WriteLine("Enter keystrokes. Enter Q to stop.");
    
        do {
          keypress = Console.ReadKey(); // read keystrokes
    
          Console.WriteLine(" Your key is: " + keypress.KeyChar);
    
          // Check for modifier keys.
          if((ConsoleModifiers.Alt & keypress.Modifiers) != 0)
            Console.WriteLine("Alt key pressed.");
          if((ConsoleModifiers.Control & keypress.Modifiers) != 0)
            Console.WriteLine("Control key pressed.");
          if((ConsoleModifiers.Shift & keypress.Modifiers) != 0)
            Console.WriteLine("Shift key pressed.");
    
        } while(keypress.KeyChar != 'Q');
      }
    }
            }
    The problem is, is that once I run it, it just sits and itterates "your key is ". I don't press a key, it just keeps going and going. I figure, maybe I have a key pressed and don't realize it, but if I open a text editor and leave it alone no keys type out so there is something wrong with my code I would presume. Can anyone help me figure out why it's reading a key over and over and not waiting for me to actually press a key?
  • MartijnHoekstra
    New Member
    • Sep 2010
    • 39

    #2
    What happens if you disconnect your keyboard?

    Comment

    • Christian Binder
      Recognized Expert New Member
      • Jan 2008
      • 218

      #3
      You could cast keypress.KeyCha r to an int to see the code of the pressed key, maybe this helps figuring out, which key is apparently pressed.

      Comment

      • MartijnHoekstra
        New Member
        • Sep 2010
        • 39

        #4
        Or better to use keypress.Key to see the textual representation of that key.

        Comment

        • Fuzz13
          New Member
          • Jun 2010
          • 110

          #5
          WEIRD!!! I turned it to keypress.key and it started saying : your key is 255
          your key is 255
          your key is 255

          And it just sits and says this over and over. If I press something fast like 'f' it will say:
          your key is f
          your key is 255
          your key is j
          your key is 255

          it will responde to my input but then go right back to 255.

          Why is it saying I'm inputing 255!?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I am not having that issue. For some reason your console application is non blocking and returning a -1. Well it says 255, but I suspect it is a -1

            Comment

            • Fuzz13
              New Member
              • Jun 2010
              • 110

              #7
              I don't understand what you mean that you suspect it's a -1 if it reads 255? I can only assume it is my console because the code looks clean, but what would make my console do that? I have no idea where to check whether I need to reinstall VS2010 or a new install of windows?

              Comment

              • Fuzz13
                New Member
                • Jun 2010
                • 110

                #8
                Chatting with some people I was informed that 255 is the ascii code for a space. Is it possible my code is reading a space and spitting it out in ascii? (I know nothing about ascii but it sounded better then not knowing why it's doing that).

                Comment

                • Fuzz13
                  New Member
                  • Jun 2010
                  • 110

                  #9
                  I transfered the program to a different computer and the code runs fine, so it must be a problem specific to my machine. Does anyone have any ideas what would cause this problem?

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    It could be a locale (language and typeset and etc) issue?
                    Try playing with the language settings on the computer with the issue.
                    255 on a signed byte would be -1
                    0xff (or 255) is not the ASCII code for a space,
                    The ASCII space is 0x20 (or 32).
                    It is however a "blank" http://www.jimprice.com/ascii-128-255.gif

                    Comment

                    • MartijnHoekstra
                      New Member
                      • Sep 2010
                      • 39

                      #11
                      255 or (0xFF) is or was commonly used for stating specific character being used.
                      The reading of keybuffer then held 2 characters.. the first being 0xFF and second one for a special key. Like UP or DOWN or Function Keys..

                      Ok.. that does not help you..

                      The input is certainly coming from somewhere. Either faulty hardware or fuzzy software.
                      While running your app, have you tried:
                      - disconnecting Keyboard?
                      - stopping unneeded processes for this test?
                      This just to see if the 255 spamming stops.

                      Comment

                      • Fuzz13
                        New Member
                        • Jun 2010
                        • 110

                        #12
                        The error occurs when ran on my laptop so I don't know if I can disconnect the keyboard very easily. I will try ending running services and see if that might help it.

                        The code has a space right before the spot for the read key, could that be causing the "blank" 255 input?

                        I'll try changing it from "please input key //key goes here" to "please input key\t//key goes here".

                        All good suggestions thanks for everyones help.

                        Comment

                        • Fuzz13
                          New Member
                          • Jun 2010
                          • 110

                          #13
                          I made those alterations and none of them fixed it. Other suggestions?

                          Comment

                          • MartijnHoekstra
                            New Member
                            • Sep 2010
                            • 39

                            #14
                            I ran out of suggestions, except for adapting your code:

                            Code:
                            do
                            {
                              do
                              { 
                                keypress = Console.ReadKey()); // read keystrokes
                              } 
                              while (keypress.Key==255);
                              ...

                            Comment

                            • Christian Binder
                              Recognized Expert New Member
                              • Jan 2008
                              • 218

                              #15
                              There's a property Console.KeyAvai lable which indicates if a key was pressed (is a non-blocking property).
                              Does it always return true?

                              Comment

                              Working...