Komodo Edit: play a sound when a key is pressed

I just started to try out Komodo Edit 4.2 under Windows as an programming editor, so I'am really a freshman here with Komodo Edit. After looking through the Komodo Edit macro capabilities, I tried to include some sort of macro, which should be capable of playing some typewriter like sounds, when keys are pressed inside the editor. The initial problems I had in finding a working solution were, that I don't know Python and also do not know much about the Komodo build-in Javascript functions.

So I came up in my very first sound play solution with three macros instead of just one, namely two little Python macros for playing wav-files under Windows and one main controlling JS macro, which called the related Python macros. - Well, since using three macros was somehow unattractive, the next step was to solve all inside just one Javascript typewriter.js macro, which is shown below.

In order to try out that macro, place it under some name into the Komodo Edit Toolbox and change the two sound URLs inside the code, so their paths point to some local wav files of your choice on your computer system.

// We first create a sound object and a 'typewriterInstance' which we can use to play sounds.
var typewriterSound = Components.classes["@mozilla.org/sound;1"].createInstance(Components.interfaces.nsISound);

// Next we define the locations for the sound files as URLs
var enterSoundURL = "file://d:/keyenter.wav";
var anySoundURL = "file://d:/keyany.wav";

// Since the sound object does not take an url as a string, we have to convert it to an URI.
// To do this we use io-service and call the function newURI.
var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var url1 = ioService.newURI(enterSoundURL, null, null);
var url2 = ioService.newURI(anySoundURL, null, null);

// Now we listen for keyboard events and play the related sounds
// if the enter or any other key is pressed
ko.extensions.play_sound_editor_focus = function(event) {
  if (event.keyCode == event.DOM_VK_RETURN) {
    typewriterSound.play(url1);
  }
  else
  {
    typewriterSound.play(url2);
  }
}
window.addEventListener("keydown", ko.extensions.play_sound_editor_focus , true);

// Note: this Typewriter JS macro has to be triggert after a file opens

Now having a single working macro with two related sound files, I next wanted to turn the whole solution into an easier to install Komodo extension. But how do you build a Komodo extension? - I've got some nice hints and reference links how to start building Extensions (xpi files) on the ActiveState community website, so I've read a little bit through some online references I was pointed to and also looked at some Komodo extension examples.

After some try and error I got my little typewriter.xpi extension for Komodo Edit finally ready and working, you can download it here for tryouts.

top