ActiveState Community

Komodo Edit: play sound when a key is pressed

Posted by vkyr on 2007-11-13 10:18
OS: Windows

I just started to use Komodo Edit 4.2 under Windows as an programming editor, so I'am 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. - The problems I had in finding a working solution were, that I don't know Python and also not much about the Komodo build-in Javascript functions etc.

However, I found an -at least for me- easy way to overcome with the above mentioned problems. I believe that for people who know Komodo, Python and JS very well, this might be a little unusual and maybe complicated way to solve the problem. Let's start my way of howto...

In order to play a wav sound file I just found somewhere a hint how to do this in Python under Windows, but I couldn't find any hint how to do this instead via Javascript for Komodo Edit. So I created two minimalistic python macros, one for playing a "keyenter.wav" sound file when the key is pressed and the other one for playing a "keyany.wav" file for any other pressed key. Here is an example of one of those otherwise nearly identical python macros:

file='d:/keyenter.wav'
from winsound import PlaySound, SND_FILENAME, SND_ASYNC
PlaySound(file, SND_FILENAME|SND_ASYNC)

So far so good, when executing the pythos macros by double clicking on them from the toolbox, the sound files are played. But the next problem was how to hook/call these macros from some other third macro, which has to know how to react on certain keypress events.

I didn't found any Komodo python example which showed how to react or listen to key press events and even to find such an example for Javascript was difficult. If I would know how to react on Komodo keypresses from some python code, I would have used just one macro file which handles all, so I was forced to use the informations I just could find and thus ended up by using another Javascript macro, which listens for the key events and then calls the related py macros.

var anySound = ko.projects.findPart("macro", "Play_any_snd", "toolbox")
var enterSound = ko.projects.findPart("macro", "Play_enter_snd", "toolbox")

ko.extensions.play_sound_editor_focus = function(event) {
    if (event.keyCode == event.DOM_VK_RETURN) {
        enterSound.invoke()
    }
    else
    {
      anySound.invoke()
    }
}
window.addEventListener("keydown", ko.extensions.play_sound_editor_focus , true);

The above Javascript macro is the main one, which recognizes if an enter-key or other key is pressed and then in turn calls the related python play sound macros. This macro is setup to be triggert after a file opens in Komodo Edit.

I've attached my whole little solution (also the sound files) as a zip-file to this posting for people who might want to give the whole thing a try.

Of course I would be interested in some examples and suggestions how to make all this much more compact and better, by just using one Javascript or Python macro which handles all. So any hints from Komodo JS or Python experts are welcome.

AttachmentSize
typewriter.zip60.39 KB
typewriter-0.1.xpi63.55 KB

ToddW | Tue, 2007-11-13 16:35

This is pretty cool.

I have not tried using this, but you may want to take a look at these Mozilla links for working with Sound, you'd then only need the one JavaScript macro which should then work on all the platforms:
http://www.xulplanet.com/references/xpcomref/ifaces/nsISound.html
http://books.mozdev.org/html/mozilla-chp-5-sect-4.html

This would make a nice extension, packaging up both the JS code and the sound files to make for an easy install.

Cheers,
Todd

vkyr | Tue, 2007-11-13 19:46

Thanks for the infos Todd, those links were very helpful for me!

I have now stripped the previous three macros down into just one Javascript Typewriter macro, which seems to work fine so far (at least on my Windows box).

// 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);

// Here 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 I have to see, learn and tryout how to build an extension, so that this Typewriter sound macro and it's referenced/related sound files can all be installed via a package in one common place.

ToddW | Wed, 2007-11-14 11:46

You can create the layout and base extension data using the Komodo extension template:
"File->New->New Project from Template" then choose the Komodo category.

Some useful Mozilla extension resources (the same details apply to Komodo):
http://developer.mozilla.org/en/docs/Building_an_Extension
http://kb.mozillazine.org/Getting_started_with_extension_development

Cheers,
Todd

vkyr | Wed, 2007-11-14 19:19

I've did a little tryouts by using the Komodo extension template, but without much success so far.

With the template I've created a typewriter extension project and placed into the /content folder the two wav-files and the above JS macro as a script file (typewriter.js).

Inside the typewriter.js script I've changed the URLs to the wav files in this way...

...
var enterSoundURL = "chrome://typewriter/content/keyenter.wav";
var anySoundURL = "chrome://typewriter/content/keyany.wav";
...

I added to the "overlay.xul" file a XML line for typewriter.js script...

...
<overlay id="typewriterOverlay" 
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
   
<script src="chrome://typewriter/content/typewriter.js" type="application/x-javascript" />
...

I can then build the project, so that the result is a typewriter-0.1.xpi zip file which contains the appropriate parts. The resulting xpi extension file can be installed, but it seems not to work or do anything useful at all.

Some questions which came to my mind related to Komodo extensions are...

1.) Will the script be triggert/enabled automatically by itself when the extension is installed, or does it have first to be initialized/called in some way in order to be activated at all?

2.) I'am not sure if an URL like "chrome://typewriter/content/keyenter.wav" is a correct reference to a sound file inside the typewriter extension package?

3.) Since I didn't used any own prefs, menus or the like (only the default by the template prebuild hello world one), I wonder how I can determine/detect under Komodo Edit, if my JS extension script is enabled or runs at all? - Is there some debugging or trace/log mode facility inside Komodo Edit for extensions?

vkyr | Thu, 2007-11-15 05:44

I now succeeded with the typewriter extension, it seems one has always to initialize an extension in some appropriate way.

I looked into the todo.xpi extension (as an example) and realized how this extension did some initialization after Komodo has finished loading. So I changed my typewriter.js script slightly and adapted that same initialization scheme...

...
// Initialize once Komodo has finished loading
addEventListener("load", function() { setTimeout(ko.extensions.typewriter.OnLoad, 3000); }, false);

Now the typewriter extension seems to work alright. I've attached the typewriter-0.1.xpi extension above to the initial thread posting for tryouts.

ToddW | Thu, 2007-11-15 13:08

That sounds so retro :D

Thanks for updating the forum with your workaround, I was previously thinking it was more of something to do with ".wav" files being inside of the chrome/jar file.

I guess it is more to do with waiting for the main window to initialize/appear before adding the event listener to it...

Thanks,
Todd

vkyr | Sat, 2007-11-17 20:01

I would like to give some suggestions, where it would be IMO useful to maybe also have the ability to enable some sound support inside of the Komodo products.

In the past I worked a lot with Borlands own JBuilder Enterprise IDEs for all kind of Java/J2EE etc. programming (nowadays I use mostly Eclipse or Netbeans for Java programming). One of the neat features, which I always liked in former JBuilder versions, was the ability the enable sound support for certain IDE tasks like for example:

  • Compilation/build success and error sounds (the later was a distinctive breaking glas sound).
  • All tests run well or some test failed sounds for Unit tests.
  • Hitting a breakpoint sound (a pling comes up when the debugger stops running at a breakpoint)
  • Playing a little sound on syntax errors inside the editor, when a red scribbel line appears.
  • ...and so on...

I know from my own experiences, that supporting sound effects inside a developer IDE can be very valuable and helpful in addition to the common visual effects. Since you don't get only just those visual indication effect, but also sounding ones too. - Ideally an IDE should then also be customizable by the user to enable/disbale sound effects and if sound support is generally enabled, also to allow the user to change certain sounds to those he prefers/likes.

ToddW | Mon, 2007-11-19 12:10

I've added this as an enhancement request, see here:
http://bugs.activestate.com/show_bug.cgi?id=73479

This would be a great idea to add into the Open Komodo project.

vkyr | Thu, 2007-11-22 15:47

Let's see, if we then maybe will soon get sound support for Komodo events and actions. I believe it will be a valuable feature addition for the ActiveState Komodo product line. - It's actually something only very few developer tools and products really offer, even we are living in the so called computer multimedia century.

ToddW | Tue, 2008-05-27 08:16

Vkyr, we have moved all of the extensions to a new home at:
http://community.activestate.com/addons

If you can, please create a new entry there (Create content->Komodo Extension) for your extension, so that it can be easily found/searched. Also, you'll need to update the maxVersion info in the install.rdf for the latest versions of Komodo (i.e. <em:maxVersion>4.*</em:maxVersion>).

If you don't have the time I can move it and update it on your behalf, just let me know.

Cheers,
Todd