76816

C++, how to control program flow with keyboard input

Question:

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:<br /><a href="https://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input" rel="nofollow">C non-blocking keyboard input</a>,<br /> but it seems excessively laborious and complicated for seemingly basic functionality. Is there an easier way to do it?

Answer1:

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 <a href="http://sourceforge.net/projects/wgois/" rel="nofollow">OIS</a> 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.

Answer2:

The <a href="http://www.libsdl.org/" rel="nofollow">SDL</a> library is one way to do it cross-platform. Here's <a href="http://www.libsdl.org/docs/html/guideinputkeyboard.html" rel="nofollow">an example of polling keyboard events</a>.

Answer3:

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.

Recommend

  • java - string return method is called before any value is assigned to string
  • find the firmware in javascript
  • adb devices command doesn't show my xperia z - got proper drivers and all [closed]
  • Demistifying Firmware [closed]
  • How programmaticaly replace internal mechanism for SMS sending
  • Opencv Raspberry python3
  • Download postview image in original size
  • Android speech to text works in emulator but not on phone
  • How to read CSS property of Psuedo class in javascript
  • how to get sequence of user interaction with android device?
  • Setting time interval in HTML5 server sent events
  • SocketScan Getting the Battery Level in Swift
  • Generic button for go to next and previous tabItem Shiny
  • Keypress event is triggered twice for a single ENTER button press in Windows CE
  • Does Cocoa connection binding to NSToolbarItem prevent deinitializing?
  • Return JSX from function
  • Multiple values for an AND/OR statement
  • Conflict between Facebook Sign In and Google Sign In - iOS
  • Erratic data cursor behavior for triangulated 3d surfaces in MATLAB R2011b
  • Using c2hs on Windows
  • Programmatically auto-power on the phone
  • Which video formats to use for Java ME in mobile phones?
  • Simplifying the use of meshgrid in Matlab
  • Embed multiple jpeg images into EXCEL programmatically?
  • How does Skobbler prioritize which POIs to show?
  • Can I call custom javascript from an R jupyter notebook
  • Cannot connect to native local socket on android 5.1
  • Confirm box return value from alertify plugin and jquery
  • How to select table rows/complete table?
  • Get used tables from sql query [duplicate]
  • SharedPreferences or SQLite Database?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • java inputstream
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Spray.io: When (not) to use non-blocking route handling?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?