473,320 Members | 1,600 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,320 software developers and data experts.

Need more control over keyboard input

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
Nov 21 '06 #1
3 2482
Andreas vinther <an*************@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 21 '06 #2
Andreas vinther wrote:
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.

Nov 21 '06 #3
In article <11**********************@b28g2000cwb.googlegroups .com>,
santosh <sa*********@gmail.comwrote:
....
>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".

Nov 22 '06 #4

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

Similar topics

0
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. ...
0
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
2
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
11
by: Ron L | last post by:
I have a barcode scanner which uses a "keyboard wedge" program so that the data it scans comes through as if it was typed on a keyboard. I am trying to have the data in the barcode be displayed in...
7
by: ChrisUttenreither | last post by:
Hello folks. I'm writing a Roguelike game in C#. Fun stuff. The game includes a map that is made entirely of colored ASCII art in a monospace font. I would like to make this map a control in...
0
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...
2
by: Frank | last post by:
In a dialog box procedure is there a way to determine if keyboard input is from the keypad or the arrow keys? I need to know if the input is from the main keyboard keys. Thanks
1
by: sklett | last post by:
I've never tried to use windows messages to automate an application, but I've read some different blogs and articles that talk about sending keyboard input and mouse input. I wanted to describe...
0
by: sukatoa | last post by:
I currently developing a simple notepad that could compile A86 codes using java.... Im using Windows XP SP2.... When @ compile, i am satisfied with the result... When i try to invoke the .COM...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.