Thread: Executing Code for as long as

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    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

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The OP never mentioned portable code was one of his requirements. What OS are you using?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    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

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread