0

I have a main routine that loops infinitely. By changing bool variables using keyboard input, I want to be able to control whether certain if{} statements within that loop are getting called. I found this thread:
C non-blocking keyboard input,
but it seems excessively laborious and complicated for seemingly basic functionality. Is there an easier way to do it?

Community
  • 1
  • 1
Matt Munson
  • 2,903
  • 5
  • 33
  • 52

3 Answers3

3

You'll have to use the OS/Firmware/Framework/environment API to get input events, or use a library that do this for you. Anyway, there is no built-in way of doing this in C++.

I often use OIS in games. It's cross-platform and easy to use. Not sure it's useful for other cases than games but it does the job for you.

Klaim
  • 67,274
  • 36
  • 133
  • 188
  • @Klaim: Looks pretty cool, any idea how much compute time including this will add to each iteration? – Matt Munson Mar 05 '11 at 12:36
  • No idea but it depends on what exactly you use. First, understand that on windows it uses directx8 (that is installed by default anyway) to get the input informations so it retrieves infos indirectly from drivers. Now you can access the input informations in two ways : directly by getting the current state of the input device (mouse, keyboard, etc.) and checking it manually (with ifs in loops) OR by using an observer pattern and just provide a "listener" object that will receive input events. I guess the event way is more flexible but a bit more costly if you just want to check precise state. – Klaim Mar 05 '11 at 13:28
  • However, I suggest you shouldn't care about the cost : it's used in games, so you can guess that it's fast enough for most of applications, inclusing cpu intensive ones (like games). Anyway you shouldn't even ask now as input management will certainly never be your performance bottleneck. Don't pessimize and just make your app work first, then check what's making it less performant, then fix it. – Klaim Mar 05 '11 at 13:30
  • @Klaim: Alright. Well I'm using it for a genetic algorithm where I'm doing very many iterations per second and have relatively few lines of code, so I tend to worry about every little thing. – Matt Munson Mar 05 '11 at 15:13
  • @Matt, in that case, why not only check for input once every (say) 1000 iterations? (Depends on how quick an iteration is, obviously.) – Martin Stone Mar 05 '11 at 18:07
  • @Martin Hey, good point. Now that you mention it, I realize that it shouldn't matter how many iterations I'm running as long as I don't explicitly make a function call with each one. However, does using the library cause me to run another process in the background, like with directx for example? Thanx for the help. – Matt Munson Mar 05 '11 at 19:39
1

The SDL library is one way to do it cross-platform. Here's an example of polling keyboard events.

Martin Stone
  • 12,682
  • 2
  • 39
  • 53
1

Put the main routine in a thread, then have something like

static char mode = ' ';
while(mode != 27) // to allow Esc to end program
{
  mode = _getch();
}

Threaded code can then do different things based on what key was pressed.

OJW
  • 4,514
  • 6
  • 40
  • 48
  • I've never done threaded programming. Is that another project in itself? And would it increases computational demand significantly? – Matt Munson Mar 05 '11 at 12:42
  • Yes it would be a bit of work to learn threads. The alternative would be integrating another library into your application, which also takes a bit of time/effort. I don't think this approach would slow your program down. – OJW Mar 05 '11 at 13:06
  • cool, thanks. Seems like a cool alternative, especially if it's faster. Do you know how much it might slow things down (computationally) to use a library instead? – Matt Munson Mar 05 '11 at 13:25