JS Monitor keyboard two key combination trigger

Source: Internet
Author: User

* The callback for the keyboard event is the Document.onkeydown function, which passes in an event type parameter
Document.onkeydown = function (e) {    //write callback function when event is triggered}

For details of the *evnet event, please see the document, which looks like this:

Specific URL:

Http://www.w3school.com.cn/jsref/dom_obj_event.asp

* Example Application

Requirement: Trigger Savecomment method When Ctrl+enter key is pressed

The code is:

Document.onkeydown = function (e) {//console.log (E.ctrlkey); if (= = E.keycode && e.ctrlkey) {//console.log (C1) ; Savecomment ();}}

Analysis: When CTRL is pressed, the method is triggered, the CTRL key is not released, and the method is triggered again by pressing the ENTER key.

Conjecture: 1) The internal implementation of E must have an array to store the key ID, which means that even if there are 10 buttons pressed at the same time, it can be recognized simultaneously.

At the same time, the properties of each button's object have a property that is pressed if it is pressed to true.

2) at the same time, pressing CTRL and then pressing ENTER does not simply call the onkeydown function again, but instead saves the last event that OnKeyDown has not completed (Ctrl-key pressed).

For specific practical applications, please see my article:

Welcome comments, Guidance.

JS Monitor keyboard two key combination trigger

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.