سؤال

How can I detect whether the user is selecting the text in the <input> box?

$('input[name=inputname]').keyup(function(e)
{
   if((e.keyCode!=37)&&(e.keyCode!=38)&&(e.keyCode!=39)&&(e.keyCode!=40)) // Check whether the user is navigating inside the box by pressing the arrows
   {
        call_the_function();
   }
});

Now when the user selects the text he/she typed by using Shift + Left Arrow or Shift + Right Arrow, the keyUp() function executes. How can I prevent this?

هل كانت مفيدة؟

المحلول 2

DEMO

$('input[name=inputname]').keyup(function (e) {
    if (event.shiftKey && ((e.keyCode >= 37) && (e.keyCode <= 40)))
    {
        return false;
    }else{
        console.log('hi'); //call function when shift+arrow keys not pressed together 
    }
});

or

DEMO

$('input[name=inputname]').keyup(function (e) {
    if (!(event.shiftKey && ((e.keyCode >= 37) && (e.keyCode <= 40)))) {
        console.log('hi');  //call function when shift+arrow keys not pressed together 
    }
});

نصائح أخرى

Try this:

$('input[name=inputname]').keyup(function(e)
{
   if((e.keyCode!=16)&&(e.keyCode!=37)
       ||(e.keyCode!=16)&&(e.keyCode!=38)
       ||(e.keyCode!=16)&&(e.keyCode!=39)
       ||(e.keyCode!=16)&&(e.keyCode!=40)) // Check whether the user is navigating inside the box by pressing the arrows
   {
        call_the_function();
   }
});

This should check for the SHIFT Key and either the left, right, up or down arrow.

what about using jquery select()

http://jsfiddle.net/kasperfish/xWEPh/

//http://api.jquery.com/select/
$( "#target" ).select(function() {
alert( "Handler for .select() called." );
});

you could easily disable the keyup events for selecting with the keyboard. This has to be done in the keyup event handler.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top