You can imagine keylogger as sort of tapping device to collect your computer keystroke and other info, and secretly send it to somewhere else. It can be either hardware, or a piece of software. Installing hardware based keylogger requires physical access to the compromised machine, while a software based keylogger can be installed remotely by some trojans. Nowadays, as more and more people use internet for financial related activities, such as on-line shopping, stock trading, and bank transactions, there is a trend that malicious hackers collecting consumer personal information, such as on-line bank account credentials by using keylogging trojans. Here we focus on talking about software based keyloggers.
To understand how software keylogger works, we have to know a little about how our computer operating systems handle keystrokes. Let's take Windows OS as an example. What happens when you hit a key, say, letter "s" on your keyboard? The keystroke signal generates an interrupt, telling the OS to invoke the keyboard driver to handle the signal corresponding to "s", which we call "scan code". Then what does the driver do? It checks the language layout settings of your OS, finds out what language you are using, and translates the device-depended scan code into device-independent code that is called "virtual-key code", then creates a message including scan code, virtual-key code, and other necessary info, and puts the message into the Windows system message queue.
You have multiple applications running in your Windows--say, a browser, the Outlook, and a notepad. Which application this keystroke is related to? Well, now let's imagine that Windows OS has component works like a postman. He looks into the system queue, gets the message, checks who will be the receiver, and sends it to message queues for each application thread. When the application got the message, it does the same thing, and posts it to the corresponding procedure that displays the letter "s" in its corresponding window.
Now, let's talk about keyloggers. Based on where they hook on, keyloggers can be categorized into hook-based and kernel/driver-based. To make it convenient for application programmers to monitor message queue and diagnose problems, Micro Soft provides Windows Hooks API. Hook-based keyloggers just take advantage of such API. In general, a hook-based keylogger comes with an .exe file to initiate the hook, and a .dll file that does logging function. Such files usually hide in the system32 directory, or the keylogger installation directory. While running, the keylogger can access all windows messaging information, and even more--including some info not through keyboard input, such as IE's AutoComplete user credentials. However, since many anti-virus programs monitor Windows Hook APIs, hook-based keyloggers can be detected relatively easy.
Kernel-based keyloggers go further. They change the OS kernel files or drivers that handle the keystrokes or translating the virtual-key code to characters. For example, Windows OS has an Interrupt Dispatch Table (IDT), which tells the system which type of hardware interrupt should be handled by which driver function. Some kernel-based keylogger can modify this table, and make the os use a compromised keyboard driver to handle keystrokes. Implementing kernel-based keylogger requires more thorough knowledge of OS, so it's more difficult. But as a kernel-based keylogger is usually loaded into system earlier than other application programs, also they can be compiled into the OS seamlessly, it's very difficult, or impossible to detect some kernel-based keyloggers. Nowadays, the technology of researching, developing, and detecting programs that can hide themselves in an OS (they are called rootkits) becomes a big arm race between hackers and security professionals.