Question:
Multiple inputs in c++ simultaneously?
Cheese
2021-03-08 08:54:32 UTC
I see it everywhere.  Video games and servers.  Productivity programs.

Keyboard input, socket input, mouse input, multiple keys simultaneously.  

But in c++, I can't find the name of what makes this possible.  

Console apps only allow one line of user input at a time or use getch() for one key at a time.   

What is this multitasking of inputs called?   Seems like I'm the only one who doesn't know this.  And people I ask don't know what I am asking.  
Three answers:
Robert J
2021-03-09 16:29:31 UTC
The key to all real-time programming is that you never wait for anything.



If some input or output device is not ready when you check it, leave it and check the next one etc.



You have to keep track of the state of every item, but the overall program runs smoothly with no pauses or glitches.



That applies to multiple input devices - you check each for new data, either store that in a buffer if it's not enough to process or do process it if appropriate, then do the next thing, new data or not.



You may also be able to use interrupts, depending on the system and program.
husoski
2021-03-08 16:03:29 UTC
The most common term I know of for this is "asynchronous I/O", and it's mostly defined and supported in the operating system and its device drivers.  The C++ language is officially platform-neutral, so you're not going to find much direct support in the standard C++ libraries. 



The "getch" function from Microsoft C is a good example of that.  It was an IBM/PC BIOS function, implemented as a C function defined in the compiler-specific header .  That's not part of any language standard for C or C++, and does not exist on Linux, MacOS, etc.



So, you either study the OS you're going to use--and it's system calls--or you use a library that does that for you.  A game engine is a good example of the second approach.
Daniel H
2021-03-08 15:41:38 UTC
Those games do not use getch. 



Look into keyboard and mouse states and the ability to poll those states at any point in your game.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...