Key Input

This is a discussion on Key Input within the A Brief History of Cprogramming.com forums, part of the Community Boards category; How can you catch a key input? I mean you've got a menu, 1. New Game, 2. Exit Game and ...

  1. #1
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542

    Question Key Input

    How can you catch a key input? I mean you've got a menu, 1. New Game, 2. Exit Game and you get to choose 1 or 2. I only know how to make it with the cin but I kinda want it on KeyPress, not type a number and press enter, just press the key and an action happens.
    Thanks
    what does signature stand for?

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    What compiler are you using? check to see if it supports getch()

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Dev-C++, cant check cuz it's got no help files...
    what does signature stand for?

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Try including getch() in your code and make. If your linker gives no error, means this function is supported, else no.
    Also, search the web for documentation on your compiler.

  5. #5
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    I'm a beginner, so what do you mean including? How?
    Thanks
    what does signature stand for?

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    What version of DEV-C++?

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    Ruski,
    For doing that, you just set your variable equal to getch(); Such as...
    Code:
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    int Keypressed;
    cout<< "Main Menu\n\n";
    cout<< "1. New Game\n";
    cout<< "2. Exit\n";
    Keypressed = getch();
    
    if (Keypressed == 1)
    // etc.
    } // end of main
    Or, I would make the variable you are inputting a string. I know that it is irritating having a little pause, but try entering a letter when asking for an integer. Not pretty, the screen fills up with repeats of your question.
    Hope that helps, post again for clarification or more examples.
    "Um...well..."
    -Kyoto Oshiro

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    1

    Thumbs up Try this!!!

    Hello Friend!!
    Whenever u accept a key using getch(), the prototype u need to define is char,insted of integer,though u r accepting int. Then try converting that into integer,which is so simple. the example code is given here.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int key;
    printf("\n1.New Game");
    printf("\n2.Exit Game");
    key=getch();
    if ((int)key==49)
    printf("New Game activated");
    if ((int)key==50)
    printf("Exit Game activated");
    }

    here (int)key is typecasts the charcter to int. The ASCII equivalent to 1 is 49 and that of 2 is 50. U can even check by putting them in a switch case.

    Hope u understood the concept. If any querries, mail me at sriharsha_kvr@yahoo.com

  9. #9
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Thanks
    what does signature stand for?

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    27
    Right im also looking for something to do that..... i got that part working but how can i then get it to go to say the game?

    Im not making a game just an example.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >void main()
    This is incorrect, use should use "int main(void)".

    >int key;
    >if ((int)key==49)
    >here (int)key is typecasts the charcter to int.
    Not exactly, key is already an int because that's how is was declared. Therefore the cast isn't necessary.

    >if ((int)key==49)
    Rather than coding the ASCII value of the character, you could use the character constant, like so:
    >if (key=='1')
    This makes things easier to read.

    >i got that part working but how can i then get it to go to say the game?
    Does this example help?:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void TheGame(void)
    {
        printf("The game has been activated\n");
    }
    
    int main(void)
    {
        int key, StillGoing = 1;
    
        while (StillGoing)
        {
            printf("--- Menu ---\n1.New Game\n2.Exit Game\n>");
            switch (key = getch())
            {
            case '1':   TheGame(); break;
            case '2':   StillGoing = 0; break;
            default:    printf("Option Invalid: %c\n", key); break;
            }
        }
    
        printf ("Exiting\n");
        return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    void main()
    LOL, nothing annoys a C programmer more than that...well us assembly programmers maybe, but thats a whole other kettle of fish.
    VC++ 6

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    that's because C programmers are under the dilusion that they are the super, low-level geniuses. when an assembler freak comes along they are blown away by reality. C is really like straddling the fence. Assembler or C++ only in my book! C is an outdated version of C++!

    Sorry, this is my first time trolling.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >that's because C programmers are under the dilusion that they are the super, low-level geniuses.
    Yes.. and?

    >when an assembler freak comes along they are blown away by reality.
    Not really, it's two different worlds, I don't think a direct comparison works. But I do appreciate the assembler lanuage, and the people that know a lot about it.

    >C is really like straddling the fence.
    What fence?!

    >C is an outdated version of C++!
    Oh no, not the C vs C++ argument!

    >Sorry, this is my first time trolling.
    Well, I'm not going to provoke you by replying to this thread. Oh dammit, I just did
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Thanks for the help
    what does signature stand for?

Page 1 of 2 12 LastLast
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, 06:45 AM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  3. key input definition
    By hkmixxa in forum C++ Programming
    Replies: 3
    Last Post: 08-09-2004, 06:39 PM
  4. Key input?
    By Kavity in forum Linux Programming
    Replies: 0
    Last Post: 12-20-2002, 11:01 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 11:45 PM

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