Thread: Key combo

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Key combo

    Code:
    int main(int argc, char *argv[]) {
    int test;
    test = 0;
    while (1) {
    	test++;
    }
    test--;
    printf("%d",test);
    return 0;
    This small test program runs until the end of time (console linux ). Now my question is.

    How do you make the while lus end by pressing a key or key combo?

    Along the lines of: while("A" is not pressed) ....

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can use kbhit(). This will loop until a key is pressed:
    Code:
    while(!kbhit()) {
        puts("You haven't pressed a key yet . . .");
    }
    If you want to exit the loop only when a key is pressed, try this:
    Code:
    for(;;) {
        if(kbhit()) {
            if(getch() == 'A') break;
        }
    
        test++;
    }
    If you don't mind waiting, use scanf() or getchar().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM