473,569 Members | 2,691 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 7921
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.cause way.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.comwr ote:
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.caus eway.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.comwr ote:
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
1589
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 German Windows 2000 to english USA International IPython generates \x00" instead of " when trying to input quotation marks?
0
1996
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. Even though I remove the Chinese keyboard input from Regional applet, the system will automatically reinstall Chinese keyboard input when I enter...
4
32931
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
2218
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 basic application was keyboard driven and now it has to be both mouse and keyboard! Any help how to do this in ASP.NET? Thx Fred
0
1767
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 to keep toggling the keyboard input language bar settings manually from the keyboard whenever I wish to switch languages - this is a real pain. ...
2
2066
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 : http://thonix.tuxfamily.org/pub/0.0.1/src/
0
1447
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 groupbox contains a few checkboxes and a button. I wrote a form1_keypress handler to handle keyboard inputs directed at the form. But once the...
8
4822
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 SIGQUIT signals using keyboard input. Anyone could give me a little hint ? thanks
3
7312
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 1 <enter> new-setting2 <enter> Settings successfully changed ben@srv001:/$
3
8401
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 receiving input when it is hosting a flash page however I have got it to work using SendKeys for the keyboard input and the Windows API SendInput...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7970
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.