Search code examples
jquerykeyup

jQuery keyup get all the characters pressed at a time


How can I get all the character pressed at a time? I have a timer of 500 ms and I want to get all the characters pressed within. Lets say I have pressed "asd" very fast, then I want to use "asd" as a search string, not just "d". Here is my idea:

    var mytimer;
    $filter_container.on('keyup',function(key){
        // getting key from ascii
        var mykey = get_key_from_ascii(key);

        clearTimeout(mytimer);
        var $item = $(this);
        var $item2 = this;
        var ms = 500; // milliseconds
        mytimer = setTimeout(function() {
            // here i want to send the whole character set to search
            my_auto_complete($item, key, $item2);
        }, ms);
    });

Solution

  • try this:

    var mytimer;
    var collectedKeys = [];
    
    $filter_container.on('keyup',function(key){
        // getting key from ascii
        var mykey = get_key_from_ascii(key);
    
        // push key to array of keys
        collectedKeys.push(mykey);
    
        my_auto_complete($item, collectedKeys, $item2);
    
    });
    
    // after 500 ms, clear keys array
    mytimer = setTimeout(function() {
            collectedKeys = [];
        }, 500);