问题描述
我已经搜索了KeyPressed
和KeyTyped
事件之间的区别,但仍然不清楚.我发现的一件事是Keypressed比KeyTyped首先被触发.请澄清一下这些触发的确切时间.哪个用途适合用于哪个目的?预先感谢
I have searched about difference between KeyPressed
and KeyTyped
Events but still I'm not clear about that . One thing I have found is Keypressed is triggered first than KeyTyped .Please clarify me when these are triggered exactly . Which is appropriate to use for which purpose ?Thanks in advance
推荐答案
keyPressed
.按下可以转换为Unicode字符的键时,将触发keyTyped
.例如,如果按住Shift键,则按"a"会告诉keyTyped
您键入了大写字母A,而keyPressed
只会得到"a"键,没有大写字母或小写字母.您无法从keyPressed
调用event.getKeyChar()
,因为没有与事件关联的键字符.字符仅来自keyTyped
.
keyPressed
is fired whenever any key press occurs. keyTyped
is fired when a key is pressed that can be converted into a unicode character. If the shift key is down, for example, pressing "a" will tell keyTyped
that you typed a capital A, and keyPressed
will just get the "a" key, without capital or lowercase designations. You cannot call event.getKeyChar()
from keyPressed
, because there is no key char associated with the events. Characters only come from keyTyped
.
基本思想是keyTyped
用于查找键入的字符,而keyPressed
用于获取原始按键.
The basic idea is that keyTyped
is used to find characters that are typed, and keyPressed
is used for obtain raw key presses.
这篇关于KeyPressed和KeyTyped混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!