mkae it so when the user presses...

This is a discussion on mkae it so when the user presses... within the C++ Programming forums, part of the General Programming Boards category; I want to make it so when the user presses the esc or anouther key the p[rogram will do somthing? ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    mkae it so when the user presses...

    I want to make it so when the user presses the esc or anouther key the p[rogram will do somthing? How do I do that?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Part of it depends on specifically what key you want pressed, but mostly it's what type of application you're doing. Is this a Windows app? If so, what API? You'll want to look into event handlers, me thinks.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    WinAPI is pretty simple.

    Create an array of boolean values of key presses.

    Code:
    ........
    
    bool bKey[255];
    
    ........
    
    // in your message switch do this
    
    LRESULT CALLBACK WinProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    {
    
        switch ( uMsg ) {
    
            case WM_KEYDOWN:
    
                bKey [wParam] = true;
    
            break;
    
            
            case WM_KEYUP:
    
                bKey [wParam] = false;
    
            break;
    
        }
    
    }
    
    ........
    
    if ( bKey [VK_ESC] ) PostQuitMessage ( 0 );
    but if your not using WinAPI then um.. what are you using.

    With conio..

    its probably like

    Code:
    if ( kbhit () == KEYS_CODE )
    
    // or
    
    var = getch ();
    if ( var == KEYS_CODE )
    Where KEYS_CODE is the ASCII value.
    What is C++?

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh well just a counsl program. Possibly the esc key, if not ctrl+q, or something that "means" quit.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Alright then - use conio like Vicious showed you. It's not standrd, but it's common.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah I know Rune is a big fan of it :P

    Oh, and im ont sure if thats how it would work.. but probable.

    Rune, I think you should look into SDL. Its not that hard really.
    What is C++?

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    firstly I droped getch(). So I'm not a big fan on conio.h anymore.

    And what is SDL?

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Simple DirectMedia Layer. It's written more for C programmers but it will work in C++ also. It gives you graphics, sound, support for peripherals, threading, event handling, etc... Lots of good stuff. Go to www.libsdl.org to learn more.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    kbhit() is used only to tell if a keystroke is waiting in the input buffer (according to my compiler's MSDN). It's used in combination with getch():
    Code:
    bool done = false;
    while(!done)
    {
       //do your game code or whatever
       if(kbhit())
       {
          char theKey = getch();
          if(theKey == 'X')
             done = true;
       }
    }
    You can play around with it to suit your needs (although, as has been mentioned, it's very non-standard).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    if(kbhit())
    {
    char theKey = getch();
    if(theKey == 'X')
    done = true;
    }
    Alright that bit of code I get a little more. It works fine too.

    Thanks

  11. #11
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah thats what I meant :P
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prevent user input
    By bonne_tim in forum C Programming
    Replies: 11
    Last Post: 01-01-2007, 02:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM
  4. execute until user presses a key?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 11:31 PM
  5. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 12:21 PM

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