How to detect a Paste action on a textbox?

D

dracognizer

Is this possible with Javascript in anyway? :)

Yes, it is possible to detect a paste action.
Here's a simple exemple, preventing to paste in a textarea :

<html>
<head>
<script>
window.addEventListener('load', function (e)
{
document.getElementById('text').addEventListener('keydown', function
(e)
{
if (e.keyCode == 86) //86 is Paste event
{
alert('You may not paste here !');
e.preventDefault();
}
document.getElementById('events').innerHTML += e.keyCode + ' ' +
e.which + '<br />';
}, false);
}, false);
</script>
</head>

<body>
<textarea id="text">
</textarea>
<div id="events">
</div>
</body>
</html>
 
A

ASM

(e-mail address removed) a écrit :
Yes, it is possible to detect a paste action.
Here's a simple exemple, preventing to paste in a textarea :

How do you expect to tape 'v' ? or 'V' ?

Anyway,
that doesn't work at all !

report under textbox tells me : 224 224
 
D

dracognizer

(e-mail address removed) a écrit :



Anyway,
that doesn't work at all !

report under textbox tells me : 224 224

Oups... You're totally rights... >_<
I'm sorry i wasn't careful enough about that...

I'm going to see if I can do something about it :p

P.S. Ton mac est si vieux ?
 
D

dracognizer

I'm sorry i wasn't careful enough about that...

I'm going to see if I can do something about it :p

P.S. Ton mac est si vieux ?

<script>
window.addEventListener('load', function (e)
{
document.getElementById('text').addEventListener('keydown', function
(e)
{
if (e.keyCode == 86 && e.ctrlKey) //86 is 'v' char key
{
e.preventDefault();
}
document.getElementById('events').innerHTML += e.keyCode + ' ' +
e.which + '<br />';
}, false);
}, false);
</script>

Here it is.
I forgot to put the "e.ctrlKey" check ( to check wether control key was
pressed, or not.

Thing is I don't have any way to try javascript on Safari ( as there's
no JavaScriptCore port on Linux, apart from KHTML, which begins to
differs a lot from WebCore... ).
What is 224 ? Ctrl+V keycode, or V keycode ?

Is the e.ctrlKey working with safari ? Since I know you have to "pomme
+ v" to paste ( sorry I don't remember the english equivalent for
'pomme' [ I just know it's not " Apple " :p ] ).
 
A

ASM

(e-mail address removed) a écrit :
Oups... You're totally rights... >_<
I'm going to see if I can do something about it :p
P.S. Ton mac est si vieux ?

The so noisy G4 MDD (1,25Mhz OS 10.3.9) 2 years old.

P.S. : qu'est-ce qu'il a le Messenger de ton Mozilla ?
(pour ne pas gommer la signature de l'envoyeur dans les réponses)
 
N

naixn

ASM wrote :
(e-mail address removed) a écrit :



The so noisy G4 MDD (1,25Mhz OS 10.3.9) 2 years old.

P.S. : qu'est-ce qu'il a le Messenger de ton Mozilla ?
(pour ne pas gommer la signature de l'envoyeur dans les réponses)

In fact, it's an old one!

Please refer to my previous post to check the changes I made.
Maybe you'll also be able to answer my safari-specific questions :)

P.S. : Tout simplement que j'utilisais google newsgroups.
C'est maintenant corrigé :)
 
A

ASM

(e-mail address removed) a écrit :
Here it is.
I forgot to put the "e.ctrlKey" check ( to check wether control key was
pressed, or not.

On Mac Control key is not used to copy/paste/etc
we use touch Command (Pomme).

With FireFox, with Command + v and Command + z
keyCode returns 224
Command associated with others touchs (x, a, p, ... ) keyCode returns
correspondant character/touch key code.
Thing is I don't have any way to try javascript on Safari

My tests show Safari doesn't report touch Command (Pomme - Apple)
nor keyCode as if anything was pressed.

My test with Opera : Opera doesn't give a special code for
Command+v Command+c (normal letter key code)
 
N

naixn

ASM wrote :
(e-mail address removed) a écrit :

On Mac Control key is not used to copy/paste/etc
we use touch Command (Pomme).

With FireFox, with Command + v and Command + z
keyCode returns 224
Command associated with others touchs (x, a, p, ... ) keyCode returns
correspondant character/touch key code.


My tests show Safari doesn't report touch Command (Pomme - Apple)
nor keyCode as if anything was pressed.

My test with Opera : Opera doesn't give a special code for
Command+v Command+c (normal letter key code)

Well, let's correct it :

<script>
window.addEventListener('load', function (e)
{
document.getElementById('text').addEventListener('keydown', function
(e)
{
if ((e.keyCode == 86 && e.ctrlKey) || (e.keyCode == 224 && e.metaKey)
{
e.preventDefault();
}
document.getElementById('events').innerHTML += e.keyCode + ' ' +
e.which + '<br />';
}, false);
}, false);
</script>

Ok now, how does it work with you, with this correction ? :)
 
R

Randy Webb

(e-mail address removed) said the following on 12/2/2006 6:09 AM:
Yes, it is possible to detect a paste action.
Here's a simple exemple, preventing to paste in a textarea :

No it doesn't. Open your test page, put the cursor in the textarea, and
press the key combination of <Shift><Insert>
 
N

naixn

Randy Webb wrote :
(e-mail address removed) said the following on 12/2/2006 6:09 AM:

No it doesn't. Open your test page, put the cursor in the textarea, and
press the key combination of <Shift><Insert>

In fact... too bad there's no onPaste event ( apart from IE I mean ).
Plus, right-click and paste doesn't work either.

I don't know, then, how to prevent/handle paste in (Gecko/Presto/Webcore)-based
browsers?
 
A

applegreenss

<html>
<head>
<script>
window.addEventListener('load', function (e)
{
document.getElementById('text').addEventListener('keydown', function
(e)
{
if (e.keyCode == 86) //86 is Paste event
{
alert('You may not paste here !');
e.preventDefault();
}
document.getElementById('events').innerHTML += e.keyCode + ' ' +
e.which + '<br />';
}, false);
}, false);

</script>
</head>

<body>
<textarea id="text">
</textarea>
<div id="events">
</div>
</body>
</html>

When I cut-and-paste this and run on IE, I get a 'object does not
support this property or metod'. Is something getting cut-off in my
code above?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top