Need more control over keyboard input

A

Andreas vinther

Hi all you hardcore coders and everyone else.

I'm a beginner programmer, so forgive me if this is a stupid question.
I'd like to be able to check if the user has pressed a key since I
flushed the keyboard buffer. But the only thing I can find is a function
kbhit(), which I believe is a Borland only component and not really a
part of ANSI C. kbhit() is not supported in any of my standard #include
files.

getch() stops execution and waits for user to press a key and I don't
want to do that.

I need to make something like this:

flush_keyboard_buffer();

while (FOREVER)
{
..
..
..
if ( is_keyboard_buffer_empty() )
{
ch = getch();
if ( ch == ESC_KEY ) break;
}
..
..
..
}

I don't know how to make: flush_keyboard_buffer()
is_keyboard_buffer_empty()

I don't know if I can use getch() like i do in the code above.


An even better and more versatile solution would be, if i'm able to get
a key's current state. This would truely make me a happy camper.
The scenario is pretty much described in the code snippet below.

I will be forever grateful if someone can help me make the function:

int key_pressed(int keycode)

The function should return 1 if the key described by keycode is
currently pressed, and 0 if the key is not pressed. It would be extra
nice if i'm able to catch the state of SHIFT, ALT, CTRL, etc.

It'll be used in code similar to the code snippet below.

while (FOREVER)
{
..
..
if (key_pressed(ESC_KEY)) break;
..
..
}

If you have an even better solution, please don't hesitate to write. I
dying to get this code implemented.

Thank you very much in advance

/A\ndreas
 
K

Keith Thompson

Andreas vinther said:
I'm a beginner programmer, so forgive me if this is a stupid
question. I'd like to be able to check if the user has pressed a key
since I flushed the keyboard buffer. But the only thing I can find is
a function kbhit(), which I believe is a Borland only component and
not really a part of ANSI C. kbhit() is not supported in any of my
standard #include files.
[...]

The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've just asked
question 19.2.
 
S

santosh

Andreas said:
Hi all you hardcore coders and everyone else.

I'm a beginner programmer, so forgive me if this is a stupid question.
I'd like to be able to check if the user has pressed a key since I
flushed the keyboard buffer. But the only thing I can find is a function
kbhit(), which I believe is a Borland only component and not really a
part of ANSI C. kbhit() is not supported in any of my standard #include
files.

None of this is possible using just standard C. As you've noted you
must call various implementation or platform specific subroutines. And
you've not mentioned your platform.

I suggest abandoning routines like khbit(), getch() etc., and using the
specific keyboard or input API exported by your OS. For example, under
Windows, you can use functions like GetConsoleInput(). They give you
the fine-grained control you seem to want. Under Linux and many other
UNIXes, you might try the curses, (or ncurses), library.

Please post further queries that do not have anything to do with
standard C, to more compiler/OS/library/program specific groups, where
you'll get better responses and less redirections.
 
K

Kenny McCormack

santosh said:
Please post further queries that do not have anything to do with
standard C, to more compiler/OS/library/program specific groups, where
you'll get better responses and less redirections.
^^^^^^^^^^^^
I think you misspelled "bullsh*t".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top