javascript - How to detect non-visible keys (ENTER, F1, SHIFT) altogether using JS or jQuery? - Stack Overflow

First of all Sorry, I don't know what to call those keys (ENTER, F1, HOME, etc).Actually, I'm

First of all Sorry, I don't know what to call those keys (ENTER, F1, HOME, etc).

Actually, I'm creating an input search box which onkeyup calls a function. When the user has input at least two keys my function is called and relevant search results are displayed using AJAX. The problem is when the user presses arrow key, HOME, END, etc then also my ajax is called which I don't want. And pressing F5 key to reload the page when focused on the input don't reloads the page, instead calls the AJAX, that's why it's a big issue for me.

$('input[name=\'search\']').on(keyup, function(e) {
  if ($('input[name=\'search\']').val().length >= 2) {
    // call ajax to display results..
  }
});

I want to add an extra expression in the if, that checks if the non-visible key is pressed or not. Like -

if ($('input[name=\'search\']').val().length >= 2 && (EXPRESSION FOR VISIBLE KEYS)) {
  // call ajax to display results..
}

If any visible key is pressed then ajax is called, else its not.

I don't know how to achieve this. As I cannot do e.keycode == '65' for each keys like A,B,C,\,=,+,1,2,3,etc

Is there a ready made library to check this or any other way to do this? Please help me.

First of all Sorry, I don't know what to call those keys (ENTER, F1, HOME, etc).

Actually, I'm creating an input search box which onkeyup calls a function. When the user has input at least two keys my function is called and relevant search results are displayed using AJAX. The problem is when the user presses arrow key, HOME, END, etc then also my ajax is called which I don't want. And pressing F5 key to reload the page when focused on the input don't reloads the page, instead calls the AJAX, that's why it's a big issue for me.

$('input[name=\'search\']').on(keyup, function(e) {
  if ($('input[name=\'search\']').val().length >= 2) {
    // call ajax to display results..
  }
});

I want to add an extra expression in the if, that checks if the non-visible key is pressed or not. Like -

if ($('input[name=\'search\']').val().length >= 2 && (EXPRESSION FOR VISIBLE KEYS)) {
  // call ajax to display results..
}

If any visible key is pressed then ajax is called, else its not.

I don't know how to achieve this. As I cannot do e.keycode == '65' for each keys like A,B,C,\,=,+,1,2,3,etc

Is there a ready made library to check this or any other way to do this? Please help me.

Share Improve this question edited Jul 12, 2015 at 19:39 Rohit Kumar asked Jul 7, 2015 at 18:37 Rohit KumarRohit Kumar 2,0311 gold badge15 silver badges30 bronze badges 1
  • window.event.shiftKey ? stackoverflow./questions/6178431/… – trainoasis Commented Jul 7, 2015 at 18:40
Add a ment  | 

4 Answers 4

Reset to default 3

You can check if e.which or e.keyCode is between 48 and 90, which corresponds to any number or letter of the alphabet. http://www.cambiaresearch./articles/15/javascript-char-codes-key-codes

if (+e.which >= 48 && +e.which <=90){ ... }

If you want to support other ones that arnt just alphanumeric, the easiest way i can see is to create an array of all the keycodes you want to whitelist, and check if the array contains the keycode. this is a good thread on how to do that: How to find if an array contains a specific string in JavaScript/jQuery?

Event keyCode is your answer:

$('input[name=\'search\']').keyup(function(event) {
   console.log( event.keyCode );
});

For example the keyCode for Enter key is 13. Also you can check Ctrl, Shift and Alt keys by the following variables:

if(event.ctrlKey) {
      if (event.ctrlLeft) {
        console.log('ctrl-left'); 
      }
      else {
        console.log('ctrl-right');
      }
}
event.altKey
...
event.altLeft
...
event.shiftKey
...

For the accepting only letters, you may use this way:

var letter = String.fromCharCode(e.which);
if (/[a-z\d]/i.test(letter)) {
    // send ajax call
}

I have got another solution to check whether a visible character key was pressed or not. Actually the basic thing I used in my code is that the visible character keys when pressed will change the length of the value of input field. I used keydown instead of keyup to get length of the string just before the current key is pressed. Have a look to my code -

$('input[name=\'search\']').on('keydown', function() {
      var orig_len = $('input[name=\'search\']').val().length; // this gets the original length just before the key was pressed
      setTimeout(function() {
          if ($('input[name=\'search\']').val().length >= 2 && $('input[name=\'search\']').val().length != orig_len) {
            // my ajax call
          }, 25);
        /* in 25ms the lenght is updated and the if checks
        whether length has changed from previous or not
        because visible char key changes length
        */
      });

The setTimeout was necessary because this 25ms elapsed time is more than enough to update the lenght of the input field after the current key is pressed.

Please have a discussion on this and tell me it is okay or not or have some bad impact or can cause issue on different browsers as I have not checked it yet on other browsers.

UPDATE: Its fully functional on all major browsers. Its working as I wanted it to be.

Another answer is a good JavaScript ponent to implement an advanced search box, like TypeAhead ponent, easy peasy ;)

Please have a look at : https://twitter.github.io/typeahead.js/examples/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745617956a4636347.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信