3

How can i capture the following keys in Textbox using JavaScript?

Ctl + a

Ctl + c

Ctl + v

Following is the original Situation.

I have three Textboxes for Phones numbers. Textbox1 max length is 3 , 2nd's is 3 and 3rd is 4. When user types three digits in TextBox1 the cursor moves automatically to TextBox2 same thing happens with TextBox2 as well as TextBox3. I am handling this functionality in keyup event. Now, I am parallely using your code. But it moves in keyup event as well. This case happens when all TextBoxes are filled. Now suppose I am in TextBox1 and presses Ctl + A . This moves the user to third TextBox(unacceptable case). This is the issue.

Pankaj
  • 9,749
  • 32
  • 139
  • 283

3 Answers3

6

Use the select, copy and paste events respectively. Pretty much universally supported these days.

var textBox = document.getElementById("textBoxId");
textBox.onpaste = function() {
    alert("paste");
};

Likewise for the other events. Demo here: http://jsfiddle.net/timdown/EC2Hf/

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • can you please tell about ctl + A also? Thanks – Pankaj Apr 13 '12 at 11:53
  • @Pankaj: That's covered by the `select` event. I added a demo to my answer. – Tim Down Apr 13 '12 at 11:54
  • Thanks for the detailed link/Demo. +1 – Pankaj Apr 13 '12 at 12:02
  • This seems to make it fire twice – pbfy0 Apr 13 '12 at 12:03
  • @pbfy0 Can you explain it bit more please? – Pankaj Apr 13 '12 at 12:03
  • @Tim Down - can i set the focus also using e in the same event handler? – Pankaj Apr 13 '12 at 12:13
  • @PankajGarg: If you're getting any of these events then the text box must already have the focus. – Tim Down Apr 13 '12 at 12:30
  • @Tim, I want to pass "`this`" in the parameter list. Is it possible? Thanks – Pankaj Apr 13 '12 at 12:48
  • @Tim Down - Your post is failing in one scenario when you double click the text in textbox and control goes to the event handler. This should not be the case as i need to detect just ctl + A/C/V – Pankaj Apr 13 '12 at 13:16
  • @PankajGarg: Why do you just want the keyboard events? – Tim Down Apr 13 '12 at 13:18
  • @Tim - Let me explain you the complete situation first. I have three `Textboxes` for Phones numbers. `Textbox1` max length is 3 , 2nd's is 3 and 3rd is 4. When user types three digits in `textBox1` the cursor moves automatically to `TextBox2` same thing happens with `TextBox2` as well as `TextBox3`. I am handling this functionality in keyup event. Now, I am parallely using your code. But it moves in keyup event as well. This case happens when all TextBoxes are filled. Now suppose I am in TextBox1 and presses Ctl + A . This moves the user to third TextBox(unacceptable case). This is the issue. – Pankaj Apr 13 '12 at 13:27
  • @PankajGarg: Ah. I'd suggest using the `input` event instead then. See http://stackoverflow.com/a/5524996/96100 – Tim Down Apr 13 '12 at 14:01
  • @PankajGarg i get 2 dialog boxes when I copy or paste – pbfy0 Apr 15 '12 at 14:19
1

And what about right click, osx that does not use control, the edit copy option on the browser, the button on my old keyboard, etc?

There is more than just key presses.

That said, most browsers support

oncopy and onpaste events.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

You would have to first check if the ctrl button was clicked and then the correspnding letter keys. This link may help you out

Krishna
  • 636
  • 3
  • 8