The Matrix Reloaded: Now Better Than The Movie

If you’re looking for a custom controller for a MAME cabinet build, CNC machine, or just want to control a robot build, you’re going to need to wire up some buttons. You could wire up a bunch of buttons to a microcontroller, but if you use an old computer keyboard the work is already done for you.

[Rupert] sent in a great tutorial on repurposing old keyboards. The build is very, very simple: just take a multimeter to each contact and measure the rows and columns for continuity. Once [Rupert] had the matrix codes for every button on the keyboard, he wired up a length of ribbon cable to the keyboard PCB. From there, a small breakout board provides all the connections that a MAME cabinet would need.

Opposed to a custom keyboard encoder like an I-PAC or a homebrew solution, [Rupert]’s build is very easy and can be built for only the dignity required to dumpster dive for a keyboard.

34 thoughts on “The Matrix Reloaded: Now Better Than The Movie

    1. Keyboard scan codes are strange that way.

      The only keys that do not block other key presses are certain modifier keys,
      specifically: Shift, Control, Alt/Option, Window/Apple/Command key, Break/SysRq, and if you have one, the windows “right-click” key.

      For a PS/2 keyboard (Generally only having Shift, Control, Alt, and Break), this gives a total of 5 keys you can press at the same time.

      USB however is a bit more liberal, although still limited.
      You get 4 modifiers (or 5 with the right-click key), but then the rest of the keys are broken apart into groups.
      In my experience generally the keyboard part will be two groups, although I’ve seen 3 and 4 before.
      Then the numeric keypad is a group, and the center keys (arrows, pg up/down, etc) are a group.

      This gives you between 7 and 9 keys able to be pressed together.

      I’ve never looked into how “media” keys are handled. It would be nice if that was another group all together.

      Older consoles (and their emulators) generally only support 4 directions.
      This gives you a directional/D-pad and 3-5 buttons.
      Enough for NES (8 total), but pushing it for SNES (12 total)

      As for MAME, it of course depends on the game. Some will work fine, but not all by far.
      Once you go up to 8 direction (IE diagonals) that doesn’t leave much left over for buttons.

      Typically if you press two buttons in the same group, the second key press will override the first. Some keyboards however will ignore the second key and instead send the key-up event for the first key still being held.

      This is also why most emulators that use a keyboard will use control/alt/shift for buttons, and ASDW or arrow keys for directional control.
      Also two player games will use ASDW for #1 and arrows for #2, because each of those are in a different group and won’t block each other.
      Aside from diagonal (Not always supported), console controllers are designed so you can not press combinations such as up+down and left+right, so with clever grouping and assuming 4 direction control, this will usually work fine.

      An interesting detail, the Nintendo and Super Nintendo had ‘developer controllers’ with two extra buttons on top.
      These actually press up+down and left+right, and quite a few games use this to access debugging (aka cheat) functions and menus.
      It is assumed that normal controllers are not physically able to do this, so they don’t always remove the debugging functions from the final release of the game.
      On some games, they take programming shortcuts (due to ram/rom limitations) and assume up+down or left+right will never happen, and so have no handlers if they see it. This can cause anything from interesting glitches/exploits, to boring crashes or freeze ups.

        1. @bob:

          Hmmm… N – NE – E – SE – S – SW – W – NW

          4 of the 8 directions require 2 simultaneous inputs…

          Times how many players = how many inputs???

          Trust me, I’m always looking for a way to cheat the system, I’ve never won with a keyboard hack… better to hack a cheap USB controller or even a Guitar Hero/Rock Band instrument, even through an adapter for PS/2, GameCube etc.


    2. (PS, sorry for the wall of text at the top, that was supposed to go down here)

      But I also wanted to add, if you use Linux as the OS and are good at programming at the hardware level (This is hackaday right? ;) you can even exceed the worst-case 4 simultaneous button limit if you have enough cheap keyboards and a USB hub.

      Linux addresses all USB keyboards separately. You can have your main keyboard work normally, and one or more extra keyboards read by a script or program to be parsed.

      You can then put 8 USB keyboard controller chips in an enclosure with a USB hub.
      This would increase the number of buttons you can press simultaneously.

      (For technical details, search for “Linux multiseat”)

      At this point thou, it would probably just be better to cannibalize some USB joypads that use the joystick protocol instead of HID.
      Then you get analog as well as no simultaneous button problems.

    1. Easier still was when I used a scanner and a paint-bucket tool to visually trace the two parts. Then you just check key X for color Y and check which pin it connects to on the board.

      Using the Gimp, I had to tweak the sensitivity of the paint bucket until it ran the wire without stopping or jumping into the blank space, but much faster then doing 101 keys by hand.

  1. Yes, most keyboards are rather limited in the number of simultaneous key presses, and controller moves can be blocked if some ‘keys’ are held.

    This keyboard hack method works fine for games like pac-man where only one direction can be pressed at a time, however modern fighters for example would be unplayable.

  2. Hacked keyboards do not make good MAME controllers because of the limited number of keys that can simultaneously pressed and key repeat… better to spend the $$$ for a I-PAC or similar controller adapter


      1. IPac, IMHO, is the best, but KeyWiz is also a good one. Downside with KeyWiz is that you have to make sure MAME is using the defaults the KeyWiz uses, because if you power cycle, the KeyWiz forgets what buttons each input is wired to. With the IPac, you can program inputs to specific buttons in NVRAM. It’s quite convenient, although MAME allows you to map anything to any key anyway.

        There is a KeyWiz version that is half input, half LED controller, and I gotta admit THAT’s really cool.

      2. I guess has the “GP-wiz40” for about $40. I’d probably just go ipac, I guess. I have a POS lappy, with some cool features from the factory (ie: POS dvd, POS monitor…) that I’d like to make into a MAME.

      1. Gah, forgot to say that it may also depend on the kind of keyboard. Those designed for gaming is less likely to have this issue than the el cheapo that is barely able to enter a URL or hammer out a CV.

      2. It’s actually unrelated to the connector. The AT and PS/2 connectors can have as many keys pressed simultaneously as you like, it’s just that most keyboards can’t do that. When you press too many keys as dictated by the keyboard design, the keyboard itself sends scancode 255 (IIRC) to the computer, which typically in those days resulted in a beep.

        Likewise the USB HID protocol has no limit on the number of keys pressed either. Like AT and PS/2 it just sends a code every time a key is pressed or released.

        You could easily use this for MAME if you had a small number of buttons, but as others have pointed out there are better solutions if it is important to be able to hold down more than 7-8 buttons simultaneously.

        But whether for MAME or something else, USB keyboards and mice are great ways to get a cheap interface going.

  3. I went with an iPac in my current MAME build simply because it’s very time consuming to find non-ghosting sets of buttons with a keyboard matrix like that. I have used a hacked KB matrix before though, and if you are careful to avoid ghosting combinations it’s generally quite reliable.

  4. Having done more than a few of the posters here and read the actual article though, I can attest that this build was not actually intended for MAME, but just for general simple keyboard interfacing. For that purpose it actually will work very well.

  5. I ran into this problem myself and ended up modding the sources to use a custom JAMMA circuit board that used 3 shift registers to handle two joysticks and several buttons through the parallel port. blocked keys are simply unacceptable for fighting games

  6. I tried this for my mame machine,getting all the combinations was extremely exhusting. but possible.

    the problem i had was getting the wireharness i created to solder to the keyboard. im not the greateset solder in the world but i think i can hold my own. the solder just would not stick to the keyboard. ended up giving up and bought a i-pac.

  7. If you’re doing a mame cabinet/controller build, you could look into doing this same kind of thing with usb game controllers. I have a ton of 6 button + d-pad controllers floating around for this purpose, I suspect all keys are individually scanned, and it’s nice that the computer reads them as game controllers.

    1. Also of course, if you’re using a console to actually run the game, like let’s say you’re playing the new Marvel-Vs-Capcom, you’d probably HAVE to wire up controllers

  8. I agree with arfink’s comment

    Sometimes its nice to read the article before posting.

    While its nice to hear comments regarding MAME its not the only reason for doing this hack. There are plenty of other programs/projects that require a custom set of keys that do not run Street Fighter , Video editing,photo editing and CNC to name a few.

    But on the topic of MAME what happened to playing proper arcade classics that dont need to remember 20 key combinations at once erm GYRUS,Pacman anyone :) I’m sure it cope admirably and doesn’t need a $50 controller.

    in the spirit of the Matrix “There is no spoon” and always remember to “free your mind !”

  9. I did something similar with a wireless keyboard:

    It would probably work well with simple games like Galaga or Pacman, but if a ton of simultaneous keystrokes are needed (like in Metal Slug or more complicated games) it wouldn’t register all of them. I eventually used an actual MAME controller which worked nicely:

  10. Did a similar project. I attempted to bring usb life to an old old keyboard. It never worked out because the number of microcontroller ports started to add up but I actually used photoshop instead of a multimeter :p (I scanned the sheets)

  11. People have been doing this for flight sims for years. In that application, when using a keyboard for flip switches, they were converted to momentary switches. The same approach could be used with push-buttons to overcome the simultaneous button pressing issue, e.g. modify the button such that pressing it would produce a press and release and the same when the button was no longer depressed. Although this approach would require software support to treat the button press as a toggle,

  12. I tried this, but I soon gave up due to the difficulty of visually tracing the wires; soldering to the keyboard circuit board without killing something, the fact that my intended MAME PC didn’t have a PS/2 input, etc.

    I thought about using IPac or KeyWiz, but instead went with a teensy for about half the cost. You have to write your own code, but get complete control over behavior in exchange. And your device can be a USB keyboard, joystick, and mouse simultaneously if you so desire. In my case a keyswitch puts everything in “admin” mode, and the joystick controls the mouse cursor.

  13. instead of using a keyboard, if you are going with a usb interface i recommend breaking open a set of playstation 2 buzz controllers. it gives you a total of 20 inputs and needs no drivers under windows.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.