Hey, I was reading my book about C++ and read the default input piece of equipment was the keyboard, I was just wondering if there was a "simple" (ish) way to change it, example if i pressed a button on the monitor it would "cout << "monitor = pressed";" or w.e


8 Years
Discussion Span
Last Post by Duoas

> the moniter doesn't communicate with the PC in any way
Unless it's a touch screen. Ed has worked with those before, and it's treated just like a mouse in the code.


In reality, almost all hardware does both I and O with the system.

The difference is in how the device is integrated into the system. A keyboard is typically (and AFAIK, always) treated as an input device, so programs that interact with it through the keyboard device driver can only use it to gather input.

Another important attribute is the kind of data handled by the device. While everything boils down to bytes, the meaning of those bytes differs depending on the device. For a keyboard, numbers mean key presses and releases, which are translated to letters, etc, that your program can read from the device driver. For a monitor, the data represents color information.

So, like Ed said, if your monitor can recognize touch, the system will typically have two device drivers attached to the monitor: one for color display (like all monitors), and another that looks like a mouse driver to the application, but which is in reality translating touch information into the mouse data.

What your book is probably getting at is that C++ doesn't really care where the input comes from. Most terminals have a keyboard attached to them for us humans to play with. But that is not necessary, nor is it required by the C++ standard. That's why things like redirection work.
For example, the following program counts the number of lines input before ENTER is pressed twice:

#include <iostream>
#include <string>
using namespace std;
int main()
  string   s;
  unsigned count = 0;
  while (getline( cin, s ))
    if (s.empty()) break;
    else count++;
  cout << count << endl;
  return 0;

You can run the program normally:

C:\> count


C:\> count < foo.txt


In the first example, the program "count.exe" counted text entered by the user at the keyboard.
In the second example, the program counted the number of consecutive non-blank lines in the file "foo.txt", which is not a keyboard.

Hope this helps.

Be sure to adhere to our posting rules.