mozilla key events

these came out of my experimentations in (currently unsuccessfully) recreating the dhtml key bindings of blogger for mozilla. there are a number of open bugs on keyevent handling. please contact me if you have any commments or additions.

> example file

first, with mozilla events, if you're doing anything that requires accessing the event object, ie key captures, forget using the html attribute handlers. it doesn't work. "onkeypress=" will not automatically generate an event that you can access the keyevent properties from.

UPDATE: just found out you can use HTML attribute events, if you manually pass "event" to the target function. see: bugzilla attachment 18807. (this explains so much...)

also, if you're dealing w/ keyevents, you can forget about looking in the dom2 docs because it ain't there (dom3 draft covers it, but this diverges from mozilla's implementation). danny goodman's article on iplanet will give you a quick rundown on event listeners that is helpful (if you're unfamiliar). also, the other thing that will come in handy is the mozilla source (/mozilla/dom/public/idl/events/MouseKeyEvent.idl is what you're looking for - i put a recent copy online for your convenience). also, the javascript console is your friend.

continuing on, what you have to do is create your element, assign it an id, and then create a listener for that element. try:

document.getElementById("id").addEventListener("event", function, capture_boolean)

id is the target element that you want to listen to. event in the case of key captures is the string "keypress" (that was annoying to find out), function is the name of what target function you are calling (no parentheses after), and the capture_boolean is true or false, determining whether the event is captured or not.

the target function will now be automatically passed an event object you can use, your function header should assign the event to a variable to use, ie

function targetfunc(evt)

note, there are some problems: the capture boolean, even when set to true will NOT prevent the event from bubbling up to the xul interface, you need to use the keyevent.stopPropagation() method. also, if you trigger an alert, the target loses focus. this is normal behavior, but gave me some trouble, so i thought i might point it out. you can fix this with a element.focus() call pretty easily. also, and this is pretty important, the keyCodes in my build of mozilla return "0" for all my character keys (bug 58709 - this is super annoying because you need to look at evt.charCode or evt.which for alphanumerics, but evt.keyCode for non-alphanumerics. sheeet.). As a result, I use the charCodes w/ an "or" to account for caps locks on and off.

working w/ text selections

apparently, window.getSelection() isn't working. looks like my experimenation stops here for now. bug 48575


------- Additional Comments From  2000-12-16 19:13 -------
lhl: window.getSelection is for the selection in the non-form areas of the
window. You can actually have two simultaneous selections inside a form's
textfield and within the non-form content. That's just the way it works - for
whatever that's worth.

Use seltext=selnode.value.substr(target.selectionStart,target.selectionEnd);

However this is not working for textarea (multiline textfields) - see bug
#58850. We expect that to be fixed soon. Email me directly if you need any more
info. --

so, now i'm waiting on bug 58850