Emulate keyboard in C in Linux

Posted on 2007-10-04
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.
Question by:Frosty555
    LVL 3

    Accepted Solution

    Check this out:

    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.
    LVL 31

    Author Comment

    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:

    Can you explain what this article is talking about?
    LVL 3

    Expert Comment

    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.
    LVL 31

    Author Comment

    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.
    LVL 3

    Expert Comment

    Check the driver (module) tarball, it is split up:

    This looks a bit more stale, but has a similar approach:

    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.
    LVL 31

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    isEverywhere  challenge 19 48
    post4 challenge 28 70
     shows up in Outlook, not OWA or on phone 3 45
    factorial example challenge 10 44
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now