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.

8

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

13 Comments

can you please tell about ctl + A also? Thanks
@Pankaj: That's covered by the select event. I added a demo to my answer.
Thanks for the detailed link/Demo. +1
This seems to make it fire twice
@pbfy0 Can you explain it bit more please?
|
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.