最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't keypress handle the delete key and the backspace key - Stack Overflow

programmeradmin9浏览0评论

I'm not asking this because I need a work-a-around. I have one that works fine, but I want to know WHY it doesn't. Is this bug in Javascript (or JQuery because I was using the JQuery .keypress handler) or is there a specific reason why this is so?

I'm not asking this because I need a work-a-around. I have one that works fine, but I want to know WHY it doesn't. Is this bug in Javascript (or JQuery because I was using the JQuery .keypress handler) or is there a specific reason why this is so?

Share Improve this question edited May 30, 2020 at 22:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 12, 2010 at 3:58 aaronaaarona 37.3k45 gold badges145 silver badges194 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 17

The keypress event is designed for handling a character typed by the user rather than detecting keyboard activity and the delete and backspace keys do not generate characters. Some browsers blur this line somewhat but the general principle is that the keyup and keydown events are there to detect any key being pressed and telling you about which key it is while keypress is for detecting an actual character being typed.

The short answer is that the onkeypress event is not fired for all key types in all browsers. Check the manual for your browser.

Why?

Probably not a prehensive answer, but consider Shift, when it goes down and when it es up relative to other keys is significant. And different keyboard hardware has different key rollover characteristics which you might want to know about in detail.

发布评论

评论列表(0)

  1. 暂无评论