best practice - for a keylistener

C

carlos

I wrote a simple key listener to listen for key combinations. It is
working fine now, and since I have some time, I would like to get some
advice from the experts on how I should refactor, if I even should. I
read online about the classical OO approach and prototypical style. I
am leaning towards refactoring using the prototypical OO style. How
would you experts model a key listener class?
 
C

carlos

I wrote a simple key listener to listen for key combinations. It is
working fine now, and since I have some time, I would like to get some
advice from the experts on how I should refactor, if I even should. I
read online about the classical OO approach and prototypical style. I
am leaning towards refactoring using the prototypical OO style. How
would you experts model a key listener class?

OH.. and perhaps I should have explained how I have it now. I decided
to make it a singleton. Well.. At least I tried to model it that way
the best I could. Here is a quick example of it.

KeyListener.SECTION_1 = 77
KeyListener.SECTION_2 = 88

function KeyListener()
{
throw new Error('must be used as a singleton');
}

KeyListener.listen = function (elm,scope) {
....
}

KeyListener.stopListening (elm) {
...
}

KeyListener.setFocus() {

}
 
T

Thomas 'PointedEars' Lahn

You can do nothing else but "prototypal OO style", as the languages you are
dealing with here are OOPLs that use only prototype-based inheritance so
far. You could simulate class-based inheritance, but I would consider that
misuse of the language.

http://javascript.crockford.com/javascript.html

Not at all, see above.
OH.. and perhaps I should have explained how I have it now. I decided
to make it a singleton.

Singleton is a concept for class-based OOP. IOW, you don't have a singleton
yet and you can't have one.
[...]
KeyListener.SECTION_1 = 77
KeyListener.SECTION_2 = 88

That is a possibility, although it might be a good idea to make those
prototype properties as well, see below.
function KeyListener()
{
throw new Error('must be used as a singleton');

Exception handling (try..catch..finally, throw) unfortunately is not
universally supported in ECMAScript implementations. I suggest you write a
wrapper that uses a string argument to eval() to execute code that uses
newer keywords (the string somewhat hides the code from non-compliant
parsers), and try to implement a fallback with window.onerror.

See http://PointedEars.de/es-matrix for a list containing information about
which language feature is supported in which version of some common
implementations. The list is currently not exhaustive and is continuously
updated.

In this case, you will have to define a property of the created object that
tells whether something went wrong on construction. But as I said, the
concept of singleton is not applicable here.
}

KeyListener.listen = function (elm,scope) {
....
}

Probably not. If your goal is code reuse, then you would define the
listen() method as a prototype method, so that objects using KeyListener()
as their constructor would inherit it through the prototype chain:

KeyListener.prototype.listen = function(elm, scope)
{
// ....
};
KeyListener.stopListening (elm) {
...
}

KeyListener.setFocus() {

}

I'm afraid these are plain syntax errors, as your favorite error console
should have displayed (for debugging, I prefer Firebug for Firefox and MS
Script Debugger for IE).

I prefer to define prototype objects as follows:

KeyListener.prototype = {
constructor: KeyListener,

listen: function(...)
{
...
},

stopListening: function(elm)
{
...
},

setFocus: function()
{
...
}
};

See also the ECMAScript Support Matrix (URI above) for details about the
compatibility of object literals.


PointedEars
 
D

dhtml

Post code, or a link to it.

I would like to know about key events, too. There isn't any standard,
AFAIK.
You can do nothing else but "prototypal OO style", as the languages you are
dealing with here are OOPLs that use only prototype-based inheritance so
far. You could simulate class-based inheritance, but I would consider that
misuse of the language.

Writing an all-encompassing KeyEventAdapter would be a great way to
learn about key events in different environments.

The goal of the code - the problem it is trying to solve - and the
bugs and anomalies in browsers would affect the design (solving a
problem in a context). It might be the case that you need a function
that takes an event and returns "fixed" keyCode object. It might have
methods like isDeleteKey() for example.

Interesting:
http://qooxdoo.org/documentation/0.6/user_manual/keyboard_events

[...]
KeyListener.SECTION_1 = 77
KeyListener.SECTION_2 = 88

That is a possibility, although it might be a good idea to make those
prototype properties as well, see below.

For the invariants like ENTER, that might be a good idea.

The tricky part would be detecting DELETE, modifiers like altKey.

Exception handling (try..catch..finally, throw) unfortunately is not
universally supported in ECMAScript implementations. I suggest you write a
wrapper that uses a string argument to eval() to execute code that uses
newer keywords (the string somewhat hides the code from non-compliant
parsers), and try to implement a fallback with window.onerror.

try-catch works all the ver 5 browsers, doesn't it? JScript issues
with try-finally and error-related bugs (Error.prototype.name) can be
coded around w/o eval.

Garrett
 
T

Thomas 'PointedEars' Lahn

dhtml said:
You can do nothing else but "prototypal OO style", as the languages you are
dealing with here are OOPLs that use only prototype-based inheritance so
far. You could simulate class-based inheritance, but I would consider that
misuse of the language.

Writing an all-encompassing KeyEventAdapter would be a great way to
learn about key events in different environments. [...]

I would apreciate it if you referred to what you quote.
[...]
KeyListener.SECTION_1 = 77
KeyListener.SECTION_2 = 88
That is a possibility, although it might be a good idea to make those
prototype properties as well, see below.

For the invariants like ENTER, that might be a good idea.

The tricky part would be detecting DELETE, modifiers like altKey.

I don't follow.
try-catch works all the ver 5 browsers, doesn't it?

Depends. What do you consider a "ver 5 browser"? Your question should be
moot as I already posted the link to the compatibility information as I know it.
JScript issues with try-finally and error-related bugs (Error.prototype.name)

`Error' and other exception-related features were not introduced before
ECMAScript Edition 3.
can be coded around w/o eval.

Talk is cheap. Show me the code.
-- Linus Torvalds


PointedEars
 
D

dhtml

Writing an all-encompassing KeyEventAdapter would be a great way to
learn about key events in different environments. [...]

I would apreciate it if you referred to what you quote.

What does this mean? Maybe you mean: I quoted you, then didn't make
any reference to what you said. If that's what you're saying: I
provided contrast to what you said. Instead of thinking about the ecma
manual (what you did), I thought about the problem. That's a
contrasting viewpoint to approach the problem.
Depends. What do you consider a "ver 5 browser"? Your question should be
moot as I already posted the link to the compatibility information as I know it.

IE5+, Moz 1.0+, Safari 1+, Opera 5+.
Talk is cheap. Show me the code.
-- Linus Torvalds
It looks like a quote/order.

I don't really have to show you anything. In this case, the problem
that the OP presented - a keylistener/adapter - is not blocked by
problems with try-catch, nor any JScript anomalies WRT errors. SO
doing so would be a distraction.

The OP still hasn't posted up any code; I think that's a good first
step before rushing ahead and with solutions that seem unrelated.

BTW - Are we supposed to trim other peoples' signatures in post
replies?
 
T

Thomas 'PointedEars' Lahn

dhtml said:
dhtml said:
[...] Thomas 'PointedEars' Lahn [...] wrote:
You can do nothing else but "prototypal OO style", as the languages
you are dealing with here are OOPLs that use only prototype-based
inheritance so far. You could simulate class-based inheritance,
but I would consider that misuse of the language.
Writing an all-encompassing KeyEventAdapter would be a great way to
learn about key events in different environments. [...]
I would apreciate it if you referred to what you quote.

What does this mean? Maybe you mean: I quoted you, then didn't make any
reference to what you said.

Exactly. Very bad discussion style.
If that's what you're saying: I provided contrast to what you said.
Instead of thinking about the ecma manual (what you did), I thought about
the problem. That's a contrasting viewpoint to approach the problem.

JFYI: I have not debated at all that it was a good idea. There is no
contrast here, there is just a distraction on your part.
IE5+, Moz 1.0+, Safari 1+, Opera 5+.

I don't think Safari 1.0 or Opera 5.0 already supported exception handling.
It looks like a quote/order.

It is a quote and a request.
I don't really have to show you anything.

If you want to prove your statement instead of just venting hot air here,
you will have to.
In this case, the problem that the OP presented - a keylistener/adapter -
is not blocked by problems with try-catch, nor any JScript anomalies WRT
errors. SO doing so would be a distraction.
Nonsense.

The OP still hasn't posted up any code; I think that's a good first step
before rushing ahead and with solutions that seem unrelated.
Irrelevant.

BTW - Are we supposed to trim other peoples' signatures in post replies?

Yes, unless you refer to its content.


PointedEars
 
V

VK

dhtml said:
dhtml wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
You can do nothing else but "prototypal OO style", as the languages
you are dealing with here are OOPLs that use only prototype-based
inheritance so far. You could simulate class-based inheritance,
but I would consider that misuse of the language.
Writing an all-encompassing KeyEventAdapter would be a great way to
learn about key events in different environments. [...]
I would apreciate it if you referred to what you quote.
What does this mean? Maybe you mean: I quoted you, then didn't make any
reference to what you said.

Exactly. Very bad discussion style.
If that's what you're saying: I provided contrast to what you said.
Instead of thinking about the ecma manual (what you did), I thought about
the problem. That's a contrasting viewpoint to approach the problem.

JFYI: I have not debated at all that it was a good idea. There is no
contrast here, there is just a distraction on your part.
IE5+, Moz 1.0+, Safari 1+, Opera 5+.

I don't think Safari 1.0 or Opera 5.0 already supported exception handling.
It looks like a quote/order.

It is a quote and a request.
I don't really have to show you anything.

If you want to prove your statement instead of just venting hot air here,
you will have to.
In this case, the problem that the OP presented - a keylistener/adapter -
is not blocked by problems with try-catch, nor any JScript anomalies WRT
errors. SO doing so would be a distraction.
Nonsense.

The OP still hasn't posted up any code; I think that's a good first step
before rushing ahead and with solutions that seem unrelated.
Irrelevant.

BTW - Are we supposed to trim other peoples' signatures in post replies?

Yes, unless you refer to its content.

PointedEars


PointedEars is an air spoiler?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top