Thread: Couple of questions

  1. #1
    Registered User mr_raging_money's Avatar
    Join Date
    Mar 2012
    Posts
    18

    Couple of questions

    1. Is there a function that does non-blocking keyboard input in Dev C++ on Windows 7?
    I have searched this dozens of times and have either found options that do not work, or don't have anything to do with what I searched.

    2. Is there a way to make a function like scanf or getch wait for a certain amount of time, and then proceed on in the program even if no input was given? Kind of like Sleep(), but acts at the same time the function is waiting for input.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or read the FAQ, and find out how to read console input without waiting for a key press.
    FAQ > How can I get input without having the user hit [Enter]? - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    What you want, based on your previous posts from last week, is a non-blocking way to read the keyboard for a graphical game you're writing. I think I said then that it would be platform-specific, and those FAQ's have been up there forever. They do work, but rely on working in a "console / terminal" environment (you might think of a console as, say, a DOS box). If you want something more cross-platform, or more simple, then you're looking at a library that abstracts that sort of hardware access away for you - like SDL, Allegro, etc. They are basically DESIGNED to make this sort of thing easy. Both have event loops that will handle all the manky details for you, no matter what the platform you're running on, and also provide you with things like:

    An event loop that stores all keyboard / mouse events for you until you're ready to deal with them (i.e. start with the next frame) and where you can deal with them on a one-by-one basis (e.g. A was pressed, Z was released, etc.) or manually check the entire keyboard state at ANY point, (e.g. Tell me all keys which are pressed down NOW).

    int SDL_GetNumKeyboards (void) - Get the number of keyboard input devices available.
    Uint8 * SDL_GetKeyboardState (int *numkeys) - Get a snapshot of the current state of the selected keyboard.
    const char * SDL_GetKeyName (SDLKey key) - Get a human-readable name for a key.

    They have lovely, simple APIs, and you can use them in commercial programs too (check your license, but SDL 1.3/2.0 is now LGPL, I believe, and you've always been able to use them in self-contained DLL's, etc.) You don't have to use them to do EVERYTHING, you can use just the keyboard parts, for example. But there's a reason that so many commercial and indie games use those libraries and so few roll-their-own keyboard input routines.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also stop using Dev C++ if you want to be taken seriously.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  2. Couple Questions...
    By GameGenie in forum C++ Programming
    Replies: 4
    Last Post: 08-26-2005, 02:57 PM
  3. Couple questions
    By random in forum C++ Programming
    Replies: 7
    Last Post: 08-19-2005, 07:29 PM
  4. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  5. Couple Questions
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2001, 05:14 PM