There is no "general" way to do it because it is operating system specific. C and C++ do not themselves have direct support for keyboards and mice.
You could use ncurses which has been ported to both MS-DOS and *nix (there is not MS-Windows gui version of it).
Ancient Dragon
Achieved Level 70
32,137 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,576
Skill Endorsements: 69
If you're using Windows you will probably want to use "SendInput" in the Windows API
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 110
Skill Endorsements: 0