禁用ctrl+win+d_vue兼容Mac和Win按键功能

5be21df39d5dcb9751dae95f0046ae4e.png

作者:小布

转发链接:https://mp.weixin.qq.com/s/NcosSIx6Nuyx5W-HHwtA-w

前言

想必大家在工作中会遇到类似的问题,领导需要我们给某些特定的按钮加上快捷键的功能。

比如,Ctrl+S/Command+S保存表单内容、Ctrl+P/Command+P打印文件、Ctrl+E/Command+E编辑等等。

前段时间小编也发布过一篇Vue项目给应用优雅的绑定快捷键,有兴趣的小伙们可以看看。


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'

实例

4e22b838d663b977057dd7b4406e040e.png

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

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

代码

  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
  
    

{{ msg }}

    
      

按钮组合列表

      
        
          添加          CmdOrCtrl+A        
        
          删除          CmdOrCtrl+D        
        
          打印          CmdOrCtrl+P        
        
          F1          F1        
      
      

{{keyMsg}}

    
    
      

弹窗内使用按键

      打开弹窗      
        
          

X

          

弹窗内按钮如何在关闭弹窗的时候禁用?!

                  弹窗内添加          清空          

{{dialogMsg}}

        
      
    
      

demo完整地址

https://github.com/yzren/key-shortcut/

作者:小布

转发链接:https://mp.weixin.qq.com/s/NcosSIx6Nuyx5W-HHwtA-w

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中实现浏览器的 `ctrl+f` 查找功能,可以通过监听键盘事件来实现。 首先,在需要监听键盘事件的组件中,添加 `@keydown.ctrl.f` 监听事件,例如: ```html <template> <div> <input v-model="searchText" placeholder="搜索"> <ul> <li v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> </div> </template> <script> export default { data() { return { searchText: '', list: ['apple', 'banana', 'orange', 'pear'] } }, methods: { handleKeyDown(event) { if (event.ctrlKey && event.keyCode === 70) { // 当按下 ctrl+f 时 event.preventDefault(); // 阻止默认事件 this.$refs.searchInput.focus(); // 让输入框获取焦点 } } }, mounted() { this.$refs.searchInput.addEventListener('keydown', this.handleKeyDown); // 监听键盘事件 }, beforeDestroy() { this.$refs.searchInput.removeEventListener('keydown', this.handleKeyDown); // 移除键盘事件监听 } } </script> ``` 上述代码中,我们在 `mounted` 钩子函数中监听了键盘事件,并在 `beforeDestroy` 钩子函数中移除了键盘事件监听,以防止内存泄漏。 在 `handleKeyDown` 方法中,我们判断是否按下了 `ctrl+f` 快捷键,如果是,则阻止默认事件,并让输入框获取焦点,以便用户可以直接在输入框中输入要查找的内容。 需要注意的是,在监听键盘事件时,我们使用了 `addEventListener` 和 `removeEventListener` 方法来添加和移除事件监听,而不是在模板中直接绑定键盘事件。这是因为在 Vue 中,模板中绑定的事件监听会在组件销毁时自动移除,但是在本例中,我们需要手动移除键盘事件监听,以防止内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值