Page 1 of 1
Linux simulating (sending) key combinations
Posted: Tue Apr 16, 2019 8:55 am
by LiK137
Hi,
Is there possibility to send key combination under Linux ?
Thanx in advance
Re: Linux simulating (sending) key combinations
Posted: Tue Apr 16, 2019 3:56 pm
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.
Re: Linux simulating (sending) key combinations
Posted: Tue Apr 16, 2019 6:49 pm
by LiK137
Thanx for reply,
PureBasic SendKey, PostMessage... alternative for Linux
Re: Linux simulating (sending) key combinations
Posted: Tue Apr 16, 2019 8:51 pm
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);
}
Linux simulating (sending) key combinations
Posted: Tue Apr 16, 2019 9:33 pm
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.
Re: Linux simulating (sending) key combinations
Posted: Tue Apr 16, 2019 10:27 pm
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.