编程小贴士

给你的编程提供小点子


在JavaScript中监听IME键盘输入事件

在 JavaScript 中监听用户的键盘输入是很容易的事情,但用户一旦使用了输入法,问题就变得复杂了。输入法应当如何触发键盘事件呢?是每一下击键都触发一次事件,还是选词 完毕才触发事件呢?整句输入又该如何触发事件呢?不同的操作系统和不同的浏览器对此有不同的看法。在最糟糕的情况下,用户使用输入法后浏览器就只触发一次 keydown ,之后就没有任何的键盘事件了。这对于 Suggestion 控件的实现来说是个大问题,因为 Suggestion 控件需要监听文本输入框的变化,而事件是最准确也最节省计算资源的做法,如果换成轮询的话性能就可能受到影响。

首先,要监听启用输入法后的击键事件应当使用 keydown 事件,这是信息最丰富的一个事件,因为在启用输入法后别的键盘事件可能不会被触发。其次,大多数操作系统和浏览器都实现了一个事实标准,就是在用户使用输 入法输入时, keydown 事件传入的 keyCode 取值为 229 。然而触发 keydown 的频率是不确定的,有些情况下每一下击键都触发事件,有些情况下只有选词完毕才触发事件。这时候,如果我们还是要实时监控文本框的内容变化,就必须使用轮 询了。

var timer;

var imeKey = 229;

function keydownHandler (e) {

clearInterval(timer)

if (e.keyCode == imeKey) {

timer = setInterval(checkTextValue, 50);

} else {

checkTextValue();

}

}

function checkTextValue() {

/* handle input text change */

}

Opera 是一款有趣的浏览器,别人做的事情它都不做,别人都不做的事情它都喜欢做。例如说,它偏偏不支持 keyCode == 229 这个事实标准,而要使用 keyCode == 197 来表示输入法的使用。因此,你需要在上述代码的基础上做一下改良,如果监测到是 Opera 浏览器,就换一个 keyCode 常量来做比较。

var imeKey = (UA.Opera == 0) ? 229 : 197;

最后,还有一个更不受重视的浏览器叫做 Firefox for Mac 。估计是因为 Mac 版本对于 Mozilla 来说实在是太不重要了,所以很多 Windows 版本都没问题的地方 Mac 版本就会出小问题,例如说对上述事件的支持。 Firefox for Mac 不会出现 keyCode == 229 的情况,而且在输入法启用后只有第一下击键会触发 keydown 事件,因此只能在击键后一直使用轮询。

if (e.keyCode == imeKey || UA.Firefox > 0 && UA.OS == \’Macintosh\’) {

在添加了这两项改进后,实时监控文本框的变化就没有问题了,即使用户启用了输入法。完整的代码如下:

var timer;

var imeKey = (UA.Opera == 0) ? 229 : 197;

function keydownHandler (e) {

clearInterval(timer)

if (e.keyCode == imeKey || UA.Firefox > 0 && UA.OS == \’Macintosh\’) {

timer = setInterval(checkTextValue, 50);

} else {

checkTextValue();

}

}

function checkTextValue() {

/* handle input text change */

}

125 Responses to “ 在JavaScript中监听IME键盘输入事件 ”

  1. chocopie说道:

    Skuiox Major thanks for the article post.Really thank you! Fantastic.

  2. Major thanks for the blog post. Really Great.

  3. Tel Aviv Escorts说道:

    Thanks again for the article post. Keep writing.

  4. Well, with only three games left in the tank and that this could turn out to

  5. Oluwadamilare说道:

    When you can grab my attention in the first few words of an article it as an accomplishment. You ave ignited my interest on this subject and I thank you for this.

  6. tunasbola.org说道:

    Would you recommend starting with a free platform like WordPress or go for a paid option?

  7. website说道:

    You ave made some really good points there. I checked on the web for additional information about the issue and found most people will go along with your views on this web site.

  8. Thanks so much for the blog post.Thanks Again. Keep writing.

  9. Your writing is very useful, thank you very much.

  10. Woking taxi说道:

    This blog is extremely cool. How was it made !?

  11. bitstarz review说道:

    It as going to be finish of mine day, but before end I am reading this fantastic article to increase my experience.

  12. austdoor tphcm说道:

    I really liked your article.Much thanks again. Cool.

  13. Wow, fantastic blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is magnificent, as well as the content!

  14. her latest blog说道:

    your web hosting is OK? Not that I am complaining, but slow loading instances

  15. hip hop说道:

    motorcycle accident claims What college-university has a good creative writing program or focus on English?

  16. It as hard to come by well-informed people in this particular subject, however, you sound like you know what you are talking about! Thanks

  17. Wow! Thank you! I continuously needed to write on my website something like that. Can I implement a fragment of your post to my blog?

  18. xuong si quan ao说道:

    It is not my first time to visit this web site, i am visiting this site dailly and take pleasant information from here daily.

  19. It is not my first time to pay a quick visit this site,

  20. Thanks a lot for the post.Much thanks again. Great.

  21. see more说道:

    It as exhausting to find educated people on this topic, but you sound like you understand what you are talking about! Thanks

  22. this side of the story. I was surprised that you are not more popular

  23. There as a lot of folks that I think would

  24. rating说道:

    Wow, great post.Really looking forward to read more. Will read on

  25. Thanks for sharing this good piece. Very inspiring! (as always, btw)

  26. Vu que nous voulons que votre bien, nous offrons la premiere communication

  27. Tejidos说道:

    The thing i like about your weblog is that you generally post direct for the point info.:,*`,

  28. my page说道:

    Say, you got a nice article post.Much thanks again. Awesome.

  29. blue nike说道:

    Wow, superb blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is excellent, let alone the content!

  30. This is very interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your fantastic post. Also, I ave shared your web site in my social networks!

  31. School news说道:

    Thanks for sharing this first-class write-up. Very inspiring! (as always, btw)

  32. Waec exam说道:

    pretty practical material, overall I think this is worthy of a bookmark, thanks

  33. I was suggested this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are incredible! Thanks!

  34. stop bullying说道:

    Very nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts. In any case I will be subscribing to your feed and I hope you write again soon!

  35. This particular blog is definitely educating additionally factual. I have found a lot of helpful tips out of this blog. I ad love to come back again soon. Thanks!

  36. scary maze说道:

    I’а†ve recently started a website, the information you provide on this site has helped me tremendously. Thanks for all of your time & work.

  37. I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You are incredible! Thanks!

  38. Post writing is also a fun, if you know afterward you can write or else it is complex to write.

  39. vender en linea说道:

    My brother recommended I might like this website. He was entirely right. This post truly made my day. You cann at imagine just how much time I had spent for this information! Thanks!

  40. Some really prime blog posts on this internet site , saved to my bookmarks.

  41. entclass说道:

    Looking forward to reading more. Great article post. Really Cool.

  42. Thanks for the blog post.Thanks Again. click here

  43. Thank you for your blog article.Thanks Again. Awesome.

  44. Thank you ever so for you blog article.Really looking forward to read more. Cool.

  45. Winners说道:

    naturally like your web-site however you have to check the spelling

  46. free guest post说道:

    Your style is really unique compared to other people I ave read stuff from. Thanks for posting when you ave got the opportunity, Guess I will just book mark this site.

  47. If some one wishes expert view on the topic of running a blog after

  48. just go to说道:

    What as up, just wanted to tell you, I loved this article. It was inspiring. Keep on posting!

  49. for more info说道:

    You made some good points there. I looked on the internet for the issue and found most guys will go along with with your blog.

  50. Just Browsing While I was surfing today I saw a great article about

  51. click here说道:

    Thank you ever so for you article.Really looking forward to read more. Cool.

  52. whoah this blog is great i love reading your posts. Keep up the great work! You know, a lot of people are looking around for this information, you can aid them greatly.

  53. Thanks for the blog post.Really looking forward to read more.

  54. social说道:

    I think this is a real great blog article.Thanks Again. Really Cool.

  55. browse说道:

    Usually I do not read article on blogs, but I wish to say that this write-up very forced me to check out and do it! Your writing taste has been amazed me. Thanks, quite great article.

  56. miller electric说道:

    Really appreciate you sharing this blog.Really looking forward to read more. Will read on

  57. Very nice post. I absolutely appreciate this website. Thanks!

  58. ads说道:

    I wanted to thank you for this fantastic write-up, I certainly loved each and every small bit of it. I ave bookmarked your web site to look at the latest stuff you post.

  59. go to see说道:

    Thanks a lot for the article.Really thank you! Want more.

  60. Im grateful for the article post.Really looking forward to read more. Will read on…

  61. Thanks again for the blog. Keep writing.

  62. go说道:

    Very useful post right here. Thanks for sharing your knowledge with me. I will certainly be back again.

  63. dong ho seiko说道:

    Some genuinely choice articles on this website , saved to bookmarks.

  64. Very informative blog article. Really Cool.

  65. daftar pb说道:

    Well I really liked studying it. This post offered by you is very useful for proper planning.

  66. bon tam bang go说道:

    I went over this site and I conceive you have a lot of superb info , bookmarked (:.

  67. Wow! This can be one particular of the most helpful blogs We have ever arrive across on this subject. Basically Magnificent. I am also an expert in this topic therefore I can understand your effort.

  68. good morning说道:

    I truly appreciate this blog article.Much thanks again.

  69. You ave made some good points there. I looked on the net for additional information about the issue and found most people will go along with your views on this website.

  70. This is a topic which is near to my heart Cheers! Exactly where are your contact details though?

  71. Really good info! Also visit my web-site about Clomid pills

  72. I truly appreciate this blog.Thanks Again. Much obliged.

  73. Very informative blog.Really looking forward to read more. Cool.

  74. uzinform说道:

    May just you please extend them a little from next time?

  75. artlife.rv.ua说道:

    I think other site proprietors should take this site as an model, very clean and excellent user friendly style and design, let alone the content. You are an expert in this topic!

  76. Links I am continually looking online for ideas that can help me. Thx!

  77. Game Online说道:

    This blog is without a doubt educating as well as amusing. I have chosen a lot of interesting things out of this source. I ad love to come back over and over again. Thanks!

  78. Canon dslr 200d说道:

    You ave made some really good points there. I checked on the web to find out more about the issue and found most individuals will go along with your views on this website.

  79. Bagdad Barcelona说道:

    I really liked your post.Much thanks again. Awesome.

  80. click here说道:

    Informative article, exactly what I needed.

  81. bep dien tu说道:

    Just that is necessary. I know, that together we can come to a right answer.

  82. This blog is definitely awesome as well as factual. I have picked up helluva handy advices out of this blog. I ad love to go back again and again. Thanks a bunch!

  83. click here说道:

    You actually make it appear really easy along with your presentation however

  84. I’а†ve learn several excellent stuff here. Certainly price bookmarking for revisiting. I surprise how so much effort you set to create this kind of wonderful informative web site.

  85. I think other web-site proprietors should take this website as an model, very clean and great user genial style and design, let alone the content. You are an expert in this topic!

  86. fluffy kitt说道:

    I think you have mentioned some very interesting points, thank you for the post.

  87. bo dam说道:

    It?s actually a great and helpful piece of info. I am satisfied that you shared this useful info with us. Please keep us informed like this. Thank you for sharing.

  88. Mobile Computer说道:

    Wanted to drop a remark and let you know your Rss feed isnt working today. I tried including it to my Google reader account but got nothing.

  89. I think this is a real great post.Really looking forward to read more. Awesome.

  90. view it now说道:

    It as fantastic that you are getting thoughts from this post as well as from our dialogue made at this time.

  91. khoa dien tu说道:

    Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is wonderful, let alone the content!

  92. Regards for helping out, good info. Our individual lives cannot, generally, be works of art unless the social order is also. by Charles Horton Cooley.

  93. Very informative blog article.Really looking forward to read more. Fantastic.

  94. I’а†ve recently started a website, the info you offer on this site has helped me greatly. Thank you for all of your time & work.

  95. information. I am bookmarking and will be tweeting this

  96. mealcoupons说道:

    There as certainly a great deal to learn about this topic. I love all of the points you made.

  97. It as not that I want to duplicate your web site, but I really like the style. Could you tell me which style are you using? Or was it custom made?

  98. mia pron khalifa说道:

    pCi3gZ Sounds like anything plenty of forty somethings and beyond ought to study. The feelings of neglect is there in a lot of levels every time a single ends the mountain.

  99. It as very easy to find out any topic on web as compared to textbooks, as I found this piece of writing at this website.

  100. their payment approaches. With the introduction of this kind of

  101. Look advanced to far added agreeable from you! However,

  102. You have brought up a very good points , thankyou for the post.

  103. There is perceptibly a bunch to know about this. I suppose you made some nice points in features also.

  104. Im no expert, but I think you just crafted an excellent point. You naturally comprehend what youre talking about, and I can seriously get behind that. Thanks for staying so upfront and so sincere.

  105. Of course, what a fantastic site and informative posts, I definitely will bookmark your site.Have an awsome day!

  106. Really appreciate you sharing this blog post.Thanks Again. Much obliged.

  107. Just added your website to my list of price reading blogs

  108. bistro masa说道:

    Very neat blog post.Thanks Again. Want more.

  109. Wow! Thank you! I continually wanted to write on my website something like that. Can I take a part of your post to my website?

  110. Tattoo Artist说道:

    I visit everyday some blogs and websites to read articles, except this website offers quality based articles.

  111. link vao 188bet说道:

    personally recommend to my friends. I am confident they will be benefited from this site.

  112. penang seo说道:

    Really appreciate you sharing this post.

  113. Stunning story there. What happened after? Good luck!

  114. This is one awesome blog article.Really looking forward to read more.

  115. Catarina Honda说道:

    There are a couple of interesting points in time on this page but I don’t determine if I see them all center to heart. There exists some validity but I’ll take hold opinion until I consider it further. Great post , thanks and now we want a lot more! Included in FeedBurner at the same time

  116. Lionel Tian说道:

    Aw, this was a really nice post. In idea I would like to put in writing like this moreover ? taking time and actual effort to make an excellent article? but what can I say? I procrastinate alot and in no way seem to get one thing done.

  117. Marty Rulon说道:

    Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is magnificent, as well as the content!

  118. Randi Castorena说道:

    I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to create my own blog and would like to find out where u got this from. many thanks

  119. Len Quade说道:

    Helpful blog post and thanks for sharing. Some things in here I haven’t thought about before, I would like to use this moment to say that I really like this blog. It has been a great resource of knowledge for me. Thank you so much!

  120. Ezekiel Zieman说道:

    I hate the fact that my iPhone be changing words and shit like damn.. Let me be nonformal for a minute.|Ceezyyy|

  121. Ulrike Laza说道:

    Aw, this was an extremely nice post. In idea I have to put in place writing similar to this additionally – spending time and actual effort to manufacture a good article… but what things can I say… I procrastinate alot and also no indicates manage to get something carried out.

  122. Clair Storment说道:

    Some truly excellent information, Sword lily I detected this.

  123. Faviola Schilder说道:

    Wow that was unusual. I just wrote an extremely long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say wonderful blog!

  124. Rhett Strauf说道:

    I found your blog web site on google and test a few of your early posts. Continue to maintain up the superb operate. I just extra up your RSS feed to my MSN Information Reader. Searching for ahead to reading more from you later on!…

  125. Piper Henneke说道:

    I like this post, enjoyed this one regards for putting up.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>