7

I want to detect caps on on smart phones, they have a small arrow which is basically a capslock.

I tried searching for it, all I could is how to do it on a desktop i.e:

$('input').keypress(function(e) { 
  var s = String.fromCharCode( e.which ); 
  if ( s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey ) { 
    alert('caps is on');
  }
});

But this doesn't work on smartphones, how can I achieve that or is it even possible?

Braiam
  • 1
  • 11
  • 47
  • 78
Niket Malik
  • 1,075
  • 1
  • 14
  • 23
  • 1
    There's no way to detect caps lock on smart phones. Try the accepted answer to [this question](http://stackoverflow.com/questions/11436366/shiftkey-in-safari-on-ios) for a method which will not work 100% but will still be better than nothing. – Taylan Aydinli Nov 18 '13 at 07:27
  • 2
    Ok, but smartphones aren't limited to just IOS, anyway thanks :) – Niket Malik Nov 18 '13 at 10:20
  • I know but that solution will work with any smart phone :) It just looks for two consecutive inputs in upper case. – Taylan Aydinli Nov 18 '13 at 10:24
  • Yes but it defeats the purpose for the **caps on** warning.. – Niket Malik Nov 18 '13 at 10:34
  • 1
    I don't think I'm getting through here :) Checking for caps lock is *completely impossible*. This is just an alternative method which obviously is not ideal but unfortunately it's the next best thing. Either you stop complaining about this method 'defeating the purpose' and use it knowing it's the best you can get (for now), or you don't use it and get on with your life. – Taylan Aydinli Nov 18 '13 at 10:38
  • Yes I get that..thanks – Niket Malik Nov 19 '13 at 03:14
  • 1
    You can also just put a warning that says "case-sensitive" (assuming this is for a password). – Trojan Dec 27 '13 at 03:12

1 Answers1

0

It is not possible to detect capslock on all smartphones using HTML/JavaScript/jQuery. In fact, taking this further, it is worth mentioning that in fact it is not possible to reliably detect any specific keypresses for all smartphones using only HTML/JavaScript/jQuery. For example, the Google Chrome browser for Android (not necessarily the native Android browser) has a bug where it returns a keypress code of 0, no matter the key being pressed. Perhaps this will be resolved one day but it's been present since the first version, which is getting on for a couple of years.

Perhaps if you're targetting a specific collection of browsers/smart phones this is less of an issue for you. e.g. Sticking to stock iOS, Windows Phone 8 and Android browsers, but its really worth mentioning that there is no magic bullet solution that covers all of them.

If you're developing a hybrid application rather than a web site, such as with PhoneGap, you have the option to use a native plug-in to capture the keypresses and hand off to PhoneGap so you can consume in JavaScript/jQuery.

Neil Cresswell
  • 1,145
  • 8
  • 20
  • Yes agreed for chrome browser..in addition it does not detect `keyup` event for backspace but detects `keyup` for lonhpress backspace.. – Niket Malik Jan 13 '14 at 05:31