Comma detection broken on foreign keyboards
Hi,
The way tag-it detects a comma keypress does not work on some keyboard layouts. For example, in the Hebrew keyboard layout the comma is on a different key, and where the comma usually is - there's the letter Tav. So when you use tag-it with such a keyboard, each time you try to input a word with Tav in it, the tag is closed instead.
I've implemented a very simple fix, which uses event.charCode to detect the comma instead of relying on $.ui.keyCode.COMMA Handling the comma then has to be moved to the keypress event instead of keydown, because event.charCode is not available on keydown. See below:
// Events.
this._tagInput
.keydown(function(event) {
// Backspace is not detected within a keypress, so it must use keydown.
if (event.which == $.ui.keyCode.BACKSPACE && that._tagInput.val() === '') {
var tag = that._lastTag();
if (!that.options.removeConfirmation || tag.hasClass('remove')) {
// When backspace is pressed, the last tag is deleted.
that.removeTag(tag);
} else if (that.options.removeConfirmation) {
tag.addClass('remove ui-state-highlight');
}
} else if (that.options.removeConfirmation) {
that._lastTag().removeClass('remove ui-state-highlight');
}
// Space/Enter are valid delimiters for new tags,
// except when there is an open quote or if setting allowSpaces = true.
// Tab will also create a tag, unless the tag input is empty, in which case it isn't caught.
if (
event.which == $.ui.keyCode.ENTER ||
(
event.which == $.ui.keyCode.TAB &&
that._tagInput.val() !== ''
) ||
(
event.which == $.ui.keyCode.SPACE &&
that.options.allowSpaces !== true &&
(
$.trim(that._tagInput.val()).replace( /^s*/, '' ).charAt(0) != '"' ||
(
$.trim(that._tagInput.val()).charAt(0) == '"' &&
$.trim(that._tagInput.val()).charAt($.trim(that._tagInput.val()).length - 1) == '"' &&
$.trim(that._tagInput.val()).length - 1 !== 0
)
)
)
) {
event.preventDefault();
that.createTag(that._cleanedInput());
// The autocomplete doesn't close automatically when TAB is pressed.
// So let's ensure that it closes.
that._tagInput.autocomplete('close');
}
}).keypress(function(event){
// Comma character (has to be checked on keypress, because charCode is 0 in keydown event)
if (event.charCode == 44) {
event.preventDefault();
that.createTag(that._cleanedInput());
}
}).blur(function(e){
// Create a tag when the element loses focus (unless it's empty).
that.createTag(that._cleanedInput());
});
Same problem with Russian "б". I need to try your fix.
Thx, ishirav, had the same probles. U saved my time!
I think this fix should be applied in the next release. Facing the same problem too.
Thank you very much! works awesome ! russian б types like a charm :)
Thanks !
thanks works. after two years and i agree with @PanJ this should get into next release
Hi, I have an issue that in android device when I press space key it does not make tag. but it works fine desktop.