Ben Sandeen
Ben Sandeen

Reputation: 1553

Change C++ stdin from keyboard to another device

I've read that the std::cin thing in C++ (I'm paraphrasing here) "takes input from the standard input stream, which is usually the keyboard".

How does one change it to allow some other device to provide input? I've no idea what purpose I'd use this for, but I'm curious how one could do this nonetheless.

(I'm sure the answer is out there somewhere, but after searching for a while, I haven't found it; if you know of a great resource that would answer my question, I'd be very grateful if you could let me know what it is.)

UPDATE: Unless what I'm asking is not possible in normal C++, this is not a duplicate question. I'm not looking to redirect stdin/stdout to a file; I want to know if it's possible to redirect it to some other device like a mouse or a game controller.

Upvotes: 3

Views: 1185

Answers (2)

Toby Speight
Toby Speight

Reputation: 30841

Usually, you change it prior to starting your program. For example, standard input may come from a file:

./my_program </some/file/name

or from another process:

/some/other/program | ./my_program

You can change stdin from within a program, but it's almost always the wrong thing to do, and can cause surprise for anyone else working on it¹. If you're writing re-usable code that needs to perform input, I recommend that you pass the input stream (or file descriptor, or FILE* if you're working with C) into your functions, to allow the external code to open its own files if it wants (perhaps more than one file, and perhaps also pipes or sockets...)


¹ "Someone else" can often be read as "yourself in 6 months' time"...

Upvotes: 1

Michael Gaskill
Michael Gaskill

Reputation: 8042

The way that you read from other input devices is device-specific. You'd have to use the APIs on the platform that you're using; the APIs are different on each platform.

If you're really curious, you can google search for "C++ mouse input" or "C++ joystick input" (and even "C++ keyboard input" for low-level keyboard handling) to find the details that you're looking for. Add "Windows", "Linux", or "Mac" to the search to find results for your specific platform.

Note that you can find some excellent low-level device control, suitable for game development, on the Stack Exchange Game Development site.

Windows APIs

On Windows, the low-level device inputs require that you use the Windows event-handling system and handling the specific events that your app needs. Microsoft documents the low-level APIs on the Windows Dev Center.

The specific APIs are:

Mac OS X

Handling low-level input events on the Mac is different than on Windows, and the documentation on the Mac Developer Library is often presented in Objective C, rather than C++. It's useful to note that on Mac OS X, XCode will allow you to write hybrid C++/Objective C code called Objective C++, so you can easily leverage your C++ experience.

The individual APIs are:

Linux/*nix

Linux (and other Unix-like systems) can support X11, KDE, or Gnome GUIs, so those each have their own APIs. I won't enumerate them here, but you can easily locate the appropriate APIs for your purpose, if you want to develop for Linux, FreeBSD, OpenBSD, or any of the other Unix variants.

Abstraction Libraries / Frameworks

There are a number of libraries (C) and frameworks (C++) that provide cross-platform interfaces for low-level input and output. Some are very well done and widely supported, and often have game engines built on them.

Some very common and well supported libraries are:

There are also portable frameworks, typically used for developing more traditional applications, and these also provide abstractions for handling user input:

If you dig around just a bit, you can find many, many more portable libraries and frameworks. Not only will they generally make it easier to develop complex logic, you'll also have a significant advantage getting your code to build and run on another platform, entirely.

Upvotes: 2

Related Questions