首页 家电百科 实时讯息 常识

笔记本numpad是什么键? vue兼容Mac和Win按键功能

100次浏览     发布时间:2023-04-05 14:13:14    

shortcuts

大家或许会想到使用KeyCode键码,来进行判断,是的这是一种可行的方案。

但是需要同时兼容mac和win的话,就需要我们进行更多的优化。

今天给大家介绍一个成熟的库:https://github.com/fabiospampinato/shortcuts

安装

npm install --save shortcuts

用法

这个库提供了一个Shortcuts类,它将管理你的快捷键,和快捷键相关对象,并且还提供了一下实用的程序。

Shortcut 语法

供我们使用的按键分别有以下分类:

  • 工具键 Alt/Option, Cmd/Command/Meta, Ctrl/Control, Shift, CmdOrCtrl/CommandOrControl
  • 数字键 1-9
  • 字母表键 A-Z
  • 功能键 F1-F24
  • Numpad 数字键 Numpad0-Numpad9
  • 特殊键Backspace, Capslock, Del/Delete, Down, End, Enter/Return, Esc/Escape, Home, Insert, Left, PageDown, PageUp, Right, Space/Spacebar, Tab, Up.
  • 符号键 !, ", #, $, %, &, ', (, ), *, +/plus, ,, -,., /, :, ;, <, =, >, ?, @, [, \, ], ^, _ ,{, |, },~, `

使用时注意

  • 快捷键不区分大小写
  • 使用组合键的时候必须要加一个加号(eg:Ctrl+A)
  • 组合键序列必须加一个空格(eg:Ctrl+K Ctrl+B)

Shortcut 类

此工具类中分别有以下方法add/remove/reset/record.

class Shortcuts {  constructor ( options?: { shortcuts?: ShortcutDescriptor[]: capture?: boolean, target?: Node, shouldHandleEvent?: event => boolean } );  get (): ShortcutDescriptor[];  add ( descriptors: ShortcutDescriptor | ShortcutDescriptor[] );  remove ( descriptors: ShortcutDescriptor | ShortcutDescriptor[] );  reset ();  record ( handler: ( shortcut ) => any ): Function;}

用法

import {Shortcuts} from 'shortcuts';const shortcuts = new Shortcuts ();function CtrlBHandler () {};shortcuts.add ([ // Adding some shortcuts  { shortcut: 'Ctrl+A', handler: event => {    console.log ( event );    return true; // Returning true because we don't want other handlers for the same shortcut to be called later  }},  { shortcut: 'Ctrl+B', handler: CtrlBHandler },  { shortcut: 'CmdOrCtrl+K Shift+B', handler: () => {    // Doing something...    return true; // Returning true because we don't want other handlers for the same shortcut to be called later  }},  { shortcut: '-Ctrl+A' } // Removing the previous shortcut]);shortcuts.remove ({ shortcut: 'Ctrl-B', handler: CtrlBHandler }); // Removing a single handlershortcuts.remove ({ shortcut: 'Ctrl-A' }); // Removing all handlers bound to this shortcutshortcuts.reset (); // Removing all shortcutsconst dispose = shortcuts.record ( shortcut => { // Recording shortcuts  console.log ( 'Shortcut recorded:', shortcut );});dispose (); // Stopping recording

Shortcut 对象

它还提供了其他的应用程序:

const Shortcut = {  shortcut2id ( shortcut: string ): number[];  shortcut2accelerator ( shortcut: string ): string;  shortcut2symbols ( shortcut: string ): string;};

用法

import {Shortcut} from 'shortcuts';Shortcut.shortcut2accelerator ( 'Meta+Del' ); // => 'Cmd+Delete'Shortcut.shortcut2symbols ( 'Cmd+Shift+A' ); // => '⌘⇧A'

实例

如上图所示,主页面和弹窗内分别都有添加按钮。

我们可以通过判断弹窗是否弹出来区分相同的按钮来执行不同的功能。

代码

  1. main.js中引用shortcuts,将其作为全局的方法
import Vue from 'vue'import App from './App.vue'import {Shortcuts} from 'shortcuts';Vue.prototype.$shortcuts = new Shortcuts();Vue.config.productionTip = falsenew Vue({  render: h => h(App),}).$mount('#app')
  1. helloWorld.vue
<template>  <div >    <h1>{{ msg }}</h1>    <div >      <h3>按钮组合列表</h3>      <div>        <div>          <button @click="add">添加</button>          <span>CmdOrCtrl+A</span>        </div>        <div>          <button @click="del">删除</button>          <span>CmdOrCtrl+D</span>        </div>        <div>          <button @click="print">打印</button>          <span>CmdOrCtrl+P</span>        </div>        <div>          <button @click="functionKey">F1</button>          <span>F1</span>        </div>      </div>      <p >{{keyMsg}}</p>    </div>    <div >      <h3>弹窗内使用按键</h3>      <button @click="openPop">打开弹窗</button>      <div id="vModal" v-if="isShow">        <div ></div>        <div >          <p  @click="isShow = false">X</p>          <p>弹窗内按钮如何在关闭弹窗的时候禁用?!</p>                  <button @click="addPopbtn">弹窗内添加</button>          <button @click="dialogMsg=''">清空</button>          <p >{{dialogMsg}}</p>        </div>      </div>    </div>      </div></template><script>export default {  name: 'HelloWorld',  props: {    msg: String  },  data(){    return{      keyMsg:'',      dialogMsg:'',      isShow:false    }  },  created(){    this.keycodeEvent()  },  methods:{    add(){      this.keyMsg='添加'      console.log('添加')    },    addPopbtn(){      this.dialogMsg='弹窗内添加按钮'    },    openPop(){      this.dialogMsg=''      this.isShow=true    },    del(){      this.keyMsg='删除'      console.log('删除')    },        print(){      this.keyMsg='打印'      console.log('打印')    },    functionKey(){      this.keyMsg='F1'      console.log('F1')    },    // 添加热键    keycodeEvent() {        this.$shortcuts.add([ // Adding some shortcuts             {                shortcut: 'cmdorctrl+A',//支持大小写                handler: event => {                  console.log ( event );                 if(document.querySelector('#vModal')){//如果弹窗出现,按键触发弹窗内的方法                    this.addPopbtn()                 }else{                    this.add()                 }                  return true;                }            },  {                shortcut: 'CmdOrCtrl+D',                handler: event => {                  console.log ( event );                  this.del()                  return true;                }            }, {                shortcut: 'CmdOrCtrl+P',                handler: event => {                  // 打印                  console.log ( event );                  this.print()                  return true;                }            },            {                shortcut: 'F1',                handler: event => {                  console.log ( event );                  this.functionKey()                  return true;                }            },        ]);    }  }}</script>
相关文章
  1. 穆塞蒂因伤退赛 阿尔卡拉斯晋级法网男单决赛

    新华社巴黎6月6日电 6日进行的2025法国网球公开赛首场男单半决赛中,8号种子、意大利选手穆塞蒂在第四盘因伤退赛,卫冕冠军、西班牙名将阿尔卡拉斯直接晋级男单决赛。首盘比赛,阿尔卡拉斯在第十局先被破发,以4:6先丢一盘。第二盘,双方互破两局进入“抢七”,阿尔卡拉斯依靠强大的跑动能力屡次化解穆塞蒂凶猛

    0 2025-06-07 00:07:00

  2. 金佰利燃气灶维修热线24小时咨询服务热线

    我是金佰利燃气灶的客服小金,非常感谢您对金佰利燃气灶的信任与支持。关于您提到的金佰利燃气灶产品相关故障的问题,请您不要担心,我会从以下几个方面为您详细解答。请您确认是否按照说明书正确使用产品。400-883-2086是我们的售后服务电话,如果在使用过程中遇到任何疑问,都可以随时拨打该电话进行

    100 2025-06-06 23:57:57

  3. 威士伯集成灶全国售后热线及常见问题解答

    威士伯集成灶售后服务维修电话:400-883-2086威士伯集成灶24小时维修客服热线:400-883-2086威士伯集成灶24小时服务电话《今日发布》威士伯集成灶附近上门服务电话7天24小时人工电话客服

    100 2025-06-06 23:54:58

  4. 方太煤气灶全国统一24小时售后服务热线号码

    方太煤气灶的维修电话可能因地区和具体服务内容而有所不同,但通常,用户可以通过拨打方太煤气灶的客服热线来获取维修服务。以下是一些可供参考的电话号码:方太煤气灶客服热线:400-883-2086方太煤气灶另一客服热线:400-883-2086这些电话号码通常提供24小时服

    100 2025-06-06 23:51:59

  5. 威猛达燃气灶售后服务部热线

    威猛达燃气灶维修服务热线:400-883-2086威猛达燃气灶维修服务热线:400-883-2086维修时,请务必遵循以下重要提示以确保服务顺利进行:在联系维修服务前,请确保您的设备已关闭,并确保安全。准备好相关产品信息,如型号、序列号等,以便维修人员能更快地了解您的需求。遵循维修人

    100 2025-06-06 23:48:06