Detecting a KeyPress

This is a discussion on Detecting a KeyPress within the C++ Programming forums, part of the General Programming Boards category; I am making a timer program. I know this is probably easy, but its been a long time since I ...

  1. #1
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176

    Detecting a KeyPress

    I am making a timer program. I know this is probably easy, but its been a long time since I did any C++.
    The question: Is there any way to make a while loop that runs until a key is pressed, and if so, how?

    The program:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    
    int h, m, s=0;
    
    using namespace std;
    
    void DoStuff();
    
    int main(){
    int h, m, s=0;
    cout<<"Press any Key to start and end the timer.";
    
    while(/*Put KeyPress Code here*/){
    cout<<h<<m<<s;
    Sleep(100);
    DoStuff();
    }
    }
    
    DoStuff(){
    s++;
    if(s=60){
    m++;
    if(m=60){
    h++;
    if(h=2){
    MessageBox(NULL, "You're waiting too long!", "Forgeting something?",MB_OK);
    return 0;}
    }
    }
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    kbhit() checks the buffer to see if a key has been pressed
    if u wanna wait on a keypress, use getch()

  3. #3
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    *Whacks self in face*
    Sorry for an easy question. As I said before, its been a long time.
    Also, my code's not working. It compiles, but goes bonkers!
    Revised Code:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    
    int h, m, s=0;
    
    using namespace std;
    
    void DoStuff();
    
    int main(){
    cout<<"Press any Key to start and end the timer.";
    
    while(!kbhit()){
    cout<<h<<m<<s;
    Sleep(100);
    DoStuff();
    }
    }
    
    void DoStuff(){
    s++;
    if(s=60){
    m++;
    if(m=60){
    h++;
    if(h=2){
    MessageBox(NULL, "You're waiting too long!", "Forgeting something?",MB_OK);
    }
    }
    }
    }
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  4. #4
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    what do you mean it goes bonkers?

    h and m are also uninitialized, that could be one of your problems.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    I made it work and did some improvements... I am sorry but I got taken away! if there is something you dont understand, feel free to ask.
    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int s = 0, m = 0, h = 0;
    
    void DoStuff();
    
    int main()
    {
    	bool PressedButton = false;
    
    	cout << "Press any Key to start and end the timer.";
    
    	getch();
    
    	while (!PressedButton)
    	{
    		cout << endl;
    		cout << h << "h " << m << "m " << s << "s";
    		Sleep(1000);
    		DoStuff();
    
    		PressedButton = (kbhit());
    
    		cout << endl << endl;
    
    		if (PressedButton)
    			getch();
    
    		else
    			system("cls");
    	}
    }
    
    void DoStuff()
    {
    	s++;
    	if (s == 60)
    	{
    		m++;
    		s = 0;
    	}
    
    	if (m == 60)
    	{
    		h++;
    		m = 0;
    	}
    
    	if (h == 2 && m == 0 && s == 0)
    	{
    		MessageBox(NULL, "You're waiting too long!", "Forgeting something?",MB_OK);
    	}
    }
    Why drink and drive when you can smoke and fly?

  6. #6
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Your code compiles fine but just displays 0h 0m 0s then bombs (Borland Free Command Line Tools). I got this to work:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    
    int h=0, m=0, s=0;
    
    using namespace std;
    
    void DoStuff();
    
    int main(){
      cout<<"Press any key to stop the timer.";
      Sleep(500);
      while(true){
        while(!kbhit()){
          DoStuff();
        } 
    cout<<"\n"; 
        system("pause");
      }
      return 0;
    }
    
    void DoStuff(){
    Sleep(1000);
    clrscr();
    s++;
    if(s==60){
    m++;
    s=0;
    if(m==60){
    h++;
    m=0;
    }
    }
    cout<<h<<":"<<m<<":"<<s;
    }
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  7. #7
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I use windows API in a DLL to implement a global CallBack when keys are pressed. It works well. Look up SetWindowsHookEx.
    Last edited by Xei; 06-15-2003 at 02:27 PM.
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You could also try
    while(cin.rdbuf()->in_avail() == 0)
    which is true while the input buffer is empty.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Xei,
    I'm not on DLLs yet.
    Rest.
    I'm finished with the programs, so no more suggestions.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  10. #10
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    Originally posted by Xei
    I use windows API in a DLL to implement a global CallBack when keys are pressed. It works well. Look up SetWindowsHookEx.
    you can do that on a console?

  11. #11
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Originally posted by the Wookie
    you can do that on a console?
    If it's in a DLL, yes. Otherwise you will only get locally pressed keys, meaning that your application must be in the foreground in order to receive keypresses. And you obviously have to be running windows, it wont run under DOS.
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    No exe compiled for windows runs in DOS, not even a console app.

    Actually there are only two differences between normal windows apps and console apps:
    1) console starts at main
    2) console automatically gets a console window (which a windows app would have to create with AllocConsole) and has stdin, stdout and stderr set to that console.

    Therefore there's no reason why a console should not set hooks or create windows.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Guys,
    I'm still doing console apps, so stay off windows. And where could I learn about DLLs?
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (MFC, Visual C++) Keypress Detection in a dialog...
    By guitarist809 in forum Windows Programming
    Replies: 4
    Last Post: 08-31-2008, 01:13 PM
  2. Detecting empty file situation.
    By xIcyx in forum C Programming
    Replies: 9
    Last Post: 06-18-2008, 10:37 PM
  3. Detecting keypress
    By dandago in forum C Programming
    Replies: 4
    Last Post: 06-10-2007, 09:34 AM
  4. How do I NOT wait for a keypress?
    By misplaced in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2005, 04:10 PM
  5. detecting keypress
    By lithium in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2003, 12:10 AM

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