Let's pretend you're running windows... PostMessage()
Ah, I think I found it: PostMessage() sends a "message" to the Window.
How Windows Programs Work: Each windows application runs a loop which checks for messages from the operating system. For example, when you press the left mouse-button down, the operating system sends a WM_LBUTTONDOWN message to the message queue.
Note that sumulating a key-press or mouse-click does not require accessing hardware (on a Windows system). It's done through the OS.
Windows programming is not that easy. Here's a Windows tutorial. You'll need to at least know how to get a handle to the target window, and you'll need to look-up the WM_ messages.
If you want an easy (Not C/C++) way of automating clicks & keystrokes, check-out WinBatch. I've used WinBatch to automate stuff that had to be run nightly.