-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 06/01/2010, at 10:36 AM, Mark Gibson wrote:
I quite agree that this should remain a plugin.
It seems to me that you're discussing a Rolls-Royce key event handler,
when what's needed is a simple bicycle. Key events are very different
in different browsers, and what the jQuery core needs is the simplest
possible way to bridge and hide the differences.
I recently spent three days researching the differences before coming
up with a solution I was happy with for my purposes, even though I
still don't implement the new DOM3 textInput event (for which there
seems to be no bind() support in jQuery?!).
jQuery in fact actively makes the cross-browser problem *worse* by
populating 'which', so the only way to tell left-arrow from single-quote
is to look at the originalEvent.
Surely, before discussing a super-duper syntax for matching key events,
jQuery should just solve the cross-browser problem as simply as can be,
instead of making things worse?
In my case, I implemented a key event dispatcher (on my Raphael object)
that merges text input with special keys, handling auto-repeat
properly for
both, and calling the "handleKey" method of whatever object the
keyboardFocus variable is currently pointing to. In a generalised jQuery
implementation, this dispatcher would act on the whole document instead
of the Raphael object, and would be properly plugin-ized, but the
required
logic is basically what follows here ('paper' is the Raphael canvas).
The code below implements (what I consider to be) the correct logic,
and is free for any jQuery plugin author to rip off in any way you like.
Clifford Heath, Data Constellation, http://dataconstellation.com
Agile Information Management and Design.
- --------------------------------- Cut Here
- ---------------------------------
/* Keyboard dispatcher */
paper.keyboardFocus = paper;
paper.handleKey = function(k, e) {
// This needs fleshing out to allow browser key shortcuts to work
as and where needed
if (k.code == Key.BACKSPACE || k.code == Key.DELETE) {
return false; // Don't propagate key - it make some browsers go
Back
} else {
return true; // Allow event to propagate
}
};
paper.takeFocus = function(obj) {
paper.keyboardFocus = obj;
};
paper.revertFocus = function() {
paper.keyboardFocus = paper;
};
/*
* An object that wants keyboard focus should call paper.takeFocus
passing an
* object that provides a handleKey method, and calling revertFocus
when done.
* At some point, a focus stack might be needed.
*
* A handleKey method gets passed a key object (as well as the event)
which is either:
* { code : the special key code } OR
* { str : the string representing a printable character }
*
* handleKey should return true to allow the event to bubble, false
to stop it.
*/
/* Record the key code from a keydown event to use on auto-repeat and
on keyup if no keypress follows */
var downKey;
/* Handle special keys using keydown; these browsers don't send
keypress for these events */
if ($.browser.msie || $.browser.safari) {
$(paper.canvas.node).bind('keydown', function(e) {
if (downKey && downKey == e.keyCode) // Auto-repeat
paper.keyboardFocus.handleKey({code: downKey}, e);
downKey = e.keyCode;
return true;
});
$(paper.canvas.node).bind('keyup', function(e) {
if (downKey) // There was no keypress in between; must be a
special key
paper.keyboardFocus.handleKey({code: downKey}, e);
downKey = null;
return true;
});
}
if (false && jQuery.browser.safari) {
$(paper.canvas.node).bind('textInput',function(e) {
// REVISIT: neither Raphael nor jQuery handle the new DOM3
textInput event type yet
// Write some sensible code here and remove false from the above
'if' condition.
});
} else {
$(paper.canvas.node).bind('keypress',function(e) {
var charCode = e.charCode || e.which;
downKey = null;
if ($.browser.opera) {
if (!e.originalEvent.which) // Without this, left-arrow
and single-quote are identical
charCode = null;
e.ctrlKey = e.metaKey; // Opera also sets metaKey
instead of ctrlKey :-(
} else if ($.browser.msie)
charCode = e.which;
else
charCode = e.charCode;
if (charCode && !e.metaKey && !e.altKey && !e.ctrlKey && charCode
>= 32)
return paper.keyboardFocus.handleKey({str:
String.fromCharCode(charCode)}, e);
else // control key on MSIE, or any special key on Firefox, etc
return paper.keyboardFocus.handleKey({code: e.keyCode}, e);
});
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Darwin)
iEYEARECAAYFAktD1g0ACgkQROuaL1tmAEZhhQCfRAz8GuAey5AXB+6ooZydcA5S
rMMAniQ6AeCzufMMFMpSUgOu/3o7zAxR
=2y99
-----END PGP SIGNATURE-----
--
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to jquery-...@googlegroups.com.
To unsubscribe from this group, send email to
jquery-dev+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en.