inb(); not reading charaters as expected

I am sorry if this is in any way against the rules; the code isn't intended to be malicious, though it could be. If it is that bad, please delete/lock the thread instead of banning me.

So, I'm creating a keylogger, and it's not working as expected. Currently the program reads directly into the keyboard port (0x64), and then prints out that character. It echo's my characters when I type into the program, but when I type in a different program, it doesn't detect them. It's not very functional yet, but here is the code:

#include <stdio.h>
#include <sys/io.h>

#define KEYBOARD 0x64

main()
{
    unsigned char c;

    iopl(3);
    ioperm(KEYBOARD, 0, 1);

    while (c = inb(KEYBOARD))
        if (c != '4' && c != '5')    /* mouse related stuff */
            printf("%c", c);

    ioperm(KEYBOARD, 0, 0);
}

Thanks for reading/responding.

What platform, operating system etc?

Sounds like the kind of thing you would put in a DOS TSR program but in UNIX this would either go into a driver, or as part of a pseudo-terminal wrapper program.

I have an Intel processor, and this is under the Linux 2.6 kernel.

Are you writing this as a user process or part of the kernel?

I'm sorry I forgot to clarify these things.

This is meant to be a user process.

Surely IO ports are only accessible from drivers, and user processes are isolated from the hardware?

If you execute the program it echoes what you type into it, so it must be reading into the hardware correctly, right?

Have you deactivated other drivers/processes that are reading the keyboard?

I haven't, and how would i?

I would expect if you want to provide this kind of functionality it should be provided in either the

(a) keyboard driver,
(b) terminal protocol handlers
(c) the X windows keyboard handler

Do you understand how a keypress/scan codes come from a physical keyboard and finally end up converted into ascii characters in an input stream to a logical console?

I'm looking at some of the code (on my system: /usr/src/linux-2.6.21.5/drivers/char/keyboard.c), and I just seem lost in the code. Do you have any documents on this subject that I should read?

You are in the middle of the kernel.

I suggest you read up on how drivers work and how they interact with the rest of the kernel.

Are you sure you really want to be doing this?

The real question is "why not try?" The worst thing that can happen is I don't succeed.

Ah yes, but life is also short. I you want to, go for it.