473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Read.Key reads a key when none is pressed

110 100+
I've got the following code trying to learn how to use the read.key method:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.     ConsoleKeyInfo keypress;
  13.  
  14.     Console.WriteLine("Enter keystrokes. Enter Q to stop.");
  15.  
  16.     do {
  17.       keypress = Console.ReadKey(); // read keystrokes
  18.  
  19.       Console.WriteLine(" Your key is: " + keypress.KeyChar);
  20.  
  21.       // Check for modifier keys.
  22.       if((ConsoleModifiers.Alt & keypress.Modifiers) != 0)
  23.         Console.WriteLine("Alt key pressed.");
  24.       if((ConsoleModifiers.Control & keypress.Modifiers) != 0)
  25.         Console.WriteLine("Control key pressed.");
  26.       if((ConsoleModifiers.Shift & keypress.Modifiers) != 0)
  27.         Console.WriteLine("Shift key pressed.");
  28.  
  29.     } while(keypress.KeyChar != 'Q');
  30.   }
  31. }
  32.         }
  33.  
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?
Sep 5 '10 #1
15 3993
What happens if you disconnect your keyboard?
Sep 6 '10 #2
Christian Binder
218 Expert 100+
You could cast keypress.KeyChar to an int to see the code of the pressed key, maybe this helps figuring out, which key is apparently pressed.
Sep 6 '10 #3
Or better to use keypress.Key to see the textual representation of that key.
Sep 6 '10 #4
Fuzz13
110 100+
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!?
Sep 6 '10 #5
Plater
7,872 Expert 4TB
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
Sep 7 '10 #6
Fuzz13
110 100+
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?
Sep 7 '10 #7
Fuzz13
110 100+
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).
Sep 7 '10 #8
Fuzz13
110 100+
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?
Sep 8 '10 #9
Plater
7,872 Expert 4TB
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
Sep 9 '10 #10
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.
Sep 9 '10 #11
Fuzz13
110 100+
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.
Sep 9 '10 #12
Fuzz13
110 100+
I made those alterations and none of them fixed it. Other suggestions?
Sep 9 '10 #13
I ran out of suggestions, except for adapting your code:

Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3.   do
  4.   { 
  5.     keypress = Console.ReadKey()); // read keystrokes
  6.   } 
  7.   while (keypress.Key==255);
  8.   ...
Sep 10 '10 #14
Christian Binder
218 Expert 100+
There's a property Console.KeyAvailable which indicates if a key was pressed (is a non-blocking property).
Does it always return true?
Sep 10 '10 #15
Fuzz13
110 100+
@Christian Binder: I don't understand what your asking, I'm very new to writing in C#.
Sep 12 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Patrick Olurotimi Ige | last post by:
With the code below i get error:- Invalid attempt to read data when reader is closed. //Get a datareader SqlDataReader objDataReader; objDataReader =...
7
by: Brian Henry | last post by:
I have a list box bound to a data set, of course the datasource,datamember, and valuemember are set, i tell it selecteditem = -1 to make nothing selected, select mode is set to only one item at a...
3
by: Mel | last post by:
Hi, Is there any way I can force to change button background colour when toggle button is checked? Just like button change its background colour when mouseover in MS Visual Studio.NET. I hope I...
2
by: kohoutec | last post by:
Hi Im very much a beginner so apologies if my terminology is wrong. I have a combo box with some items already loaded, the users can also enter a new item. If they enter a new item and then...
2
by: ShawnD | last post by:
I'm having some issues when trying to read input off of a pipe using a python script. I'm trying to process packet data from tcpdump in real-time, so it's a filter that needs to read data while the...
1
by: Ed Sutton | last post by:
<Newbie> How can I get arguments, and build a query URL, before a button is pressed? I have a text box to enter a serial number. When the search button is pressed, I wish append the serial...
2
by: =?Utf-8?B?VkIgLk5FVCBSb29raWU=?= | last post by:
I am creating an application and now when I try to run my program the forms open as Read Only and I can't type any text into my text boxes. How do I change this back to normal???? Thanks in...
5
by: angelotti | last post by:
Hi everyone , i have the following problem , i read data from another application that i started through the runtime.exec() in my Java class. I need to periodically check if there is any data...
1
by: thewickedman | last post by:
Hi, I try to open new OpenOffice document (writer), But I am getting the exception FRM 40735:WHEN-BUTTON-PRESSED Trigger Raised unhandled Exception ORA - 305500 Please help me to resolve...
13
by: Ezzz | last post by:
I have created a report that displays a "Product Summery". The problem is that it displays all products even when there is none in stock. Is ther a way to get the report to display only the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.