0

When non-printable char is pressed, it's replaced with let's say for CTRL=17 with "[CTRL]". Here is code an example

$('#textbox1').keyup(function (event) {
    if (8 != event.keyCode) {
       if(17==event.keyCode){
        $('#textbox1').val($('#textbox1').val()+"[CTRL]")
        $('#textbox2').val($('#textbox1').val());
       }else{
        $('#textbox2').val($('#textbox1').val());
       }

    } else {

        $('#textbox2').val($('#textbox1').val());
    }
});

the problem is when user presses backspace the second input must reflect the content of the first one, so "[CTRL]" must be deleted at once like any other chars.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
elsadek
  • 1,028
  • 12
  • 39
  • don't you read the title ? non-printable chars are not supported with .val() – elsadek May 06 '14 at 18:53
  • Is this linear, meaning if the user presses backspace if deletes the last inputted character, or can they click around to different parts of the text box and delete characters at any place? – Gavin Jun 04 '14 at 06:55
  • @Gavin The point is capturing non-printable chars and handle them like other printable chars specially when backspace – elsadek Jun 04 '14 at 06:59
  • You need to keep a list containing the pressed key codes, otherwise it's impossible to decide whether "[Ctrl]" stands there because the user pressed Ctrl or because he typed it letter by letter. – Ingo Bürk Jun 04 '14 at 07:00
  • @elsadek, so what you mean is if your first text box contains "abcdefghijk" and the 2nd text box contains "abc[CTRL]defg[ALT]hijk" then the user can click in between "c" and "d" in the first text box and if they press backspace and it will remove [CTRL]? Or can they only "undo" the last character they entered when they press backspace? – Gavin Jun 04 '14 at 07:30
  • @Gavin the first case wouldn't be handy for the end user but the second is the closest one to my use case – elsadek Jun 04 '14 at 07:40
  • Ok, and is it just one-level of undo, or can you undo as far back as the user has started typing? – Gavin Jun 04 '14 at 07:54
  • @Gavin user may undo many times. – elsadek Jun 04 '14 at 07:57

3 Answers3

1

You can check in the keydown for the last character in the input field. If it's a ] you can remove everything from the right to the last found opening bracket [. Unfortunatly this does not work if you're cursor is inside '[ ]'.

$('#textbox1').keydown(function(event) {
    if(8 == event.keyCode) {
        var element = $(this),
            value = element.val(),
            lastChar = value.slice(-1);

        if(lastChar == ']') {
            var lastIndex = value.lastIndexOf('['),
                index = value.length - lastIndex;

            element.val(value.slice(0, -index) + "]");
        }
    }
});

Fiddle

Dieterg
  • 16,118
  • 3
  • 30
  • 49
1

You could make use of the keyCode and/or in combination with charCode (if required). Basic idea would be:

  1. Create a map of all required key codes in an array/object
  2. Handle event for say keydown and listen for keycode
  3. Look for the keycode in your map and if found show it
  4. prevent the default (to prevent e.g. say backspace browsing back)
  5. If not found in map, let the character go thru as usual.

A very basic example:

Demo: http://jsfiddle.net/abhitalks/L7nhZ/

Relevant js:

keyMap = {8:"[Backspace]",9:"[Tab]",13:"[Enter]",16:"[Shift]",17:"[Ctrl]",18:"[Alt]",19:"[Break]",20:"[Caps Lock]",27:"[Esc]",32:"[Space]",33:"[Page Up]",34:"[Page Down]",35:"[End]",36:"[Home]",37:"[Left]",38:"[Up]",39:"[Right]",40:"[Down]",45:"[Insert]",46:"[Delete]"};

$("#txt").on("keydown", function(e) {

    // check if the keycode is in the map that what you want
    if (typeof(keyMap[e.keyCode]) !== 'undefined') {

        // if found add the corresponding description to the existing text 
        this.value += keyMap[e.keyCode];

        // prevent the default behavior
        e.preventDefault();
    }

    // if not found, let the entered character go thru as is
});

Edit: (as per the comments)

The concept remains the same, just copying the value to the second input:

Demo 2: http://jsfiddle.net/abhitalks/L7nhZ/3/

$("#txt1").on("keyup", function(e) {
    if (typeof(keyMap[e.keyCode]) !== 'undefined') {
        this.value += keyMap[e.keyCode];
        e.preventDefault();
    }
    $("#txt2").val(this.value); // copy the value to the second input
});

Regarding deletion of the description, I could not get it done by caching the last inserted descrition from the map. Somehow, I kept struggling with the regex with a variable. Anyway, a simpler solution is to just add another event handler for keyup with hard-coded map.

Thanks to @serakfalcon for (that simple solution), which we are using here:

$('#txt1').keydown(function(event) {
    if(8 == event.keyCode) {
        var el = $(this);
        el.val(el.val().replace(/\[(Tab|Enter|Shift|Ctrl|Alt|Break|Caps Lock|Esc|Space|Page (Up|Down)|End|Home|Left|Up|Right|Down|Insert|Delete)\]$/,' '));
        $("#txt2").val(el.val());
    }
});
Community
  • 1
  • 1
Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • 2
    that's not what the OP is asking. they want to be able to delete `[CTRL]` in one go when they hit backspace. – serakfalcon Jun 04 '14 at 06:50
  • @abhitalks you are missing the important part of the question which is reflecting input value – elsadek Jun 04 '14 at 07:06
  • @elsadek: yes of course, i am working on that part right now. please give me a few minutes.. – Abhitalks Jun 04 '14 at 07:07
  • @abhitalks [fify](http://jsfiddle.net/L7nhZ/1/) (unless you want to separate manual entries of `[CTRL]` from ctrl keypresses) – serakfalcon Jun 04 '14 at 07:07
  • Thanks a lot @serakfalcon, i was trying to with a cache variable. – Abhitalks Jun 04 '14 at 07:09
  • Thanks @abhitalks, if I could make @@serakfalcon share the bounty with you, is it possible ? – elsadek Jun 04 '14 at 07:45
  • 1
    elsadek: Don't worry about the bounty :) You could just award it to @serakfalcon . After all it was he who helped complete the puzzle, and deserves it. In fact I had started answering before you put a bounty, got to know only after posting the answer. More important is the fact that you were able to solve your problem. Cheers! – Abhitalks Jun 04 '14 at 07:49
  • This still has the problem that a manually typed [Ctrl] will be treated as if the user pressed Ctrl. It might be worth trading this bug off to avoid a lot more complexity, though. – Ingo Bürk Jun 04 '14 at 07:56
1

you can always use a regex.

$('#textbox1').keydown(function(event) {
    if(8 == event.keyCode) {
        var el = $(this);
        el.val(el.val().replace(/\[(CTRL|ALT|SHIFT)\]$/,' '));
    }
});

fiddle

Edit: combined with abhitalks code

serakfalcon
  • 3,501
  • 1
  • 22
  • 33