Jump to content

How do you obtain the key value in OnKeyPress handler?


boylesg

Recommended Posts

How is the function being called? If it's through an attribute on the element then it won't work. The event handler has to be assigned through code:

element.addEventListener("keypress", doOnKeyPress, false);// For IE 8 and underelement.attachEvent("onkeypress", doOnKeyPress)
Link to comment
Share on other sites

to find the key that is pressed use something like this:

var key = event.key || event.keyCode || event.which;alert(key); 

event.key seems to be the upcoming standard way of getting the key, but hasn't been implemented as of yet. keyCode and which are the current recommendations of grabbing the character, yet they are slated to be depreciated and won't be recommended soon. the code I show here "should" give you the right property regardless of version or platform.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...