Sunday, February 8, 2009

dojo and the enter key

Attaching a listener for the enter key on a form can help a lot from a usability perspective. If your users are more familiar with tabbing through a forms using only keys, asking them to use the mouse just to submit the form can be a little irritating. dojo comes to the rescue with a simple dojoattachevent. All you need to do is add the a dojoAttachEvent to your template file:


dojoattachevent="onkeyup: checkForEnter"


Then, in your widget logic, all you need to do is:


checkForEnter: function(keyEvent) {
if(keyEvent.keyCode == 13) {
if(!dojo.isFF) {
if(this._validateForm()) {
this._createAccount();
}
}
}
}


Oddly enough, Firefox is able to recognize the enter key and submits the form all by itself.

dojo campus has quite a few keys defined so you can capture a lot of what your users are trying to achieve with your web application.

No comments:

Post a Comment