473,806 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_buf fer_empty() )
{
ch = getch();
if ( ch == ESC_KEY ) break;
}
..
..
..
}

I don't know how to make: flush_keyboard_ buffer()
is_keyboard_buf fer_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(ES C_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 2504
Andreas vinther <an************ *@gmail.comwrit es:
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_Keit h) 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************ **********@b28g 2000cwb.googleg roups.com>,
santosh <sa*********@gm ail.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
2011
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 data in Office 2003. After I switch from Chinese keyboard input back to English keyboard input, it...
0
6737
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 my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles. however, everytime I use the readfile function I am getting "object reference not set to an instant...
2
8771
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 my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles. however, everytime I use the readfile function I am getting "object reference not set to an instant...
11
11191
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 a text box. I know that there are certain control characters embedded in the data that I want to display substitutions for in the text box, for instance I want to replace ASCII character 04 with the string "<EOT>". I have tried doing a simple...
7
2485
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 the main form. My first attempt was with RichTextBox. I wanted to set the background to black. After cruising newsgroups I determined that wasn't possible. My second attempt involves an AxSHDocVw.AxWebBrowser control. I got this by adding...
0
1451
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 groupbox2 receives focus (if i change one of the checkboxes or click on the button), even before i can...
2
1440
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
2146
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 what I want to do and hopefully someone here can tell me if it's possible or if I will be wasting my time. The steps I need to execute are: 1) Import a file using the file menu of the target application (File->Import)
0
1089
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 file from java, the problem occured... my a86 simple program that waits and accept a character input from the keyboard and print it directly then next keypress terminates... ////////////////////////////////////////////ASM...
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9598
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10623
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10373
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9192
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.