Simulate keypress in a Linux C console application

后端 未结 2 1505
时光说笑
时光说笑 2020-12-01 08:46

Is there any way to simulate a keypress in Linux using C?

In my specific situation, I\'m on Ubuntu 9.04 and need a simple app that invokes a press on the \"pause\" b

2条回答
  •  自闭症患者
    2020-12-01 09:09

    There's a programmable library called 'xdotool':

    sudo apt-get install libxdo-dev libxdo2

    cat test.c

    #include 
    #include 
    #include 
    #include 
    int main() {
        xdo_t * x = xdo_new(":0.0");
    
        while(1) {
            printf("simulating Shift_L keypress\n");
            xdo_keysequence(x, CURRENTWINDOW, "Shift_L", 0);
            sleep(5);
        }
    
            return 0; 
    }
    

    Run like this:

    gcc test.c -lxdo -o test

提交回复
热议问题