Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: matias s..
Added: Jun 27, 2018 1:52 PM
Views: 9
Tags: no tags
Detects if CapsLock is enable or not.
  1. var capsLockIsOnKeyDown = {shiftWasDownDuringLastChar: false,
  2.   capsLockIsOnKeyDown: function(event) {
  3.     console.log(event);
  4.     var eventWasShiftKeyDown = event.which === 16;
  5.     var capsLockIsOn = false;
  6.     var shifton = false;
  7.     if (event.shiftKey) {
  8.         shifton = event.shiftKey;
  9.     } else if (event.modifiers) {
  10.         shifton = !!(event.modifiers & 4);
  11.     }
  12.  
  13.     if (event.target.value.length > 0 && !eventWasShiftKeyDown) {
  14.       var lastChar = event.target.value[event.target.value.length-1];
  15.       var isAlpha = /^[a-zA-Z]/.test(lastChar);
  16.  
  17.       if (isAlpha) {
  18.         if (lastChar.toUpperCase() === lastChar && lastChar.toLowerCase() !== lastChar
  19.           && !event.shiftKey && !capsLockIsOnKeyDown.shiftWasDownDuringLastChar) {
  20.           capsLockIsOn =  true;
  21.         }
  22.       }
  23.     }
  24.     capsLockIsOnKeyDown.shiftWasDownDuringLastChar = shifton;
  25.     if (capsLockIsOn){
  26.       console.log('CapsLock Activado');
  27.     }else{
  28.       console.log('CapsLock Desactivado');
  29.     }
  30.     return capsLockIsOn;
  31.   }
  32. }
Comments disabled