From 4b1204c5dedf1fe321b3be926a1fb1353a2bfd54 Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Thu, 13 Sep 2012 16:38:20 -0700 Subject: [PATCH] improve keyboard handling. fixes #389 --- select2.js | 45 +++++++-------------------------------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/select2.js b/select2.js index 6c9be910..44f3b8d3 100644 --- a/select2.js +++ b/select2.js @@ -1537,21 +1537,13 @@ selection.bind("keydown", this.bind(function(e) { if (!this.enabled) return; - if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) { - // prevent the page from scrolling + if (e.which == KEY.DOWN || e.which == KEY.UP + || (e.which == KEY.ENTER && this.opts.openOnEnter)) { + this.open(); killEvent(e); 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 (this.opts.allowClear) { this.clear(); @@ -1559,34 +1551,11 @@ killEvent(e); return; } - + })); + selection.bind("keypress", this.bind(function(e) { + var key = String.fromCharCode(e.which); + this.search.val(key); 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();