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

javascript - Get character typed, cross-browser - Stack Overflow

programmeradmin6浏览0评论

Simple question -- Does anyone know of a reliable cross-browser function to get the character typed from a keydown event? I can write one from the quirksmode grid but would rather not re-invent the wheel for something so simple yet so non-standard.

Let me clarify:

There is no way to do this simply using event.keyCode or event.which. The overall logic is something like:

  • get keycode
  • detect shift, ctrl
  • if ctrl, ignore
  • create map of keycodes with and without shift key
    • example map {186 : ';', 187 : '=', 188 : ',', ....}
    • need a separate map for shift key modifier
    • map needs to change depending on browser (especially Safari Mac which has keycodes like 60,000)
  • if keycode is in map, return mapped key
  • if keycode is not in map, normalize numbers, apply shift modifier key if necessary, return String.fromCharCode(key)

It's not a simple solution, which is why I'm looking for a pre-made one :)

Simple question -- Does anyone know of a reliable cross-browser function to get the character typed from a keydown event? I can write one from the quirksmode grid but would rather not re-invent the wheel for something so simple yet so non-standard.

Let me clarify:

There is no way to do this simply using event.keyCode or event.which. The overall logic is something like:

  • get keycode
  • detect shift, ctrl
  • if ctrl, ignore
  • create map of keycodes with and without shift key
    • example map {186 : ';', 187 : '=', 188 : ',', ....}
    • need a separate map for shift key modifier
    • map needs to change depending on browser (especially Safari Mac which has keycodes like 60,000)
  • if keycode is in map, return mapped key
  • if keycode is not in map, normalize numbers, apply shift modifier key if necessary, return String.fromCharCode(key)

It's not a simple solution, which is why I'm looking for a pre-made one :)

Share Improve this question edited Apr 24, 2023 at 16:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 1, 2011 at 18:13 user578895user578895
Add a ment  | 

3 Answers 3

Reset to default 6

Are you willing to use jQuery?

$("input").bind("keydown",function(e){ var value = this.value + String.fromCharCode(e.keyCode); }

I believe that switching to the keypress event would solve the issue mentioned in your ment. Would the code below meet your requirments?

$("input").bind("keypress",function(e){ var value = this.value + String.fromCharCode(e.keyCode); }

I've been wondering the same thing for years. Recently I got fed up with having to look up keycodes for events that I wrote a module called keysight that translates keypress, keydown, and keyup events into characters and keys respectively.

Example:

 element.addEventListener("keydown", function(event) {
    var character = keysight(event).char
 })

It doesn't ignore anything on ctrl, so you'd have to do that manually if you want. But it does all the rest of the logic you gave.

That opton is the best cause will catch the value updated, instead of using 'keydown' who just gets the value without the last char typed

element.addEventListener("keyup", () => {
  console.log(element.value)
})
发布评论

评论列表(0)

  1. 暂无评论