Web JS to obtain the window button action (press Shift, Alt, Ctrl ....)

Source: Internet
Author: User

How to obtain

It is basically determined from the Action event.

 


Processing in IE
In IE, the window object contains the event. See the example below:
[Html]
<! -- Add by oscar999 -->
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Author" CONTENT = "oscar999">
<Script>
Function showWinEvent ()
{
If (window. event. altKey)
{
Alert ("you pressed Alt Key ");
} Else if (window. event. ctrlKey)
{
Alert ("you pressed Ctrl Key ");
} Else if (window. event. shiftKey)
{
Alert ("you pressed Shift Key ");
} Else {
// Alert (window. event. button)
}
}
</Script>
</HEAD>
 
<BODY>
<Input onclick = "showWinEvent ()" type = "button" value = "click me">
</BODY>
</HTML>

<! -- Add by oscar999 -->
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Author" CONTENT = "oscar999">
<Script>
Function showWinEvent ()
{
If (window. event. altKey)
{
Alert ("you pressed Alt Key ");
} Else if (window. event. ctrlKey)
{
Alert ("you pressed Ctrl Key ");
} Else if (window. event. shiftKey)
{
Alert ("you pressed Shift Key ");
} Else {
// Alert (window. event. button)
}
}
</Script>
</HEAD>

<BODY>
<Input onclick = "showWinEvent ()" type = "button" value = "click me">
</BODY>
</HTML>

The above work can be performed in IE and Chrome, but there is a problem in firefox, the undefined error will be reported, because the window object of firefox does not contain event.

 


Firefox compatibility


[Html]
<! -- Add by oscar999 -->
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Author" CONTENT = "oscar999">
<Script>
Function showWinEvent (evn)
{
If (evn. altKey)
{
Alert ("you pressed Alt Key ");
} Else if (evn. ctrlKey)
{
Alert ("you pressed Ctrl Key ");
} Else if (evn. shiftKey)
{
Alert ("you pressed Shift Key ");
} Else {
// Alert (window. event. button)
}
}
</Script>
</HEAD>
 
<BODY>
<Input onclick = "showWinEvent (event)" type = "button" value = "click me">
</BODY>
</HTML>

<! -- Add by oscar999 -->
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Author" CONTENT = "oscar999">
<Script>
Function showWinEvent (evn)
{
If (evn. altKey)
{
Alert ("you pressed Alt Key ");
} Else if (evn. ctrlKey)
{
Alert ("you pressed Ctrl Key ");
} Else if (evn. shiftKey)
{
Alert ("you pressed Shift Key ");
} Else {
// Alert (window. event. button)
}
}
</Script>
</HEAD>

<BODY>
<Input onclick = "showWinEvent (event)" type = "button" value = "click me">
</BODY>
</HTML>

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.