Mottie/Keyboard

autocomplete messes up the caret position in IE9

banku opened this issue · 2 comments

I am using the latest version of the keyboard (Version 1.12) and autocomplete extension (v1.3).

Steps to reproduce bug: Have a textbox attached to keyboard with autocomplete plug in. Set focus on the textbox and add one or two characters using the virtual keyboard so that the autocomplete suggestions show up. Select an autocomplete selection.

Result:The selection shows up on the target textbox and the caret moves to the end which is all good.

Bug: After this, when you move the mouse to a key on the virtual keyboard and then move the mouse out of it, the caret position moves back to it's original position. For e.g. if two characters were entered before using autocomplete, the caret position will go back to the position after 2 characters.

Expected result: Caret position should stay at the end.

I know that this happens when the below code is executed:
.bind('mouseup.keyboard mouseleave.kb touchend.kb touchmove.kb touchcancel.kb', function(){
if (base.isVisible() && base.isCurrent()) { base.$preview.focus(); }
base.mouseRepeat = [false,''];
clearTimeout(base.repeater); // make sure key repeat stops!
if (base.checkCaret) { base.$preview.caret( base.lastCaret.start, base.lastCaret.end ); }
return false;
})

So I guess that the fix should be setting base.lastCaret properly when the textbox is updated using autocomplete selection. Does that make sense?

Adding the line[base.lastCaret = { start: 1, end: ui.item.value.length };] in the jquery.keyboard.extension-autocomplete.js fixes the issue. Here is the code where this line needs to be put in.

.bind('autocompleteselect', function(e,ui){
                    if (base.hasAutocomplete && ui.item.value !== ''){
                        base.$preview
                        .val( ui.item.value )
                        .focus();

                    base.lastCaret = { start: 1, end: ui.item.value.length };
                }
            });

Thanks for isolating the problem! I'll include it in the next update :)