Thread: Get Enter Key

  1. #1
    Unregistered
    Guest

    Get Enter Key

    I am writing a program that is supposed to go through a loop each time someone hits the Enter key, how is this done?

    for example:

    while( KeyPressed == Enter )
    {
    DoStuff;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    PHP Code:
    #include <conio.h>                            // for getch();
    #include <iostream>                            // for cout
    using namespace std;

    #define ENTER 13                            // Make it easier for you, so you can just put ENTER instead of 13

    int main()
    {
        
    int key;                                // Variable to store the key press

        
    key=getch();                            // Scan for keypresses

        
    if( key == ENTER )                        // Pretty straight forward, could use: if( key == 13 ) also
            
    cout << "Enter key pressed\n";        // Display Enter key pressed
        
    else                                    // If the enter key wasnt pressed
            
    cout << "Enter key not pressed\n";    // Display Enter key not pressed

        
    return 0;

    theres a program that checks if enter key was pressed

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    You could have two threads. One that handle the keyboard input, and one that acctually do stuff. These two threads must share the variable KayPressed.

    you could also do like this, and this is how most games would work like (exept those that works like the above

    Code:
    void mainloop()
    {
        while(bGameRunning)
        {
            GetInput(); // <- set the Kayboard/Mouse/Joystick structs
            UpdateObjects(); /* <- this function update all game
    objects, these object can use the Keyboard variable and do
    stuff depending on what key is pressed. EX "Walk North". */
            Render();
        }
    }
    This is an easy simplified example... Depending on your programming skills, you can expand this. I have for example an eventbased system where a keypress can trigger an event for the game object (player or UI or something like that), and a mouseclick can trigger an objects event if its pressed.

    So... how you do this is up to your skills... If you asked how to get the keyboard input in the first place, then I just say www.google.com everything is there...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Dialog Edit Control and Enter Key
    By Quantrizi in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2004, 07:59 AM
  4. Need to capture the enter key
    By tyouk in forum Windows Programming
    Replies: 7
    Last Post: 11-08-2003, 06:07 PM