Thread: single key input

  1. #1
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35

    Question single key input

    I'm trying to get single keypress input without having to press enter after each key.
    I can do it using conio.h but that isn't supported by the standard.
    Which means in DevC++ it won't let me compile if I have
    -Wall -Wstrict -prototypes -ansi -pedantic
    turned on.

    This is what I have
    Code:
      char key;
      key=getch();
      switch(key)
      {
       case 'w': mover.move(0,-1); break;
       case 's': mover.move(0,1);  break;
       case 'a': mover.move(-2,0); break;
       case 'd': mover.move(2,0);  break;
      }
    Anyone got a simple way to do it that will work with the ansi warnings and stuff turned on?
    - Well that's my 4c Australian.

  2. #2
    does getche work for you?

    Code:
    char c = (char)getche();
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Anyone got a simple way to do it that will work with the ansi warnings and stuff turned on?
    No, under the constraints of the standard there is no way to do this.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    Originally posted by Inquirer
    does getche work for you?

    Code:
    char c = (char)getche();
    ~Inquirer
    I dunno what you mean...
    I tried this:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    char c = (char)getche();
    return 0;
    }
    but it says getche isn't defined, so assumably there is something I need to include...
    - Well that's my 4c Australian.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>there is something I need to include...
    maybe conio.h if you have it. (again, it's non-standard)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    [EDIT]
    I am sorry, i musn't have read your first post clearly enough. I didn't realize that you don't want to include conio.h. I have heard that it is possible to do it through raw communication with the istream cin, but i don't know how to approach this. Sorry for any inconvenience.

    ~Inquirer
    [/EDIT]

    [IGNORE]
    Yes, conio.h would be the one. Also, remember to cast it to a char. I had endless problems with this, and i wanted to warn you. Also, be careful of getche, because some (special) keys on the keyboard send two chars to the buffer, so if the user hits the left arrow key, it would return (char)224 the first time, and something else the second time. This can mess things up too. Let us know if you have getche, and we (I) will be happy to post some more example code o get you aterted.
    [IGNORE]

    ~Inquirer
    Last edited by Inquirer; 10-30-2002 at 03:30 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  7. #7
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    hmmm....
    just go get conio.h ... shouldnt it be downloadable somewhere if it dont come with the compiler?

    keyboard input would be great!
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  8. #8
    Read the FAQ, under "can't i just get the header" or something like that.

    http://www.cprogramming.com/boardfaq.html#header
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  9. #9
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    I HAVE conio.h That's where I was using getch from. conio.h is very nice, and getch is wonderful, BUT it's non-standard. The point is, I want to be able to do this using standard stuff only.
    Thanks for your help anyway people.
    - Well that's my 4c Australian.

  10. #10
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    so where do you get conio.h anyways? sounds like a nice file to have.
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  11. #11
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    Viper, did you read the faq like Inquirer said?
    You can't 'get' things like conio.h, you either have them or not.
    Bloodshed DevC++ has conio.h, that compiler is about 15Mb to download, and it's free, so go look for it if you want...
    - Well that's my 4c Australian.

  12. #12
    Thanks spiritofcat.

    Which compiler option are you using to make it only compile standard things?
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  13. #13
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    I mentioned them in my original post, but I'll be clearer this time.

    Options>Compiler Options: Directories tab, Add the following commands when calling compiler: -Wall -Wstrict -prototypes -ansi -pedantic

    Then when you try to compile something that uses conio.h or other non-standard stuff it gives you wanrnings and refuses to compile.
    - Well that's my 4c Australian.

  14. #14
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    i love bloodshed. but i didn;t search the header files yet. maybe ill find it.
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There is no way to do it portably, but why not just create a header for each implementation called "key.h" or something, and place the appropriate one in the directory at compile time (else name them different and use ifdef's). For the Bloodshed version just do something like:


    // key.h

    #include <conio.h>
    #define getkey _getch


    ...and in Bloodshed's conio.h, just do:


    // conio.h

    int _getch(); //...Colin Peters hid this one, define it, and it's yours
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console Mouse and Key Input
    By Yuri in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2006, 04:56 PM
  2. key input definition
    By hkmixxa in forum C++ Programming
    Replies: 3
    Last Post: 08-09-2004, 05:39 PM
  3. Proccessing Arrow Key Input
    By Xzyx987X in forum Windows Programming
    Replies: 10
    Last Post: 06-22-2004, 09:10 PM
  4. Allegro Specific key input...
    By o0obruceleeo0o in forum Game Programming
    Replies: 6
    Last Post: 07-01-2003, 12:41 PM
  5. Key input?
    By Kavity in forum Linux Programming
    Replies: 0
    Last Post: 12-20-2002, 11:01 AM