Press and release a key with Linux
<p>I'm trying to make a little program that types for a user, using c++.  I've found the method <code>keybd_press(...)</code> but it seems as though that only works with the Windows header.  Any way to achieve this in Linux?  Basically what I want it to do is emulate a key being pressed for me, an auto-typer if you will.  I'd like it to be ran from the terminal, read the input from the user, and then type it until the number of times it should be typed is reached.  I can do all of that stuff, I just can't figure out how to emulate a key being pressed and released.</p>

<p>I don't think code is really necessary but this is what I'm working with:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main () {
    string mystr;
    cout &lt;&lt; "What key would you like me to press? ";
    getline (cin, mystr);
    pressKey(mystr);
    return 0;
}

void pressKey(string str) {
    const int KEYEVENTF_EXTENDEDKEY = 0x1;
    const int KEYEVENTF_KEYUP = 0x2;

    keybd_event(VkKeyScan(str), 0x45, KEYEVENTF_EXTENDEDKEY, 0);
    keybd_event(VkKeyScan(str), 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
</code></pre>

<p><strong>Note:</strong> <em>I wouldn't mind using Shell for this if it is better, if it is an example would be highly appreciated!</em></p>
