Linux simulating (sending) key combinations

Linux specific forum
LiK137
Enthusiast
Enthusiast
Posts: 279
Joined: Wed Jun 23, 2010 5:13 pm

Linux simulating (sending) key combinations

Post by LiK137 »

Hi,
Is there possibility to send key combination under Linux ?

Thanx in advance
DarkDragon
Addict
Addict
Posts: 2218
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Linux simulating (sending) key combinations

Post by DarkDragon »

Which software do you use? X? Wayland? For X you can use the test part of the xlib, for wayland: no, it doesn't provide such an interface.
bye,
Daniel
LiK137
Enthusiast
Enthusiast
Posts: 279
Joined: Wed Jun 23, 2010 5:13 pm

Re: Linux simulating (sending) key combinations

Post by LiK137 »

Thanx for reply,

PureBasic SendKey, PostMessage... alternative for Linux
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: Linux simulating (sending) key combinations

Post by vwidmer »

Some C that maybe some one can put in to PB code to make work.

Code: Select all

/* Demo of how to emulate key presses 
(C) P.J.Onion 2014
No Warantee of any kind.  
Running this may cause the destruction
of the universe!  You have been warned !
*/



#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <regex.h>
#include <dirent.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/time.h>


bool pressKeys( void )
{
    static int keyboardFd = -1;
    int rd,n;
    bool ret = false;

    DIR *dirp;
    struct dirent *dp;
    regex_t kbd;

    char fullPath[1024];
    static char *dirName = "/dev/input/by-id";
    
    int result;
    struct input_event forcedKey;


    // Send ls<ret>
    uint16_t keys[] = {KEY_L,KEY_S,KEY_ENTER,0}; 
    int index;

    /* Find the device with a name ending in "event-kbd" */

    if(regcomp(&kbd,"event-kbd",0)!=0)
    {
        printf("regcomp for kbd failed\n");
        return false;

    }
    if ((dirp = opendir(dirName)) == NULL) {
        perror("couldn't open '/dev/input/by-id'");
        return false;
    }


    do {
        errno = 0;
        if ((dp = readdir(dirp)) != NULL) 
        {
            printf("readdir (%s)\n",dp->d_name);
            if(regexec (&kbd, dp->d_name, 0, NULL, 0) == 0)
            {
                printf("match for the kbd = %s\n",dp->d_name);
                sprintf(fullPath,"%s/%s",dirName,dp->d_name);
                keyboardFd = open(fullPath,O_WRONLY | O_NONBLOCK);
                printf("%s Fd = %d\n",fullPath,keyboardFd);
                printf("Getting exclusive access: ");
                result = ioctl(keyboardFd, EVIOCGRAB, 1);
                printf("%s\n", (result == 0) ? "SUCCESS" : "FAILURE");

            }


        }
    } while (dp != NULL);

    closedir(dirp);


    regfree(&kbd);


    /* Now write some key press and key release events to the device */


    index = 0;
    while(keys[index] != 0)
    {
    
        forcedKey.type = EV_KEY;
        forcedKey.value = 1;    // Press
        forcedKey.code = keys[index];
        gettimeofday(&forcedKey.time,NULL);
            
        n = write(keyboardFd,&forcedKey,sizeof(struct input_event));
        printf("n=%d\n",n);

        forcedKey.type = EV_KEY;
        forcedKey.value = 0 ;   // Release
        forcedKey.code = keys[index];
        gettimeofday(&forcedKey.time,NULL);       
             
        n = write(keyboardFd,&forcedKey,sizeof(struct input_event));
        printf("n=%d\n",n);
    
        index += 1;
    }

    close(keyboardFd);

    return(true);

}




int main(int argc,char **argv)
{
    pressKeys();
    return(1);
}

WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Linux simulating (sending) key combinations

Post by JHPJHP »

Hi Lik137,

Sending keystrokes in Linux can be found in my PureBasic Interface to OpenCV downloads.

See the LNX version of the download; includes folder, cv_procedures.pbi, keybd_event Procedure... two separate methods.
Last edited by JHPJHP on Wed Apr 17, 2019 4:02 am, edited 2 times in total.
LiK137
Enthusiast
Enthusiast
Posts: 279
Joined: Wed Jun 23, 2010 5:13 pm

Re: Linux simulating (sending) key combinations

Post by LiK137 »

Many thanx for both replies.
At least now I have JHPJHP's working example from OpenCV and would be incredible thankful for PureBasic way of C source (even do not have a chance to test that code) posted by vwidmer.
Post Reply