可以在JavaScript中覆盖重音键的行为吗?

huangapple go评论57阅读模式
英文:

Can one override accent keys behavior in javascript?

问题

针对某些外语键盘(德语、瑞典语),当要输入带重音的字母(á,ñ,ê)时,首先要按下一个重音键(´,~,^),然后再按要带重音的字母。但如果按下一次重音键,不会发送input,直到再次按下或按下另一个字母,然后input将包含这个组合。我想知道的是是否有一种方法可以检测到其中一个这些... "粘键" 被按下,然后覆盖默认行为并"解除"该键,并显示重音符号本身?

英文:

For certain foreign keyboards (German, Swedish) when one wants to write accented letters (á, ñ, ê) one would first press one of the accent keys (<kbd>´</kbd>, <kbd>~</kbd>, <kbd>^</kbd>) followed by the letter to have that accent. But if one presses it once no input is sent until one either presses it again or another letter, and the input gets that combination. What I'd like to know is if there is some way to detect when one of these... "sticky keys" are pressed then override the default behavior and "un-stick" the key, and display the accent on its own?

答案1

得分: 1

你可以在输入元素上使用事件监听器:

对于 ` 你可以像这样做:

document.getElementById("input").addEventListener("keydown",(event)=>{
  console.log(event.keyCode)
})

然后你可以测试它,看看你的键的期望键码是多少,然后调整代码,像这样:

document.getElementById("input").addEventListener("keydown",(event)=>{
  if(event.keyCode==x){ //x 是你的键码
    //做一些事情
  }
})

` 的键码是192。

对于另外两个键,更难因为你需要跟踪是否按下了 shift 键:

let shiftPressed = false;
document.getElementById("input").addEventListener("keydown",(event)=>{
  if (event.keyCode == 16){ //shift
    shiftPressed=true
  }
  if (event.keyCode == 54 && shiftPressed){ // 对于 ^
    // ^
    // 做一些事情
  }
  if (event.keyCode == 192 && shiftPressed){ // 对于 ~
    // ~
    // 做一些事情
  }
})
document.getElementById("input").addEventListener("keyup",(event)=>{
  if (event.keyCode == 16){ //shift
    shiftPressed=false
  }
})

要单独添加它们,你可以像这样改变输入元素的值:

document.getElementById("input").value +="`"

编辑:

为了防止浏览器再添加一组 `` ,你可以调用 event.preventDefault。

这是完整的代码(为了可变输入重排,而不是多次调用 document.getElementById):

let shiftPressed = false;
const input = document.getElementById("input")
input.addEventListener("keydown",(event)=>{
  if (event.keyCode == 16){ //shift
    shiftPressed=true
  }
  if (event.keyCode == 54 && shiftPressed){ // 对于 ^
    event.preventDefault()
    input.value+= "^"
  }
  if (event.keyCode == 192 && shiftPressed){ // 对于 ~
    event.preventDefault()
    input.value+= "~"
  }
  else if(event.keyCode == 192){
    event.preventDefault()
    input.value+= "`"
  }
})
input.addEventListener("keyup",(event)=>{
  if (event.keyCode == 16){ //shift
    shiftPressed=false
  }
})
英文:

You can use event listeners on the input element:

For ` you can do something like:

document.getElementById(&quot;input&quot;).addEventListener(&quot;keydown&quot;,(event)=&gt;{
  console.log(event.keyCode)
})

Then you can test it and see what the desired keycode is for your key and then adjust the code to something like this:

document.getElementById(&quot;input&quot;).addEventListener(&quot;keydown&quot;,(event)=&gt;{
  if(event.keyCode==x){ //x is your keycode
    //do something
  }
})

The keycode of ` is 192.

For the other two it is harder because you need to keep track if shift is being pressed:

  let shiftPressed = false;
  document.getElementById(&quot;input&quot;).addEventListener(&quot;keydown&quot;,(event)=&gt;{
    if (event.keyCode == 16){ //shift
      shiftPressed=true
    }
    if (event.keyCode == 54 &amp;&amp; shiftPressed){ // for ^
      // ^
      // do some things
    }
    if (event.keyCode == 192 &amp;&amp; shiftPressed){ // for ~
      // ~
      // do some things
    }
  })
  document.getElementById(&quot;input&quot;).addEventListener(&quot;keyup&quot;,(event)=&gt;{
    if (event.keyCode == 16){ //shift
      shiftPressed=false
    }
  })

To add them on their own you can change the value of the input element like this:

document.getElementById(&quot;input&quot;).value +=&quot;`&quot;

Edit:

To prevent the browser from adding another set of `` you can call event.preventDefault.

This gives the total code (reformatted for variable input instead of calling document.getElementById multiple times):

let shiftPressed = false;
const input = document.getElementById(&quot;input&quot;)
input.addEventListener(&quot;keydown&quot;,(event)=&gt;{
  if (event.keyCode == 16){ //shift
    shiftPressed=true
  }
  if (event.keyCode == 54 &amp;&amp; shiftPressed){ // for ^
    event.preventDefault()
    input.value+= &quot;^&quot;
  }
  if (event.keyCode == 192 &amp;&amp; shiftPressed){ // for ~
    event.preventDefault()
    input.value+= &quot;~&quot;
  }
  else if(event.keyCode == 192){
    event.preventDefault()
    input.value+= &quot;`&quot;
  }
})
input.addEventListener(&quot;keyup&quot;,(event)=&gt;{
  if (event.keyCode == 16){ //shift
    shiftPressed=false
  }
})

huangapple
  • 本文由 发表于 2023年3月7日 22:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定