This article describes how to block the default shortcut key and then execute a custom event. The following example uses enter in textarea to save the event, if you are interested, you can refer to how to shield more shortcut keys and search by google.
Here we will talk about how to block and then execute custom events.
Here, we use Kibo for ease of use as an example. The results searched by google are generally implemented in javascript native, which is very simple and will not be introduced here.
Here is an example of saving enter in a textarea to block the original carriage return event.
The Code is as follows:
The Code is as follows:
// Keyboard listener
Var areaKey = new Kibo ($ ("# aac010") [0]);
AreaKey. down ('enter', doSave );
Function doSave (){
Event. keyCode = 0;
Event. returnValue = false;
SetTimeout (save, 300 );
Return false;
}
To shield original js events, it is generally implemented by keyCode = 0, returnValue = false, and return false;. The key is how to call a custom method, as shown in the save method above, if you directly write the save method here, because it takes some time to execute save and false is not returned for a short time, the original event of enter will be triggered and cannot be blocked, therefore, setTimeout is used to call the custom method. Let the returun return in time so that the original event will not be triggered.
The specific reason is unclear. If someone has a better method or knows the reason, leave a message. Thank you ~~~