According to MDN article keypress
event is deprecated:
But I can't find any information elsewhere on whether we should use this event in a new projects. If we shouldn't, what is the replacement?
Could somebody give an insight?
Since the event is deprecated, you should avoid using it in new code, and plan on removing it from old code. The W3C specification says this about deprecated features:
Features marked as deprecated are included in the specification as reference to older implementations or specifications, but are OPTIONAL and discouraged. Only features which have existing or in-progress replacements MUST be deprecated in this specification. Implementations which do not already include support for the feature MAY implement deprecated features for reasons of backwards compatibility with existing content, but content authors creating content SHOULD NOT use deprecated features, unless there is no other way to solve a use case. Other specifications which reference this specification SHOULD NOT use deprecated features, but SHOULD point instead to the replacements of which the feature is deprecated in favor. Features marked as deprecated in this specification are expected to be dropped from future specifications.
The specification of the keypress event
says:
Warning The keypress event type is defined in this specification for reference and completeness, but this specification deprecates the use of this event type. When in editing contexts, authors can subscribe to the beforeinput event instead.
You can also use the keydown
and/or keyup
events. See What's the difference between keyup, keydown, keypress and input events?
However, since beforeinput
doesn't yet have much support, if none of these other events fits your use case you'll have to continue to use keypress
for now (that's the "unless there is no other way to solve a use case" exception in the spec).
There seems to be no data on browser support for
beforeinput
event. MDN has a link to it here, but the page doesn't exist. Do you have any additional info on this event?That's true, but
keydown
is a little bit different. In this question, I'm trying to figure out the exact replacement for using when it's time to stop usingkeypress
. If spec authors intendbeforeinput
for this purpose, I believe it should be more exact match.Completely agree with you. Very disappointing that
keypress
is deprecated so early when there is actually no direct replacement provided in the real world leaving developers with the tough choice on their own.@AlexanderAbakumov - That's the point of deprecation though, to warn users to avoid using the feature, in this case because it has interoperability problems. The reality is, of course, that no mainstream browser will ever stop implementing keypress, because doing so would break a large swathe of existing websites. All it means at present is that you should start thinking about how to use a combination of keydown, beforeinput, input, and keyup events instead. You could seek to detect beforeinput support and use it if it is available, falling back to keypress otherwise.
@Alohci Thank you. Just to make it more clearly, I'm not againt this deprecation in general. But the really nasty part of this specific situation is that it's deprecated too early. A deprecation is good only after providing a replacement that's doable practically with comparable level of effort. I believe no dev team having working apps with
keypress
(which is the most popular option for restricting character set forinputs
) would try messing withkeydown
,input
, andkeyup
events just becausebeforeinput
isn't available yet.