Hacker News new | past | comments | ask | show | jobs | submit login

I created a music auto-player since I was too lazy to play your guy's songs by myself. Paste this in the console.

  // SETTINGS
  var input = "rsa ecde srgu yhgr bv rsa ecde srgu yhgr bv hybtg ser erv";
  input += " hybtg iii r hybtg ser erv tvr rrr rvgres rrr rvg rrr rvgres ";
  input += "ggg rgh grs sxebbe cuuuhbgres grs sxebbe cbbbgvrsai";
  var TIME_INTERVAL = 300;

  // Add jquery
  var script = document.createElement('script');
  script.src = 'http://code.jquery.com/jquery-1.11.0.min.js';
  script.type = 'text/javascript';
  document.getElementsByTagName('head')[0].appendChild(script);

  function play () {
    // Sound array setup
    var soundArray = [];
    var down = $.Event("keydown");
    var up = $.Event("keyup");
    input = '  ' + input; // hack
    for (var i = 0; i < input.length; ++i) {
      soundArray.push(input.toUpperCase().charCodeAt(i));
    }

    // Start sound
    var index = 0;
    function playSound () {
      up.which = soundArray[index];
      $("body").trigger(up);
      ++index;

      down.which = soundArray[index];
      $("body").trigger(down);
      // // is there another?
      if (index < soundArray.length) {
        setTimeout(playSound, TIME_INTERVAL);
      }
    }
    playSound();
  }

  (function loadjQuery () {
    if (typeof jQuery === 'undefined') {
      setTimeout(loadjQuery, 100);
    } else {
      play();
    }
  })();



Huh, this doesn't seem to handle commas or semicolons at all. I can't immediately see why, since string.toUpperCase() maps "," to "," and ";" to ";", as it should. And switching input to

"uyvyu,pl,ynuuupl,kmk,,uyvuyvyu,pl,ynuuupl,kmk,,uyvppp;[[;plp;;;pl,kmk,ynuu,,,kmmml;pl,,kuu,lp;[,lp;l,"

completely breaks it about half way through.

Anyway, cool hack!


That's because the keydown event uses keyCode instead of charCode, and the two don't always match. Try typing Shift+U and then '[' on this page to see what I mean: http://unixpapa.com/js/testkey.html.

Since KeyboardJS is already loaded via requirejs, we can require it and then use it to get the keyCode for each character in the for loop.

    var KeyboardJS = require('lib/keyboard');

    [...]

    for (var i = 0; i < input.length; ++i) {
      var code = KeyboardJS.key.code(input.charAt(i));
      if (code) {
        soundArray.push(code);
      } else {
        soundArray.push(32);
      }
    }
Here's an updated version of the script, using your example as input: https://gist.github.com/peterjmag/489364ca58330c348c33


I particularly like input = "qwdtytdwqertytreqxctntcxzxctbtvwqwdtytdwqertytreqxctntcxzxctbtvwqwdtytdwqertytreqxctntcxzxctbtvwqwdtytdwqertytreqxctntcxzxctbtvwqwdtytdwqertytreqxctntcxzxctbtvw"


It's a me, Mario.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: