Link to home
Start Free TrialLog in
Avatar of Frosty555
Frosty555Flag for Canada

asked on

Emulate keyboard in C in Linux

I know a fair bit about linux, but I'm a bit of a C newbie. I know C up to about a 2nd year university level.... so... not a whole heck of a lot ^^; but enough to know how it works.

I've been asked to write a piece of software for a linux machine, which will run in parallel with another graphical application (so.. run in its own thread?) which is running full-screen. I want the C program to pause for a minute or so, then output some keystrokes in such a way that the other program that is running in full screen will receive them.

In windows it's easy, there's an API function called keybd_event() you can use for just that. But... to do it on linux I'm a little lost.

I will be executing this program from a shell script just prior to the full screen application loading. So when I run it I could probably put "&" at the end to signify that it runs in its own thread, unless of course I'm missing something.

As for the linux system, it is a very stripped down linux system. It does not have a desktop environment, and when it boots there is no "user" login. It just logs in as root and goes. There may not be much in the way of addon software or modules to work with on the system.
ASKER CERTIFIED SOLUTION
Avatar of lynxlupodian
lynxlupodian

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Frosty555

ASKER

This looks useful, but I am concerned about its reliance on /dev/kbde. I'm really looking for something more low-level.

I found this little semi-engrishy article on kbde here. I think I want to know what code would do exactly what kbde is doing. I think this explains it a little: http://kbde.sourceforge.net/kbde/readme.driver.html

Can you explain what this article is talking about?
Avatar of lynxlupodian
lynxlupodian

I didn't try it, but it looks like /dev/kbde could be arbitrarily changed. Or better yet, just symlink your real device to it. Though, the only way to get a keyboard /dev node I know of, is by having the event subsystem enabled in the kernel (CONFIG_INPUT_EVDEV=[my]). This is not uncommon to have, but for some reason I don't have it anymore.

You can also check its manpages for slightly better docs.
Checking through it... I've figured out so far that the kbde program registers the /dev/kbde device as a module on the linux kernel. The purpose of it is that when you write to it, it can immediately retrieve that data and use it. It's purpose is a conduit to pass information to the program. I suppose it means if you installed this, you could write to that file by any means you feel like, and have the program deal with it.

But I don't want to diddle with kernel modules and devices, I don't really have that kind of access to my linux machine.

All of this was in the kbde.c and ascii_kbde.c file, can you figure out which file has the source code that actually deals with reading from /dev/kbde and emulating the keyboard? If i could find that I think I'll be able to understand what it is doing.
Check the driver (module) tarball, it is split up:
http://sourceforge.net/project/showfiles.php?group_id=97955

This looks a bit more stale, but has a similar approach:
http://cvs.savannah.nongnu.org/viewvc/swinput/README?revision=1.5&root=swinput&view=markup
http://savannah.nongnu.org/projects/swinput/

If your application requires X to run, you could perjaps hook into its keyboard system to avoid the need for special devices or kernel modules.
looks like no matter which way I slice it, I will have to install a new keyboard driver on the linux machine. You can't synthesize a keyboard without calls to handle_scancode() which is a function used in keyboard drivers. And I'd rather use one that's already written rather than write my own.

So this works. I can install the kbde virtual keyboard and write to /dev/kbde from my C program.