开发者

Press and release a key with Linux

I'm trying to make a little program that types for a user, using c++. I've found the method keybd_press(...) 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.

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

#include <iostream>
#include <string>
using namespace std;

int main () {
    string mystr;
    cout << "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);
}

Note: I wouldn't mind using Shell for this if it is better, if it is an example would be highly appreciated!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜