Hey Perrin,
That's what I thought. Class.Binds workes like a charm.
Thanks for the tip!

var KeyboardListener = new Class({
    Binds: ['keydownHandler'],
    logging: true,
    initialize: function() {
        this.startListening();
    },
    log: function() {
        if (this.logging) {
            if (window['console']) {
                console.log($A(arguments).join('\n'));
            }
            else {
                alert($A(arguments).join('\n'));
            }
        }
    },
    startListening: function() {
        this.log('start listening ...');
        window.addEvent('keydown', this.keydownHandler);
    },
    keydownHandler: function(e) {
        if (e.key == 'esc') {
            this.log(e.key, 'stop listening ...');
            window.removeEvent('keydown', this.keydownHandler);
        }
        else {
            this.log(e.key);
        }
    }
});

window.addEvent('domready', function() {
    new KeyboardListener();
});


On Mar 11, 5:55 pm, Perrin Perrin <[email protected]> wrote:
>   window.removeEvent('keydown', this.keydownHandler);
> This says remove this specific function if it is there.  However you added
> it like this -->
>
>   window.addEvent('keydown', this.keydownHandler.bindWithEvent(this));
> Most of the methods that mootools adds to Function 
> (http://www.mootools.net/docs/Native/Function) return a different function
> that does other things.  It is pretty much returning a function that will
> call your function.  That being said the function you added and the one you
> are trying to remove are not the same function.
>
> Try modifing it to this.
>   this.keydownEvent = this.keydownHandler.bindWithEvent(this);
>   window.addEvent('keydown', this.keydownEvent);
> ....Later....
>   window.removeEvent('keydown', this.keydownEvent);
>
> You might also want to take a look 
> athttp://www.mootools.net/docs_rc1/more/Class/Class.Binds
>
> --Perrin (CrypticSwarm)
>
> On Wed, Mar 11, 2009 at 8:23 AM, marlun78 <[email protected]> wrote:
>
> > Hey,
> > Can anyone explain to me why this super simple script ain't working? I
> > want to listen for key strokes until the user presses the Esc-button.
> > What am I doing wrong?
>
> > // Test Class
> > var KeyboardListener = new Class({
> >    logging: true,
> >    initialize: function() {
> >        this.startListening();
> >    },
> >    log: function() {
> >        if (this.logging) {
> >            if (window['console']) {
> >                console.log($A(arguments).join('\n'));
> >            }
> >            else {
> >                alert($A(arguments).join('\n'));
> >            }
> >        }
> >    },
> >    startListening: function() {
> >        this.log('start listening ...');
> >        window.addEvent('keydown', this.keydownHandler.bindWithEvent
> > (this));
> >    },
> >    keydownHandler: function(e) {
> >        if (e.key == 'esc') {
> >            this.log(e.key, 'stop listening ...');
> >            window.removeEvent('keydown', this.keydownHandler);
> >        }
> >        else {
> >            this.log(e.key);
> >        }
> >    }
> > });
>
> > // Implementation
> > window.addEvent('domready', function() {
> >    new KeyboardListener();
> > });
>
> > Thanks!
> > /Martin

Reply via email to