Keyboard Drums

Play a drum set with your keyboard

This is a project that I've wanted to do for a while. I have a bunch of 16 pad drum projects ranging from web apps to native desktop applications. I always used hotkeys and had labels on the 16 pads for each hotkey. But this would limit me to only part of the keyboard.

The hotkeys and pads were arrainged as such.

1 2 3 4
q w e r
a s d f
z x c v

I always wanted to be able to use the rest of the keyboard and I figured I would be able to expand the code to include more hotkeys at any time. I was wrong. Partially since when I reopend the project react and webpack had changed versions. My code was way behind. So I updated everything. And Started from scratch. First with the keyboard html css look then with the Javascript guts that made it work.

I was able to recycle some of my old code since It was so well documented and organized into different modules. This is my favorite thing. I'm always thinking about code re-usability. From this I've been able to export my modules to native apps and begin using them there.


Here is the keyboard listener function. I only need to launch this once for it to create a window event listener for the keyboard.

//Keyboard listener
keyboardListener() {
 window.onkeydown = function(e) {
 window.onkeyup = function(e) {

I want to listen for key down and key up since I want to stop the sound when the key is lifted. Both the handleHokeyDown and up functions get called by they hotkey listener and the click events on the dom elements as well.

Mouse Clicks

OnMouseDown fires when the finder is pressing down on the key. onClick fires when the finger leaves the mouse. Lets just say this is the event I use to stop the sound.

  className={`${['1'] ? 'active' : ''} key num dual`}>

Both of the handleMouseDown and OnClick events call handleHotkeyDown and handleHotkeyUp passing through the key.

Event Funnel

The sound playing code is quite simple. These functions pass the events to the sound manager class that holds all the Audio objects.

handleHotkeyDown(key) {
  if (keyAudios[key] !== undefined) {

handleHotkeyUp(key) {
  if (keyAudios[key] !== undefined) {

Play Sounds

What we do is make a huge object that contains key names that are the actual keyboard keys and the values are new keyAudio classes. This way all we need to do is look at the KeyAudiosObject[key] and we have our extended Audio object

class keyAudio {
  constructor(hotKey, audioSource) {
    this.audioSource = audioSource;
    this.key = hotKey; = new Audio(this.audioSource);

  play() {
    // Lets me rappid fire play the samples; = 0;
    // Play from start

  pause() {;

  makePromise(promise) {
    // Promise expected by google chrome audio policy.
    if (promise !== undefined && promise !== false) {
        .then(_ => {
          // Autoplay started!
        .catch(error => {
          // Error

Try it out for your self: Demo