Hi,
Is there possibility to send key combination under Linux ?
Thanx in advance
Linux simulating (sending) key combinations
-
- Addict
- Posts: 2228
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Re: Linux simulating (sending) key combinations
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
Daniel
Re: Linux simulating (sending) key combinations
Thanx for reply,
PureBasic SendKey, PostMessage... alternative for Linux
PureBasic SendKey, PostMessage... alternative for Linux
Re: Linux simulating (sending) key combinations
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
Linux simulating (sending) key combinations
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.
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.
Re: Linux simulating (sending) key combinations
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.
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.