Key ModifiersWhen listening for keyboard events, we often need to check for detailed keys. Vue allows you to add key modifiers to v-on when listening to keyboard events: <!-- Call `vm.submit()` only if `key` is `Enter` --> <input v-on:keyup.enter="submit"> You can simply convert any valid key name exposed by KeyboardEvent.key to kebab-case to use as a modifier. <input v-on:keyup.page-down="onPageDown"> To support older browsers when necessary, Vue provides aliases for most common key codes:
You can also customize key modifier aliases via the global config.keyCodes object: // You can use `v-on:keyup.f1` Vue.config.keyCodes.f1 = 112 System modifier keysThe following modifiers can be used to implement a listener that triggers mouse or keyboard events only when the corresponding key is pressed.
Do something <!-- Press Alt + Release C to trigger --> <input @keyup.alt.67="clear"> <!-- Triggered by pressing Alt + releasing any key--> <input @keyup.alt="other"><!-- Triggered when pressing Ctrl + enter--><input @keydown.ctrl.13="submit"> For elementUI input, we need to add .native at the end, because elementUI encapsulates the input and native events do not work. <input v-model="form.name" placeholder="nickname" @keyup.enter="submit"> <el-input v-model="form.name" placeholder="nickname" @keyup.enter.native="submit"></el-input> .exact ModifierThe .exact modifier allows you to control which events are triggered by a precise combination of system modifiers. <!-- This will fire even if Alt or Shift is pressed together --> <button v-on:click.ctrl="onClick">A</button> <!-- Triggered only when Ctrl is pressed--> <button v-on:click.ctrl.exact="onCtrlClick">A</button> <!-- Triggered only when no system modifier is pressed--> <button v-on:click.exact="onClick">A</button> Mouse Button Modifiers
These modifiers restrict the handler function to respond only to specific mouse buttons. System key combinationsIf we want to monitor the global key operation method, obviously, it is not possible to bind it to the page element. We can monitor in mounted: mounted() { document.onkeydown = function (event) { let key = window.event.keyCode; if (key === 65 && event.ctrlKey) { // Listen for the ctrl+A key combination window.event.preventDefault(); // Turn off the browser's default shortcut key console.log('crtl+ a key combination') } else if(key === 83 && event.ctrlKey) { window.event.preventDefault(); //Close the browser shortcut key console.log('Save'); } } } From the above examples, we can see that shift, control, and Alt can also be replaced by "window.event.shiftKey", "window.event.ctrlKey", and "window.event.altKey" in JS. Appendix - Keyboard button keyCode tableThe above is the detailed summary of Vue's monitoring of keyboard events. For more information about Vue's monitoring of keyboard events, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: A brief analysis of Docker private image library and Alibaba Cloud object storage OSS
>>: MySQL 5.7.17 installation and use graphic tutorial
Achieve results Implementation Code html <div ...
Today, it suddenly occurred to me that it would be...
In some scenarios, we want to assign multiple IPs...
Before talking about CSS priority, we need to und...
Table of contents Query Background 1. Like query ...
Table of contents Opening scene Direct rendering ...
1. Please download the Busybox source code online...
The explain command is the primary way to see how...
Copy code The code is as follows: <html> &l...
Preface: Integer is one of the most commonly used...
The default ssh remote port in Linux is 22. Somet...
BFC BFC: Block Formatting Context BFC layout rule...
MySQL8.0.22 installation and configuration (super...
When installing mha4mysql, the steps are roughly:...
I remember a question the interviewer asked durin...