Introduction: Finding the Keyboard Matrix
Recently, I have come to realize that keyboard controllers are a cheap and easy way to make simple HID input devices. They can be used for a variety of devices that only require button presses, such as a gamepad, or adding extra functionality, like bluetooth connectivity, to another keyboard. Yes I know, you can use a Teensy, Bluefruit, or an Arduino for the same purpose, but USB and Bluetooth keyboards can be acquired for a fraction of the price and have a LOT more buttons (over 100, compared to just 10 or 20). You may even be able to find keyboards on the street from people who want to throw them out. At first, I saw the potential in using keyboards in my projects, but I had no idea how they worked, and the Internet wasn't of much use as far as I could tell. However, after some investigation, it turned out to be quite straightforward.
Step 1: What's Inside the Keyboard?
This depends on what kind of keyboard you're taking apart. Most keyboards tend to have a rubbery membrane with indentations which are flattened when you press a key. Upon flattening, it pushes two thin sheets of plastic with a printed circuit on it together, sending a signal to the keyboard controller, which is the chip generally located in a corner. If the keyboard was cheap, it most probably has this membrane. If you are taking apart a mechanical keyboard, there won't be the rubbery membrane, and in its place you will have either some switches or buckling springs (like in the IBM M). Unless you want to keep the keyboard itself, the only things you want to keep are the controller itself and optionally the thin sheets of plastic with the circuit.
Step 2: How the Keys Work
Each key on a keyboard is itself a kind of "coordinate". When a connection from one sheet of plastic touches one from the other sheet, it acts as a switch for two different cables present on the controller. By combining different connections with one another, you can get a variety of keys out of a small number of connections. By looking at the plastic sheets, you can see how many connections are on each sheet. In this keyboard, there are 8 on one sheet, and 18 on another, which means it has a 8x18 matrix, which allows for a maximum of 144 keys.
Step 3: Finding the Matrix
To simplify things, I numbered the 18 first connections and lettered the 8 following connections, which I then make a table out of, filling it out with each key I find as I go along.
So far, I have found two ways to find the matrix of the keyboard.
The first method involves tracing each key on the plastic sheets back to its connection on the controller, which gives you a letter and a number, giving its "coordinates". The advantage to this method is that it doesn't require the use of a computer or phone, but it is a boring and long task. Apart from being long, the keyboard controller may have some keys programmed into it which are not visible on the sheets.
The second method involves the use of a computer, tablet, or phone. The idea behind it is to use a short length of wire (or equivalent, a paperclip will do), and using it to short the connection between every possible combination of keys available to the controller, in this case 144 connections. You put one end of the wire on one of the 18 numbered connections and another on one of the 8 lettered ones, which sends a signal to your device of choice as if a key were pressed. On your device, you must have software that displays the scancode or name of the key being pressed.
On Linux, this is quite easy, as it is already integrated into the system. Open up the terminal, and type in "xev" (without the quotation marks). You can then go on pressing buttons to your heart's contempt, and your terminal window will give out the scancodes of each of those keys.
On Android, you must download an app to show info about the key being pressed. One such app is KeyTest, which you can download from here. Note, you may not always be able to connect your keyboard to your Android device. If it is USB, you will need a USB OTG cable, and a phone whose kernel supports having the MicroUSB port serving as a host. If you have a higher-end device, your phone will most probably have such capabilities. As far as I know, all Nexus devices have OTG capabilities, as do most Sony devices and the better Samsung devices. You will have to do some research as to whether your device supports it or not. If your keyboard is bluetooth, you shouldn't have to worry about this.
On Windows, you also need to download extra software. I found that KeyCastOW suits my needs, as it is relatively lightweight and requires no installation. You can grab a copy from here. Once downloaded you just have to double click on the executable file, there is no need to extract it.
I have no experience with neither OSX nor iOS, so you will have to investigate for yourself.
You should end up with a table similar to the one above.
Possible method 3:
I have read on some forums that on some bluetooth keyboards with a Broadcom chip, people have managed to get a full table of the matrix by dumping it out of an EEPROM located on the chip, and can even edit the matrix, which means you can tailor the controller to suit your needs and have no compromises. I have attempted to do this myself, by dumping the contents out of the EEPROM using the I2C protocol available on the Raspberry Pi. I then had a 16KB file on which the matrix was stored somewhere in hexadecimal values. However, I was unsuccessful when it came to finding the keyboard matrix inside the contents of the EEPROM. I will update with more info if I am able to recognize the matrix in the EEPROM dump.
Step 4: Putting It to Use
You now have a chip which sends keypresses to a computer, phone or tablet. You can use it for a variety of things. For example, you can turn your old NES controller gathering dust in the corner into a wireless bluetooth gamepad for your phone or computer, build your own keyboard out of LEGO, or anything you can possibly imagine which involves the use of buttons. Personally, I have used keyboard controllers to control the buttons of two separate gameboys, one USB, one Bluetooth. I also have used it to convert a calculator into an input device for my phone, and I am currently working on converting my old IBM M keyboard into a bluetooth one (by changing the matrix in the EEPROM).