473,505 Members | 16,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keyboard input

Please help. Trying to just get simple keyboard input from a C program.
Ideally would like to be able to just press a key without having to press
return, but at this point I don't care anymore. I can get one character
using getchar(), but I have to press return. When I do, it completely skips
over my second call to getchar(). Why? How do I make successive calls that
read the keyboard?

c = getchar();

if (c == 'y') {
bwok = 1;
printf("OK. You pressed 'y'\n"
}

c = getchar();
printf("char :%c\n",c);
Thanks
B
Oct 1 '08 #1
5 7914
bint wrote:
Please help. Trying to just get simple keyboard input from a C
program. Ideally would like to be able to just press a key without
having to press return, but at this point I don't care anymore. I
can get one character using getchar(), but I have to press return.
When I do, it completely skips over my second call to getchar().
Why? How do I make successive calls that read the keyboard?

c = getchar();

if (c == 'y') {
bwok = 1;
printf("OK. You pressed 'y'\n"
The line above is broken. In the future, post your actual code. Don't
try to retype it.
}

c = getchar();
printf("char :%c\n",c);
That's because the carriage return also places a character in the
stream. The second getchar() gets that.

Add another one right after the printf("Ok. You pressed 'y'\n");


Brian
Oct 1 '08 #2
Default User wrote, On 01/10/08 23:01:
bint wrote:
>Please help. Trying to just get simple keyboard input from a C
program. Ideally would like to be able to just press a key without
having to press return,
To do that you would need to use implementation specific extensions. If
you ask in a group dedicated to your specific system they will probably
be able to tell you how to do it.
but at this point I don't care anymore. I
>can get one character using getchar(), but I have to press return.
When I do, it completely skips over my second call to getchar().
Why? How do I make successive calls that read the keyboard?

c = getchar();

if (c == 'y') {
bwok = 1;
printf("OK. You pressed 'y'\n"

The line above is broken. In the future, post your actual code. Don't
try to retype it.
> }

c = getchar();
printf("char :%c\n",c);

That's because the carriage return also places a character in the
stream. The second getchar() gets that.

Add another one right after the printf("Ok. You pressed 'y'\n");
That would solve the immediate problem, however if a user types "yes"
instead of just "y" it won't. One solution to this is a simple loop
reading until getchar() returns either EOF (end of file or an error) or
'\n'.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 1 '08 #3
On Oct 2, 7:44*am, "bint" <b...@igs.comwrote:
Please help. *Trying to just get simple keyboard input
from a C program. Ideally would like to be able to just
press a key without having to press return,
Please read the comp.lang.c FAQ. This can't be done (or
at least not guaranteed) in standard C. Note that it's
usually the console that buffers input, not the C
implementation.
but at this point I don't care anymore. *I can get one
character using getchar(), but I have to press return.
*When I do, it completely skips over my second call to
getchar().
No, it doesn't.
>*Why? *How do I make successive calls that
read the keyboard?
The way you wrote it. [Though you should add some checks
for EOF.] The 'return' you enter is a '\n' in the input
stream.
* *c = getchar();

* *if (c == 'y') {
* * bwok = 1;
* * printf("OK. You pressed 'y'\n"
* *}

* * c = getchar();
* * printf("char :%c\n",c);
--
Peter
Oct 1 '08 #4
On 1 Oct, 23:07, Flash Gordon <s...@spam.causeway.comwrote:
Default User wrote, On 01/10/08 23:01:
bint wrote:
Please help. *Trying to just get simple keyboard input from a C
program. Ideally would like to be able to just press a key without
having to press return,

To do that you would need to use implementation specific extensions. If
you ask in a group dedicated to your specific system they will probably
be able to tell you how to do it.
You are looking for info about terminal modes. Here's a place to
start:
http://en.wikipedia.org/wiki/Termina...input.2Foutput

Oct 2 '08 #5
On 1 Ott, 23:44, "bint" <b...@igs.comwrote:
Please help. *Trying to just get simple keyboard input from a C program..
Ideally would like to be able to just press a key without having to press
return, but at this point I don't care anymore. *I can get one character
using getchar(), but I have to press return. *When I do, it completely skips
over my second call to getchar(). *Why? *How do I make successive calls that
read the keyboard?

* *c = getchar();

* *if (c == 'y') {
* * bwok = 1;
* * printf("OK. You pressed 'y'\n"
* *}

* * c = getchar();
* * printf("char :%c\n",c);

Thanks
B
Use this to get the character and eat the CR away:

scanf("%c%*c", &c);

Ciao
Oct 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1576
by: Claudio Grondi | last post by:
German Windows 2000, SP 4 Python 2.3.4 (#53, May 25 2004, 21:17:02) IPython 0.6.10 -- An enhanced Interactive Python. Is it already known, that after switching the keyboard input scheme on...
0
1991
by: Ray | last post by:
I have English Windows XP Pro and Office 2003 Pro on my computer. When I enter data into fields of tables, queries and forms of Access 2003, it automatically switches to Chinese keyboard input. ...
4
32923
by: Ralf Toender | last post by:
Hi! Does anybody know how to send a keystroke C#? In C++ it's done by: keybd_event ( ... ) or SendInput ( ... ). What namespace does include this counterpart? Thanks Ralf
3
2205
by: FabFreddy | last post by:
Hello, I'm writing a ASP.NET application. A applicatie that first was written in Basic and now i'm writting it in VB.NET, but I have one huge problem. I don't know how to get keyboard input. The...
0
1764
by: Freddy | last post by:
In my Access db (which I have created to help me learn Greek) I am using text box controls on a form. Some of these controls require English input and some require Greek input. Currently I have...
2
2060
by: pelletier.thomas | last post by:
Hello everybody. I'm trying to code a very little OS. But I have a problem with the keyboard input: It show thechar + "~" :s Can you help me ? The source is there :...
0
1442
by: Srin | last post by:
Hi, I'm in the process of writing a small C# App, in which i have two groupboxes in a form. The form has the KeyPreview property set to true. The groupbox1 contains a set of labels. The second...
8
4817
by: RJ45 | last post by:
Hello, I am writing a shell in C. I need to intercept Signals like CTRL+C or CTRL+D and set to ignore them. This is on Unix, using gcc. my goal is to avoid users escaping the shell with SIGINT...
3
7309
by: bwgames | last post by:
Hi there, I have a script that takes two inputs from the keyboard to change some settings on some equipment. I would usually run this as e.g. ben@srv001:/$ change-settings<enter> new-setting...
3
8392
by: =?Utf-8?B?cHJvZ2dlcg==?= | last post by:
I have a C# application that hosts an AxWebBrowser control which I automate by sending mouse clicks and keyboard input. I have had various problems in doing this due to a bug in the AxWebBrowser...
0
7218
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
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5035
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.