Set the principle of the shortcut key for Web pages

Source: Internet
Author: User
Tags copy key window
Web page

In many Web pages, you can use shortcuts to complete certain actions, such as the Discuz forum "after completion can press Ctrl+enter release" Function!

This function is done using the KeyCode method of the event attribute in JavaScript, using the onkeydown event to drive.
In JavaScript, you can pass the KeyCode property to the Asscii code value that the user presses. The specific corresponding relation may check the ASSCII code table.
Example:

CODE: [Copy to Clipboard]
--------------------------------------------------------------------------------

<script language= "JavaScript" >
<!--
Function Hotkey ()
{
var A=window.event.keycode;
if (a==65)
{
Alert ("You press a key");
}
}//End Hotkey

Document.onkeydown = Hotkey; Call hotkey function when onkeydown event occurs
-->
</SCRIPT>


In addition, in the use of shortcut keys, ALT, CTRL, SHIFT, and other auxiliary keys are often used, can be altkey, Ctrlkey, shiftkey these three properties to determine the ALT, CTRL, SHIFT key state
For example, the value of Event.ctrlkey is true, which means that the user presses the CTRL key.
Example:

CODE: [Copy to Clipboard]
--------------------------------------------------------------------------------

<script language= "JavaScript" >
<!--
Function Hotkey ()
{

if (Event.ctrlkey)
{
Alert ("You pressed the CTRL key");
}
}//End Hotkey

Document.onkeydown = Hotkey; Call hotkey function when onkeydown event occurs
-->
</SCRIPT>


Can be combined to form a real shortcut key, the execution of the statement for you want the effect on it.

CODE: [Copy to Clipboard]
--------------------------------------------------------------------------------

<script language= "JavaScript" >
<!--
Function Hotkey ()
{
var A=window.event.keycode;
if ((a==65) && (Event.ctrlkey)
{
Alert ("You press CTRL + a key");
}
}//End Hotkey

Document.onkeydown = Hotkey; Call hotkey function when onkeydown event occurs
-->
</SCRIPT>


Document.onkeydown = Hotkey statement is in the full page, when the user presses the key, call the hotkey function, perhaps you want to implement shortcut key function in the page, then, you can imitate the following do (remember to delete "Document.onkeydown = Hotkey "statement):


CODE: [Copy to Clipboard]
--------------------------------------------------------------------------------

<textarea rows= "7" cols= "name=" "Message" tabindex= "2" ></textarea>

In this way, you can use shortcut keys only if the cursor focus is in a text box.



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.