Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vue] vue如何监听键盘事件? #263

Open
haizhilin2013 opened this issue Jun 16, 2019 · 5 comments
Open

[vue] vue如何监听键盘事件? #263

haizhilin2013 opened this issue Jun 16, 2019 · 5 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

[vue] vue如何监听键盘事件?

@haizhilin2013 haizhilin2013 added the vue vue label Jun 16, 2019
@wenyejie
Copy link

那要看你怎么监听了, 比如
@keyup.enter,
或者直接全局监听

@CaoRuimiao
Copy link

回车 @keyup.enter.native

@canwdev
Copy link

canwdev commented Feb 23, 2020

  1. @keyup. 方法

    <template>
      <input ref="myInput" type="text" value="hello world" autofocus @keyup.enter="handleKey">
    </template>
    
    <script>
      export default {
        methods: {
          handleKey(e) {
            console.log(e)
          }
        }
      }
    </script>
  2. addEventListener

    <script>
      export default {
        mounted() {
          document.addEventListener('keyup', this.handleKey)
        },
        beforeDestroy() {
          document.removeEventListener('keyup', this.handleKey)
        },
        methods: {
          handleKey(e) {
            console.log(e)
          }
        }
      }
    </script>

@crush2020
Copy link

v-on:keyup后面接修饰符,代表你想监听的按键

@Cai-zhiji
Copy link

使用事件修饰符

@keydown.enter:监听 Enter 键按下事件。
@keydown.esc:监听 Esc 键按下事件。
@keydown.tab:监听 Tab 键按下事件。



# 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

6 participants