1
0
mirror of synced 2025-02-04 14:19:23 +03:00

improve keyboard handling. fixes #389

This commit is contained in:
Igor Vaynberg 2012-09-13 16:38:20 -07:00
parent 908c3583d7
commit 4b1204c5de

View File

@ -1537,21 +1537,13 @@
selection.bind("keydown", this.bind(function(e) { selection.bind("keydown", this.bind(function(e) {
if (!this.enabled) return; if (!this.enabled) return;
if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) { if (e.which == KEY.DOWN || e.which == KEY.UP
// prevent the page from scrolling || (e.which == KEY.ENTER && this.opts.openOnEnter)) {
this.open();
killEvent(e); killEvent(e);
return; return;
} }
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e)
|| e.which === KEY.ESC) {
return;
}
if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
return;
}
if (e.which == KEY.DELETE || e.which == KEY.BACKSPACE) { if (e.which == KEY.DELETE || e.which == KEY.BACKSPACE) {
if (this.opts.allowClear) { if (this.opts.allowClear) {
this.clear(); this.clear();
@ -1559,34 +1551,11 @@
killEvent(e); killEvent(e);
return; return;
} }
}));
selection.bind("keypress", this.bind(function(e) {
var key = String.fromCharCode(e.which);
this.search.val(key);
this.open(); this.open();
if (e.which === KEY.ENTER) {
// do not propagate the event otherwise we open, and propagate enter which closes
killEvent(e);
return;
}
// do not set the search input value for non-alpha-numeric keys
// otherwise pressing down results in a '(' being set in the search field
if (e.which < 48 ) { // '0' == 48
killEvent(e);
return;
}
var keyWritten = String.fromCharCode(e.which).toLowerCase();
if (e.shiftKey) {
keyWritten = keyWritten.toUpperCase();
}
// focus the field before calling val so the cursor ends up after the value instead of before
this.search.focus();
this.search.val(keyWritten);
// prevent event propagation so it doesnt replay on the now focussed search field and result in double key entry
killEvent(e);
})); }));
this.setPlaceholder(); this.setPlaceholder();