Summary of Vue's monitoring of keyboard events

Summary of Vue's monitoring of keyboard events

Key Modifiers

When 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:

  • -enter
  • - tab
  • - delete (captures the "Delete" and "Backspace" keys)
  • -esc
  • - space
  • - up
  • - down
  • -left
  • - right

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 keys

The following modifiers can be used to implement a listener that triggers mouse or keyboard events only when the corresponding key is pressed.

  • - .ctrl
  • - .alt
  • - .shift
  • - .meta

Do something
Unlike key aliases, when modifier keys are used with the keyup event, a normal key must be pressed when the event is triggered. In other words: if you want keyup.ctrl to be triggered, you must release other keys while pressing ctrl; simply releasing ctrl will not trigger the event.

<!-- 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 Modifier

The .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

  • .left
  • .right
  • .middle

These modifiers restrict the handler function to respond only to specific mouse buttons.

System key combinations

If 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 table

The 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:
  • Vue project soft keyboard enter triggers search event
  • Example explanation of Vue keyboard enter event
  • Vue quick way to listen to keyboard events [recommended]
  • Summary of Vue keyboard event usage
  • Detailed explanation of Vue's keyboard events

<<:  A brief analysis of Docker private image library and Alibaba Cloud object storage OSS

>>:  MySQL 5.7.17 installation and use graphic tutorial

Recommend

In-depth analysis of MySQL database transactions and locks

Table of contents 1. Basic Concepts ACID 3.AutoCo...

Solution to the conflict between nginx and backend port

question: When developing the Alice management sy...

Use js to write a simple snake game

This article shares the specific code of a simple...

Solution to HTML2 canvas SVG not being recognized

There is a new feature that requires capturing a ...

How to implement HTML Table blank cell completion

When I first taught myself web development, there...

Understanding v-bind in vue

Table of contents 1. Analysis of key source code ...

MySQL 5.6.24 (binary) automatic installation script under Linux

This article shares the mysql5.6.24 automatic ins...

MySQL uses SQL statements to modify table names

In MySQL, you can use the SQL statement rename ta...

How to display JSON data in HTML

background: Sometimes we need to display json dat...

Summary of using MySQL online DDL gh-ost

background: As a DBA, most of the DDL changes of ...

Mysql optimization Zabbix partition optimization

The biggest bottleneck of using zabbix is ​​the d...

How to store text and pictures in MySQL

Large Text Data Types in Oracle Clob long text ty...

How to use docker to deploy spring boot and connect to skywalking

Table of contents 1. Overview 1. Introduction to ...

How to check where the metadata lock is blocked in MySQL

How to check where the metadata lock is blocked i...