?
Solved

Emulate keyboard in C in Linux

Posted on 2007-10-04
6
Medium Priority
?
957 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:Frosty555
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
lynxlupodian earned 2000 total points
ID: 20022045
Check this out:
http://linux.softpedia.com/get/System/Operating-Systems/Kernels/KBDE-11335.shtml

If you use that, you can do the whole thing in bash. You could also check its sources, to learn how it does the emulation.
0
 
LVL 31

Author Comment

by:Frosty555
ID: 20042276
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?
0
 
LVL 3

Expert Comment

by:lynxlupodian
ID: 20044119
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Author Comment

by:Frosty555
ID: 20047850
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.
0
 
LVL 3

Expert Comment

by:lynxlupodian
ID: 20048797
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.
0
 
LVL 31

Author Comment

by:Frosty555
ID: 20062167
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question