It is currently Thu Dec 05, 2019 9:09 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Linux simulating (sending) key combinations
PostPosted: Tue Apr 16, 2019 8:55 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 206
Hi,
Is there possibility to send key combination under Linux ?

Thanx in advance


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux simulating (sending) key combinations
PostPosted: Tue Apr 16, 2019 3:56 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jun 02, 2003 9:16 am
Posts: 2093
Location: Germany
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux simulating (sending) key combinations
PostPosted: Tue Apr 16, 2019 6:49 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 206
Thanx for reply,

PureBasic SendKey, PostMessage... alternative for Linux


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux simulating (sending) key combinations
PostPosted: Tue Apr 16, 2019 8:51 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Jan 20, 2014 6:32 pm
Posts: 241
Some C that maybe some one can put in to PB code to make work.

Code:
/* 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


Top
 Profile  
Reply with quote  
 Post subject: Linux simulating (sending) key combinations
PostPosted: Tue Apr 16, 2019 9:33 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Oct 09, 2010 3:47 am
Posts: 1594
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.

_________________

I will be unreachable from the forum for an indefinite period; downloads moved to My PureBasic Stuff.
_________________


Last edited by JHPJHP on Wed Apr 17, 2019 4:02 am, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Linux simulating (sending) key combinations
PostPosted: Tue Apr 16, 2019 10:27 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 206
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye