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
Table of contents 1. Basic Concepts ACID 3.AutoCo...
question: When developing the Alice management sy...
This article shares the specific code of a simple...
There is a new feature that requires capturing a ...
When I first taught myself web development, there...
Table of contents 1. Analysis of key source code ...
This article shares the mysql5.6.24 automatic ins...
In MySQL, you can use the SQL statement rename ta...
background: Sometimes we need to display json dat...
background: As a DBA, most of the DDL changes of ...
The biggest bottleneck of using zabbix is the d...
Large Text Data Types in Oracle Clob long text ty...
Table of contents 1. Overview 1. Introduction to ...
How to check where the metadata lock is blocked i...
This article describes the Linux user and group c...