LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-21-2018, 08:13 AM   #1
Kajpio
LQ Newbie
 
Registered: Feb 2018
Posts: 2

Rep: Reputation: Disabled
Exclamation Keyboard Emulator in C++


Hi,

I'm trying to code a KeyBoard Emulator for Linux (based on jtyrrell, a member of this forum), first I thought to use python to achieve it, but all the libraries were using X (and I don't want to use it). I decided to code in C++ to write in the keyboard buffer. After a few days of learning C and doing some research on Linux and how the input system works, I came up with this:

#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sstream>
#include <unistd.h>

#define EV_PRESSED 1
#define EV_RELEASED 0

int Emulate(char character)
{
printf("Starting the keyboard buffer writer\n");


int fd = 0;
char *device = "/dev/input/event1";

if( (fd = open(device, O_RDWR)) > 0 )
{

struct input_event event;
printf("The keyboard code is: %d \n", KEY_A);

event.type = EV_KEY;
event.value = EV_PRESSED;
event.code = KEY_A;
write(fd, &event, sizeof(struct input_event));

event.value = EV_RELEASED;
event.code = KEY_A;
write(fd, &event, sizeof(struct input_event));
close(fd);
}
return 0;
}


int main(int argc, char *argv[]){
for(int i=0; i < sizeof(argv[1])/sizeof(int); i++){
Emulate(argv[1][i]);//for each car in the argv 1 call Emulate
}
}
As you can see I'm writing manually KEY_A to press the a key. And I was wondering if there is a function or a way to change a char to the keycodes defined in the file /usr/include/linux/input-event-codes.h.

Thank you for your help !

Last edited by Kajpio; 02-25-2018 at 10:25 AM.
 
Old 02-22-2018, 06:08 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Just FYI, there is already a program that does that: xdotool
https://www.semicomplete.com/projects/xdotool/

It comes with a library in C, maybe you could use it to make your own program

Edit: Oh you don't want to use X, sorry...
Maybe use a character codes array and a character value / array index mapping

PS: your program is in C not in C++

Last edited by keefaz; 02-22-2018 at 06:14 PM.
 
1 members found this post helpful.
Old 02-25-2018, 10:29 AM   #3
Kajpio
LQ Newbie
 
Registered: Feb 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Edit: Oh you don't want to use X, sorry...
Maybe use a character codes array and a character value/array index mapping
Damn Xorg

Thanks for the advice, I ended up doing a big dictionary in a Python script, and then this script launches the C program!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Microwriter (chorded keyboard) emulation on standard keyboard (not music chords) flexdream Linux - Newbie 4 01-15-2024 07:39 AM
keyboard does not work CentOS 6.7 Rosewill striker RK-6000 keyboard jeduffy CentOS 10 11-07-2015 04:54 PM
Windowmaker: bound keyboard commands use qwerty keyboard but within apps, dvorak. daweefolk Linux - General 2 09-01-2012 05:21 PM
Emulator woes - emulator: ../../src/xcb_io.c:249: process_responses: Assertion GunNam Linux - Mobile 1 03-30-2011 03:55 PM
USB Keyboard Emulator for Linux 2.6 OS kernal pgill Linux - Software 0 07-25-2005 05:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration