简体   繁体   English

onekeydown 为 Firefox 65.0 ubuntu 18.04 中的每次键盘按下返回“Process”字符串

[英]onekeydown returns "Process" string for each keyboard press in Firefox 65.0 ubuntu 18.04

I'm using Firefox 65.0 for my web development.我正在使用 Firefox 65.0 进行 Web 开发。 My project code contains password input.我的项目代码包含密码输入。 I developed this before more than a year ago using JS code that detects the key presses using onkeydown and onkeyup functions.一年多前,我使用 JS 代码开发了它,该代码使用onkeydownonkeyup函数检测按键。 It works fine for all browsers except the new Firefox version 65.0它适用于所有浏览器,除了新的 Firefox 65.0 版

My code is similar to this:我的代码类似于:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type=password id="button" autocomplete_="off"></input> <script> $("#button").keydown(function(event) { console.log("key: " + event.key); }); document.getElementById("button").onkeydown = function(event) { console.log("key: " + event.key); }; </script>

For both cases the event.key value is not the pressed button, but it's just "Process" as a string.对于这两种情况, event.key值不是按下的按钮,而是作为字符串的"Process" Does someone have an idea why I get this result?有人知道为什么我会得到这个结果吗? How can I solve it and detect the real pressed key value?我该如何解决并检测真正的按键值?

My OS version: is ubuntu 18.04我的操作系统版本:是 ubuntu 18.04

Edit: add debug screen shot编辑:添加调试屏幕截图

In addition I can't find any attribute under event that can help me另外我在事件下找不到任何可以帮助我的属性

you can find below a screen shot of a debug in the onkeydown handler for the case of 'a' key.您可以在 onkeydown 处理程序中找到“a”键情况下的调试屏幕截图。

在此处输入图片说明

There are quite a few different event properties for key codes, and they all return something a little bit different.键代码有很多不同的事件属性,它们都会返回一些不同的东西。 Try event.which , and if that doesn't work, here's a list of other event properties尝试event.which ,如果这不起作用,这里是其他事件属性的列表

Edited to provide additional info编辑以提供更多信息

I did some testing, the cause appears to be that your trying to log keypresses on an input of type="password".我做了一些测试,原因似乎是你试图在 type="password" 的输入上记录按键。 I tested your code snippet in Firefox after removing the type="password" and both event.which and event.key work normally.删除 type="password" 后,我在 Firefox 中测试了您的代码片段,并且event.whichevent.key可以正常工作。 I suppose its intentionally obfuscated by Firefox to avoid scripts logging password keystrokes.我想它是由 Firefox 故意混淆的,以避免脚本记录密码击键。 If you need the value of the password, use event.target.value in the event listener.如果您需要密码的值,请在事件侦听器中使用event.target.value Also I would recommend using the 'keyup' event rather than keydown, or you run into the problem of the listener repeatedly firing when you hold down a key.此外,我建议使用 'keyup' 事件而不是 keydown,否则您会遇到在按住某个键时反复触发侦听器的问题。

With the password attribute使用密码属性

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="password" id="button" autocomplete_="off"></input> <script> // Either remove the password type on your button, or access the event.target.value property and slice out the last keypress document.querySelector("#button").addEventListener('keyup', (event) => { console.log(`Which: ${event.which}, Key: ${event.key}`); console.log(`Value: ${event.target.value}`); }); </script>

Without the password attribute没有密码属性

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="button" autocomplete_="off"></input> <script> // Either remove the password type on your button, or access the event.target.value property and slice out the last keypress document.querySelector("#button").addEventListener('keyup', (event) => { console.log(`Which: ${event.which}, Key: ${event.key}`); //console.log(`Value: ${event.target.value}`); }); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM