English 中文(简体)
关于KeyPress Vs,关于KeyUp和KeyDown
原标题:onKeyPress Vs. onKeyUp and onKeyDown

这三个事件之间有何区别? 我发现:

  • The onKeyDown event is triggered when the user presses a key.
  • The onKeyUp event is triggered when the user releases a key.
  • The onKeyPress event is triggered when the user presses & releases a key (onKeyDown followed by onKeyUp).

我理解头两条,但正文tonKeyPress。 同onKeyUp? 能否发布一个钥匙(onKeyUp)而无需改动(onKeyDown)?

这是一种轻视的混淆,有人可以向我表明这一点吗?

最佳回答

http://web.archive.org/web/20161212021242/http://www.bloggingdeveloper.com/post/KeyPress-KeyDown-KeyUp-The-Difference-Between-Javascript-Key-Events.aspx” rel=“noretinger”>link最初用于本答复。

从这一联系来看:

理论上,onKeyDownonKeyUp 活动是受到压制或释放的关键,而<代码>onKeyPress活动则具有分类性质。 理论的实施在所有浏览器中并不相同。

问题回答

这里的大多数答复更多地侧重于理论而不是实际事项,在<代码>keyup和keypress之间有一些很大的差异,因为它至少涉及投入领域价值(在43年测试)。

如果用户类型1 纳入一个空洞的投入要素:

  1. 输入元件的价值将是<条码>关键标线/代码>内的空洞(旧值)。 秘书

  2. 输入元件的价值将在<代码>keyup<>code>1(新值)内输入。

如果你做一些事情,依靠的是了解投入后的新价值,而不是目前的价值,如线性鉴定或自动制片,这一点至关重要。

设想:

  1. The user types 12345 into an input element.
  2. The user selects the text 12345.
  3. The user types the letter A.

当<代码>keypress在进入A后发生火灾时,文本箱现在只载有的信。 A

但:

  1. Field.val() is 12345.
  2. $Field.val().length is 5
  3. The user selection is an empty string (preventing you from determining what was deleted by overwriting the selection).

因此,浏览器(Firefox 43)似乎抹去了用户选择,then <>>>> /em>fires the keypress event, then Update the field content, thenfireskeyup

首先,它们具有不同的含义:它们开火:

第二,一些关键人物向其中一些事件开火,不向其他事件发射<>。 例如,

  • KeyPress ignores delete, arrows, PgUp/PgDn, home/end, ctrl, alt, shift etc while KeyDown and KeyUp don t (see details about esc below);
  • when you switch window via alt+tab in Windows, only KeyDown for alt fires because window switching happens before any other event (and KeyDown for tab is prevented by system, I suppose, at least in Chrome 71).

此外,您应铭记,event.keyCode (and event. )对KitDown和KuiUp通常具有相同价值,但对于Press而言,则有所不同。 Try the playground 我创造了机会。 顺便提一下:在 Chrome,当我新闻ctrl+ainput//<>>上,对于带有<条码>的火力(和<条码>.keyCode (及<条码>>>>>)等于<1条码/条/条码>! (当投入不是空的时,它根本就没有火灾。)

注:这些日,使用<代码>event.key和event.code,是最佳选择,因为它在浏览器、投放器和活动(法克)方面是标准化的,而和event.keyCode >>>>s deprecated (

最后,有pragmatics:

  • For handling arrows, you ll probably need to use onKeyDown: if user holds , KeyDown fires several times (while KeyUp fires only once when they release the button). Also, in some cases you can easily prevent propagation of KeyDown but can t (or can t that easily) prevent propagation of KeyUp (for instance, if you want to submit on enter without adding newline to the text field).
  • Suprisingly, when you hold a key, say in textarea, both KeyPress and KeyDown fire multiple times (Chrome 71), I d use KeyDown if I need the event that fires multiple times and KeyUp for single key release.
  • KeyDown is usually better for games when you have to provide better responsiveness to their actions.
  • esc is usually processed via KeyDown: KeyPress doesn t fire and KeyUp behaves differently for inputs and textareas in different browsers (mostly due to loss of focus)
  • If you d like to adjust height of a text area to the content, you probably won t use onKeyDown but rather onKeyPress (PS ok, it s actually better to use onChange for this case).

我在我的项目中使用了所有3个,但不幸可能忘记了一些务实的做法。 (待指出:还有<条码>输入和<条码>变动事项>。)

。 钥匙在倒塌时发射(如在短处;例如,在<代码>Ctrl+A、<代码>Ctrl<>/code>中)。

<编码>钥匙在钥匙释放时发射(包括 mo/etc keys)。

<条码>的关键>是作为<条码>的关键<>/代码>和<条码>的组合发射的,或取决于键盘重复(<条码>>的关键>。 (这种重复行为是我事先测试的。) 如果你测试的话,增加评论!

<代码>textInput (webkit only) is fire when some text is into (for example, Shift+A 将进入A级,但<代码>Ctrl+A将选择案文而不是输入任何案文。 在该案中,所有其他事件都遭到射击。

http://unixpapa.com/js/key.html“rel=“noretinger”>article Jan Wolter是我所走过的最好东西,你可以找到存档的拷贝rel=“noreferer”here。 如果联系已经死亡的话。

它真实地解释了所有重要事件。

keydown, 事件发生在钥匙受到压力之后,紧接着是主要压缩活动。 然后,在钥匙释放后,便会产生keyup活动。

为了理解keydown和keypress之间的区别,应当区分characterskeys。 a key是计算机键盘上的物理纽盘。 Acharacter是按吨位排列的象征。 在美国钥匙板上,在4的钥匙上打上了Shift的钥匙通常会产生“美元标识”。 这不一定是世界上每一个重要方面的情况。 在理论上,keydown和keyup活动是受到压制或释放的关键,而keypress活动则是一种特性。 在实践中,这并非总是得到实施。

另有一次,一些浏览器在keypress之后立即发射另一场称为textInput<>>。 “DOM 3”标准的早期版本意在取代keypress活动,但整个概念后来被撤销。 Webkit在525和533版本之间支持了这项工作,Im对国际电子格式表示支持,但我从未发现,可能是因为Webkit要求将其称作textInput,而IE称其为textinput

还有一个名为input的活动,得到所有浏览器的支持,是在向textarea或投入领域作出改动之后发射的。 通常的关键压力会火力,因此,字体的特性将出现在文字区,然后投入会火上。 input/em> 事件实际上没有提供关于关键类型的任何信息——你必须检查文本箱,以显示变化,因此我们不认为这是一个关键事件,而没有把它真实地记载在这里。 虽然最初只界定了文本处理器和投入箱,但我认为,还有一些行动要将其普及到其他类型物体上。

key和key似乎也一样(在上文提及的短 key钥匙小差的情况下)。

你可以尝试:

<textarea type="text" onkeypress="this.value=this.value +  onkeypress  "></textarea>
<textarea type="text" onkeydown="this.value=this.value +  onkeydown  " ></textarea>
<textarea type="text" onkeyup="this.value=this.value +  onkeyup  " ></textarea>

你们会看到,在钥匙受到压力时,而不是在钥匙受到压力时,都会引发key和down。

不同之处是,这次活动不是一次而是多次发生的(只要你掌握了钥匙)。 了解这一点并相应处理。

冲压式活动对所有钥匙都有效,但ALT、CTRL、SHIFT、ESC等除外。 关键事件的手段涵盖了所有关键因素。

赞成:

在利用 on击事件启动联合调查方法时,该事件的char形形同你掌握的星号一样!

例如,在使用基压时,NOT在使用基调时将退回与字母钥匙上的关键数相同的焦炭编码!

在我看来,我检查某些char形码的文字为什么在使用key时失败!

Demo: https://www.w3schools.com/code/tryit.asp?filename=FMMBXKZLP1M

并且是。 我确实知道这些方法的定义是不同的,但令人非常混淆的是,在这两种方法中,活动的结果都是用事件检索的。 关键概念是,它们不会回去同样的价值,而不是非常明确的执行情况。

基本上,这些事件在不同浏览器类型和版本上不同,我做了一些小 j测试,你可以检查黄色,了解这些事件如何为你的目标环境发生,希望这一帮助

可能有用的一些实际事实,可以决定哪些活动需要处理(将下文文字放在投入箱上):

$( input ).on( keyup keydown keypress ,e=>console.log(e.type, e.keyCode, e.which, e.key))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input/>

出版社:

  • 不插入/搭载钥匙(例如Shift,,Ctrl)不会触发keypress。 Press Ctrl and release it:

    第17节

    B. 关键控制 17

  • 采用特性转换为其他特性的关键板的钥匙可能导致Dead 复制“keys”(例如~,>>>><>>>keydown>。 Press and release it in order to show a Double ́ :

    B. 关键目标 192

    关键缩编 192

    第180号关键指令

    第180号关键指令

    关键因素 192

此外,非打字投入(例如<input category=“range”>)仍将根据压力钥匙引发所有关键、关键和关键压缩事件。

BLAZOR...。

如果你想要检查哪一个钥匙在key子上被压倒,但如果你想从文字现场拿到案文,然后检查最后的钥匙,例如,你正在扫描一个条码,你甚至想要发射(几乎所有的条码扫描器都发送了13个“ENTER”),那么你就应该用钥匙,否则你不会在文字领域打上文字。

例如

<input category=“text” category=“form-control”@bind=“barcode”@onkeyup=“BarCodeScan” placeholder=“Scan”/>

这将把BarCodeScan功能称作在你通过打字而进入报界之后,或者如果你从扫描器中扫描,将自动称作BarCodeScan功能。 如果你在此使用“关键”或“关键”的话,那么约束不会发生,你不会从案文领域获得。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.