Is there any way to determine in javascript whether shift key is pressed on mobile keyboard, and distinguish it from the caps lock(twice pressed shift key)
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Some Facts
First, let’s look at some facts about iOS keyboards I assume you already know:
shiftkey is always activatedCaps Lockmust be activated manually (I guess this is not used too widely)iPhone Shift Key Handling
I investigated a bit into this issue, and here’s what I found:
The
shiftKey triggers no key EventThere is no special iPhone Browser API to detect whether the shift key is pressed or not, except in an iOS App (duh)
The
keydown,keypress,keyupevent triggered by iOS look normal, except they do not indicate shiftKey usage, and apart from their timestamp and type cannot be distinguished.You cannot manually dispatch a Keyboard Event in iOS because of this issue,
keyCodeandwhichare readonly and always set to0. Retriggering a Keyboard event to get some indication about the shift key still being on is impossible.Actually, The iPhone treats the
shiftkey like some sort ofShort Term Caps Lockkey. The difference is that normally, you activate it once, and it deactivates automatically.What can be done
I assume you want to indicate on an input field whether the user should be careful about having Shift/Caps Lock pressed (a password field, for example). What I came up with is some sort of a workaround, but I think it’s better than nothing.
You can also test the jsfiddle here.
DOM Setup
Javascript
This checks wheter the user did enter capitalized input, and it assumes the user is using
caps lockif two capitalized letters where entered.As I said, better than nothing, but not a solution if you need to know the shift key is pressed without the user doing any input. That seems to be impossible right now.