我如何知道用户正在打字或粘贴?

发布于 2024-08-25 22:31:03 字数 134 浏览 1 评论 0原文

在我的 JSP 的文本字段中,我希望知道用户是在输入数据还是只是粘贴。 我如何使用 javascript 来识别它?

编辑:根据安迪的回答,我知道如何去做,但仍然好奇那些人是如何编写 onpaste 事件的。

In text fields of my JSP, I wish to know whether user is typing in the data or just pasting.
How can I identify this using javascript ?

EDIT: As per Andy's answer I know how I can go about it, but still curios how those guys wrote onpaste event.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残龙傲雪 2024-09-01 22:31:03

Safari、Chrome、Firefox 和 Internet Explorer 都支持 onpaste 事件(不确定 Opera 是否支持)。锁定 onpaste 事件,您将能够在粘贴内容时捕获该事件。


Writing this is simple enough. Add the event handler to your input using html:

<input type="text" id="myinput" onpaste="handlePaste(event);">

或 JavaScript-DOM:

var myInput = document.getElementById("myInput");

if ("onpaste" in myInput) // onpaste event is supported
{
    myInput.onpaste = function (e)
    {
        var event = e || window.event;
        alert("User pasted");
    }
}
// Check for mutation event support instead
else if(document.implementation.hasFeature('MutationEvents','2.0'))
{
    /* You could handle the DOMAttrModified event here, checking 
       new value length vs old value length but it wouldn't be 100% reliable */
}

据我所知,Opera 不支持 onpaste 事件。您可以使用 DOMAtrrModified 事件,但即使脚本更改输入框的值,该事件也会触发,因此您必须小心处理。不幸的是,我不熟悉突变事件,所以我不想通过编写一个我不自信的例子来搞乱这个答案。

Safari, Chrome, Firefox and Internet Explorer all support the onpaste event (not sure about Opera). Latch onto the onpaste event and you will be able to catch whenever something is pasted.


Writing this is simple enough. Add the event handler to your input using html:

<input type="text" id="myinput" onpaste="handlePaste(event);">

or JavaScript-DOM:

var myInput = document.getElementById("myInput");

if ("onpaste" in myInput) // onpaste event is supported
{
    myInput.onpaste = function (e)
    {
        var event = e || window.event;
        alert("User pasted");
    }
}
// Check for mutation event support instead
else if(document.implementation.hasFeature('MutationEvents','2.0'))
{
    /* You could handle the DOMAttrModified event here, checking 
       new value length vs old value length but it wouldn't be 100% reliable */
}

From what I've read, Opera does not support the onpaste event. You could use the DOMAtrrModified event, but this would fire even when scripts change the value of the input box so you have to be careful with it. Unfortunately, I'm not familiar with mutation events so I wouldn't like to mess this answer up by writing an example that I wouldn't be confident of.

一刻暧昧 2024-09-01 22:31:03

计算按键次数并确保其与文本框中的内容匹配,粘贴不会包含文本框中完整的字符数。

Count the key presses and make sure it matches whats in the text box a paste will not have complete number of characters as is in the text box.

幸福还没到 2024-09-01 22:31:03

你永远不会确切地知道。即使在拦截按键输入时,用户也可能使用上下文菜单来使用鼠标进行粘贴。访问剪贴板(将输入与剪贴板内容进行比较)将无法按您想要的方式工作,因为它是严格的仅限用户操作。未经用户明确同意,您无法以编程方式访问(浏览器将显示确认消息)。

You will never know for sure. Even when intercepting key input, the use may have used the context menu to paste using the mouse. Accessing the clipboard (to compare the input with the clipboard contents) will not work the way you want because it is a strict user-only operation. You are not able to access is programmatically without the explicit consent of the user (the browser will show a confirmation message).

如梦 2024-09-01 22:31:03

我知道对于文本区域,您可以使用 onPaste 事件捕获粘贴事件。

HTML:

<textarea id="textEditor" />

在 JS 中:

var editor = document.getElementById("textEditor");

if (isIE /* determine this yourself */) {
   editor.onPaste = function() {

   }
} else {
   //Not IE
   editor.onpaste = function() {

   }
}

//The capitalisation of the onpaste (non-IE) and onPaste (IE) makes a difference.

对于输入,有 onKeyDownonKeyUponKeyPress 事件。

希望这有帮助。

可能与SO相关的问题 IE onPaste事件不使用javascript HTML

I know for textarea you can capture on paste event using the onPaste event.

HTML:

<textarea id="textEditor" />

In JS:

var editor = document.getElementById("textEditor");

if (isIE /* determine this yourself */) {
   editor.onPaste = function() {

   }
} else {
   //Not IE
   editor.onpaste = function() {

   }
}

//The capitalisation of the onpaste (non-IE) and onPaste (IE) makes a difference.

As for typing, there's onKeyDown, onKeyUp, onKeyPress events.

Hope this helps.

Possible SO-related question IE onPaste event using javascript not HTML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文