Thread: non-blocking keyboard input?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    29

    non-blocking keyboard input?

    So ive been searching the web all over to find the right solution for a program im making, the program uses SDL to display a simulation. what i WANT to do, is make it so when i press a key, such as plus or minus, it changes the simulation or changes some variable, etc.
    something that can accomplish this:
    Code:
    while 1
    {
    if (keypressed = some key) do this;
    
    //simulation
    }
    Thanks a ton in advance!
    Jeromy

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If your compiler's library has conio.h you can use kbhit() and getch() ... but normally the keyboard is a blocking input.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    29
    Im using gcc in Fedora, how exactly is kbhit used?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    conio.h is an old carry-over from the DOS days, and there's no Linux port I'm aware of. But in Linux, you can use the curses (or ncurses) library for what you're after.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    29
    okay, could you give me an example?

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    29
    The problem im seeing with ncurses is that in all the examples im seeing it sets up some window that it receives the input from, but that window wont work alongside my SDL window.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I found this link getch alternative in SDL. This seems to address your problem.

    Jim

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    29
    Thanks! i think thats fixed it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (console) Non blocking input
    By Mario F. in forum C++ Programming
    Replies: 0
    Last Post: 08-23-2009, 11:22 AM
  2. Keyboard input
    By Darkinyuasha1 in forum Windows Programming
    Replies: 7
    Last Post: 06-19-2007, 03:21 PM
  3. non-blocking keyboard read
    By pppbigppp in forum Linux Programming
    Replies: 10
    Last Post: 01-17-2007, 06:51 AM
  4. Simple client - receiving data from server and keyboard w/o blocking
    By Spitball in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-08-2005, 12:32 PM
  5. Non-Blocking-Input And Overwriting-Output
    By Geolingo in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 05:47 PM