tag-it icon indicating copy to clipboard operation
tag-it copied to clipboard

Comma detection broken on foreign keyboards

Open ishirav opened this issue 14 years ago • 7 comments

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());
                });

ishirav avatar Aug 23 '11 16:08 ishirav

Same problem with Russian "б". I need to try your fix.

weekens avatar May 06 '12 15:05 weekens

Thx, ishirav, had the same probles. U saved my time!

CrazyClicker avatar Jun 13 '12 12:06 CrazyClicker

I think this fix should be applied in the next release. Facing the same problem too.

PanJ avatar Jun 14 '12 08:06 PanJ

Thank you very much! works awesome ! russian б types like a charm :)

zmichael avatar Nov 07 '13 12:11 zmichael

Thanks !

artyil avatar Dec 30 '13 10:12 artyil

thanks works. after two years and i agree with @PanJ this should get into next release

oak-tree avatar Apr 22 '14 13:04 oak-tree

Hi, I have an issue that in android device when I press space key it does not make tag. but it works fine desktop.

yogeshmatta avatar Jul 28 '16 12:07 yogeshmatta