I have been lucky to find String.fromCharCode()
. It has helped me significantly. However, I noticed it doesn't take into account the shift modifier. I know the event.shiftKey
property and use it, but right now I need to get the key value (eg: "A" or "a") which takes into account the shift key modifier. At first I used String.toLowerCase()
.. But I neglected to think of number keys, to give one example: "5" (without shift) and "%" (with shift). I need to differentiate between the two in my program. What's an easy way to get the key char value?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
This brute force technique isn't the prettiest, but it seems to work. I'm in search of the same functionality.
Expanding on wsams response, here is a more complete solution which takes more memory but is more performant
It all depends on the event you are binding to.
String.fromCharCode()
will account forshiftKey
during theonkeypress
event. It will not, however, pay attention to it during theonkeydown
event.