press any key

This is a discussion on press any key within the A Brief History of Cprogramming.com forums, part of the Community Boards category; does anyone know how to implement "to continue press any key" in C on dos I just need to know ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question press any key

    does anyone know how to implement "to continue press any key" in C on dos
    I just need to know how to know when any key is being pressed on the keyboard.
    Also i would like to know how to tell which key is being pressed if anyone knows.
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Try the getch(); for just one key press or getchar(); for more then 1 key press.

    if you dont want the program to stop and wait for the key press use the kbhit(); and then later use getch(); or getchar(); to read what key(s) they pressed.
    ()()() MonKey ware
    Kyle J. R.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    thanks for the reply but with getch() and getchar() the user has to press return before the program moves on. i want the program to move on immediately after any key is pressed on the keyboard
    any hints

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    You could make a loop that loops until the user presses a key using the kbhit() for breaking

    Code:
    #include <conio.h>
    
     while(kbhit() == 0)
     {
        DoCoolThings();
     }

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    that worked well but now the character of the key the user presses shows up at the next command prompt. how do i get rid of this character so that it doesnt show up anywhere

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    you could use clrscr(); after using the kbhit() loop

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    kbhit and getch

    Hi,
    Try this.

    #include <conio.h>
    #include <stdio.h>
    int main()
    {
    char a;
    while(!kbhit())
    {
    //do whatever
    }
    getch();// captures the char from kbhit

    return 0;
    }
    Pappy
    You learn something new everyday.

  8. #8
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    simple....

    use
    #include <iostream.h>
    #include <stdlib.h> /* needed for system() */

    int main()
    {
    system("pause");
    return 0;
    }

    Output:

    Preass any key to continue...

    Bam!
    +++
    ++
    + Sekti
    ++
    +++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09: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

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