键盘事件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>键盘事件</title>
    <script src="../../vue.js"></script>
</head>

<body>
    <!-- 
        1.Vue中常用的按键别名:
            回车 =>Enter
            删除 =>delete(捕获“删除”和“退格”键)
            退出 =>esc
            空格 =>space
            换行 =>tab
            上=>up
            下=>down
            左=>left
            右=>right
        2.Vue未提供别名的按键,可以使用按键原始的key值去绑定 

        3.系统修饰键(用法特殊):ctrl、alt、shift、meta
            (1).配合keyup使用:按下修饰键的同时,再按下其他键,随后释放其他键,事件才会触发
            (2).配合keydown使用:正常触发事件
        4.也可以使用keyCode 去指定具体的按键(不推荐)
        5.Vue.config.keyCodes.自定义键名 = 键码,可以去定制按键别名
     -->
    <div id="root">
        <!-- 准备一个容器 -->
        <h1>学习{{name}}的一天</h1>
        <input type="text" @keyup.huiche='up' placeholder="按下回车提示输入">
    </div>
</body>
<script>
    Vue.config.productionTip = false;  // 阻止 vue启动是生产提示
    Vue.config.keyCodes.huiche = 13; //定义一个别名
    new Vue({
        el: '#root',
        data: {
            name: 'AAA',

        },
        methods: {
            up(e) {
                // if (e.keyCode != 13) return
                // console.log(e.key, e.keyCode)
                console.log(e.keyCode);
            }

        }
    })
</script>

</html>

 

posted @ 2022-10-09 23:28  小忱  阅读(35)  评论(0)    收藏  举报