19

I have a function which detect max length. but the problem is that when the max length reached Ctrl+A combination does't work. How can I detect Ctrl+A combination through javascript.

This is my maxlength code.

if (event.keyCode==8 || event.keyCode==9 || event.keyCode==37 || event.keyCode==39 ){
        return true;
} else {            
        if((t.length)>=50) {    
            return false;
        }   
}
3
  • This question has already been answered stackoverflow.com/questions/2903991/… Commented Nov 24, 2012 at 8:13
  • Have you looked at any doco for the keyboard event object? You can test the event.ctrlKey property to see if the ctrl key was down when the key event was generated, and also test for the keyCode corresponding to the A key. Commented Nov 24, 2012 at 9:08
  • Open jQuery source. See how they have implemented it. This may help Commented Nov 24, 2012 at 10:50

5 Answers 5

37

Check event.ctrlKey:

function keyHandler(event) {
    event = event || window.event;
    if(event.keyCode==65 && event.ctrlKey) {
        // ctrl+a was typed.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

key codes:

shift   16
ctrl    17
alt     18

your jQuery:

$(document).keydown(function (e) {
    if (e.keyCode == 18) {
        alert("ALT was pressed");
    }
});

JavaScript Madness: Keyboard Events

Comments

1

You can use the following:

document.onkeypress = function(evt) {
  evt = evt || window.event;
  etv = evt;
  switch (etv.keyCode) {
    case 16:
      // Code to do when Shift presed
      console.log('Pressed [SHIFT]');
      break;
    case 17:
      // Code to do when CTRL presed
      console.log('Pressed [CTRL]');
      break;
    case 32:
      // Code to do when ALT presed
      console.log('Pressed [ALT]');
      break;
  }
};

Comments

0

I needed a solution for this too, so found some stuff that worked, cleaned it up to be a lot less code, and ES6... JSFiddle link

function isCapsLock(event=window.event) {
  const code = event.charCode || event.keyCode;

  if (code > 64 && code < 91 && !event.shiftKey) {
    return true;
  }

  return false;
}

document.getElementById("text").addEventListener("keypress", event => {
  const status = document.getElementById("status");
  if (isCapsLock(event)) {
    status.innerHTML = "CapsLocks enabled";
    status.style.color = "red";
  } else {
    status.innerHTML = "CapsLocks disabled";
    status.style.color = "blue";
  }
}, false);
<input type="text" id="text" /><br>
<span id="status"></span>

Comments

0

This is a very old question. gilly3's answer is valid only if we have at hand an event object of type KeyboardEvent passed as a function argument. How to detect the current control key state if we have not event object available such as in this function?

function testModifierKey() {
  // have I some modifier key hold down at this running time?
}

I found the solution after a long search from https://gist.github.com/spikebrehm/3747378 of spikebrehm. his solution is tracing the modifier key state at any time using jQuery with a global variable.

The global variable window.modifierKey can be used in any circonstance without requiring event object.

function testModifierKey() {
  // have I have some modifier key hold down at this executing time?
  if(window.modifierKey) {
    console.log("Some modifier key among shift, ctrl, alt key is currently down.");
    // do something at this condition... for example, delete item without confirmation.
  } else {
    console.log("No modifier key is currently down.");
    // do something at other condition... for example, delete this item from shopping cart with confirmation.
  }
}

Here is his script to load in your HTML document:

// source: https://gist.github.com/spikebrehm/3747378
// modifierKey used to check if cmd+click, shift+click, etc. 
!function($, global){
  var $doc = $(document);
  var keys;

  global.modifierKey = false;

   global.keys = keys = {
      'UP': 38,
      'DOWN': 40,
      'LEFT': 37,
      'RIGHT': 39,
      'RETURN': 13,
      'ESCAPE': 27,
      'BACKSPACE': 8,
      'SPACE': 32
  };

  // borrowed from Galleria.js
  var keyboard = {
    map: {},
    bound: false,

    press: function(e) {
      var key = e.keyCode || e.which;
      if ( key in keyboard.map && typeof keyboard.map[key] === 'function' ) {
        keyboard.map[key].call(self, e);
      }
    },

    attach: function(map){
      var key, up;

      for(key in map) {
        if (map.hasOwnProperty(key)) {
          up = key.toUpperCase();
          if (up in keyboard.keys) {
            keyboard.map[keyboard.keys[up]] = map[key];
          } else {
            keyboard.map[up] = map[key];
          }
        }
      }
      if (!keyboard.bound) {
        keyboard.bound = true;
        $doc.bind('keydown', keyboard.press);
      }
    },

    detach: function() {
      keyboard.bound = false;
      keyboard.map = {};
      $doc.unbind('keydown', keyboard.press);
    }
  };

  $doc.keydown(function(e) {
    var key = e.keyCode || e.which;
    if (key === 16 || key === 91 || key === 18 || key === 17) {
      modifierKey = true;
    } else {
      modifierKey = false;
    }
  });

  $doc.keyup(function(e) {
    modifierKey = false;
  });
}(jQuery, window);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.