C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-14-2010, 06:18 PM   #1
Registered User
 
Join Date: Feb 2010
Posts: 66
Smile Executing Code for as long as

Hi

I was wondering how I could make a program that executes a piece of code for as long as a key is being touched?

So for example:

Code:
while A is pressed down
cout<<"You are typing A"<<endl;
With the input of A, the output would be "you are typing A".

I can't use
Code:
std::string key;
while(1){
cin >> key;
if (key == "a"){
cout<<"You are typing A"<<endl;
}
since the "cin" would require I press Enter/Return.


When I type text on my computer, the keyboard will output the letter "a" as I type it.
I need something similar to that - which doesn't require Enter/Return.

Thanks in advance
CPlus is offline   Reply With Quote
Old 02-14-2010, 06:50 PM   #2
Registered User
 
NeonBlack's Avatar
 
Join Date: Nov 2007
Posts: 356
There is no portable way to do this, so it depends which OS you want to run it on. The easiest ways to do it on Windows and Linux are probably conio.h and curses, respectively.
__________________
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo
NeonBlack is offline   Reply With Quote
Old 02-14-2010, 07:18 PM   #3
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 8,415
The OP never mentioned portable code was one of his requirements. What OS are you using?
Bubba is offline   Reply With Quote
Old 02-14-2010, 07:45 PM   #4
Registered User
 
Join Date: Feb 2010
Posts: 66
Hello, thanks for your answers!

I am currently using Windows 6, Vista.
I checked my Include folder, and did see conio.h

How can I use it to do this?
Thanks
CPlus is offline   Reply With Quote
Old 02-14-2010, 08:27 PM   #5
Registered User
 
NeonBlack's Avatar
 
Join Date: Nov 2007
Posts: 356
With a loop using kbhit() and getch(). Look up the documentation for these 2 functions.

Also I should mention that there is a curses library for Windows called pdcurses, so if you ever might want to port to Linux or Apple, you might want to go that route.
__________________
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo
NeonBlack is offline   Reply With Quote
Old 02-14-2010, 09:51 PM   #6
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
Or you can trap the keydown event and continue doing what you are doing until you get a keyup event (and it is your 'A' char or whatever). Different way to skin the cat, so so speak.
__________________
C/C++ Environment: GNU CC/Emacs
Make system: CMake
Debuggers: Valgrind/GDB
jeffcobb is offline   Reply With Quote
Reply

Tags
as long as, automatic, execute, key, no enter

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My code wont link with gcc but does with Visual Studio moniker C Programming 2 12-19-2009 05:08 AM
could I got some help to edit this code please J U S T I N Linux Programming 8 08-26-2009 10:15 AM
Merge and Heap..which is really faster silicon C++ Programming 2 05-10-2005 04:06 PM
it's a very long code "sorry" enjoy C Programming 11 05-03-2004 08:14 AM
need help emperor C Programming 1 03-04-2002 12:26 PM


All times are GMT -6. The time now is 08:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22