Event altKey, ctrlKey, shiftKey attribute Parsing

Source: Internet
Author: User

Event altKey, ctrlKey, shiftKey attribute Parsing

This article mainly analyzes the Event altKey, ctrlKey, and shiftKey attributes in detail. If you need a friend, you can refer to them and hope to help you.

Event. altKey

Function: checks whether the Alt key is held down when an event occurs.

 

Syntax: event. altKey

 

Value: true | false

 

Note:

 

If the altKey attribute is true, the Alt key is pressed and maintained when the event occurs. If the attribute is false, the Alt key is not pressed.

The altKey attribute can be used with the mouse or keyboard to create shortcuts.

 

Event. ctrlKey

Function: checks whether the Ctrl key is held down when an event occurs.

 

Syntax: event. ctrlKey

 

Value: true | false

 

Note:

 

If the ctrlKey attribute is true, the Ctrl key is pressed and maintained when the event occurs. If the value is false, the Ctrl key is not pressed.

The ctrlKey attribute can be used with the mouse or keyboard to create shortcuts.

 

Event. shiftKey

Function: checks whether the Shift key is held down when an event occurs.

 

Syntax: event. shiftKey

 

Value: true | false

 

Note:

 

If the shiftKey attribute is true, the Shift key is pressed and maintained when the event occurs. If it is false, the Shift key is not pressed.

The shiftKey attribute can be used with the mouse or keyboard. It is mostly used to create shortcuts.

 

Instance 1

Combined Operation example.

 

The Code is as follows:

<Input id = "txt1" type = "text" value = "Hello World! "Onclick =" checkAlt (event) "/>

 

<Script type = "text/javascript">

Function checkAlt (oEvent)

{

If (oEvent. altKey)

Document. getElementById ("txt1"). select ();

}

</Script>

 

 

The effect of this Code is:

 

If you press the Alt key and click the text box above, you can select the text in the text box.

 

Instance 2

Combined Operation example.

 

The Code is as follows:

<Input id = "txt2" type = "text" value = "Hello World! "Onclick =" clearText (event) "/>

 

<Script type = "text/javascript">

Function clearText (oEvent)

{

If (oEvent. ctrlKey & oEvent. keyCode = 46)

Document. getElementById ("txt2"). value = "";

}

</Script>

 

 

The effect of this Code is:

 

Use the Ctrl + Del key combination to clear the content of the text box above. (Focus must be obtained for the text box first. This example only applies to IE browsers .)

 

Instance 3

Combined Operation example.

 

The Code is as follows:

<Div id = "box" style = "width: 50px; height: 25px; border: 1px solid black; background-color: red" onclick = "setColor (event) "> </div>

 

<Script type = "text/javascript">

Var B = true;

Function setColor (oEvent)

{

If (oEvent. shiftKey & B)

Document. getElementById ("box"). style. backgroundColor = "blue ";

If (oEvent. shiftKey &&! B)

Document. getElementById ("box"). style. backgroundColor = "red ";

B =! B;

}

</Script>

 

 

The effect of this Code is:

 

Press and hold the "Shift" key and click the color block above to change the color block.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.