Git Product home page Git Product logo

use-tinykeys's Introduction

use-tinykeys

Vue composition API for listen keyboard event.

install

npm install @yafh/use-tinykeys

or usage yarn

yarn add @yafh/use-tinykeys

usage

import { useTinykeys } from '@yafh/use-tinykeys'
import { defineComponent } from 'vue'

export default defineComponent({
  setup() {
    // 监听单个键盘值
    const { enable } = useKeyStroke('Control+s', (event) => {
      console.log(event)
    })

    // 监听多个键盘值
    const { enable } = useKeyStroke(
      ['Control+s', 'Control+Shift+s'],
      (event) => {
        console.log(event)
      }
    )

    // 同时监听多个键盘事件
    const { enable } = useKeyStroke(
      'Control+s',
      (event) => {
        console.log(event)
      },
      {
        eventType: ['keydown', 'keyup'],
      }
    )

    // 分别监听多个键盘事件
    const { enable } = useKeyStroke('Control+s', {
      keydown: (event) => {
        console.log(event)
      },
      keyup: (event) => {
        console.log(event)
      },
    })

    // 针对不同平台监听不同键盘值
    const { enable } = useKeyStroke(
      {
        windows: 'Control+s',
        mac: ['Command+s', 'Command+Shift+s'],
      },
      (event) => {
        console.log(event)
      }
    )

    // 更换监听目标
    const { enable } = useKeyStroke(
      'Control+s',
      (event) => {
        console.log(event)
      },
      {
        target: document.body,
      }
    )
  },
})

tips

按键 key 说明:

  1. 'a' - 按下 a 键
  2. 'Control+a' - 按下 ctrl+a 键
  3. 'a b c d' - 依次按下 a、b、c、d 键

预设键值点击查看

options

target

需要监听的DOM元素

  • type Window | HTMLElement
  • default: window

eventType

监听的事件类型

  • type 'keydown' | 'keyup' or ('keydown' | 'keyup')[]
  • default: 'keydown'

timeout

连键超时时间(毫秒)

  • type number
  • default: 300

listenOnkeepAlive

在组件不激活时是否仍然监听键盘事件

  • type boolean

use-tinykeys's People

Contributors

yanandfish avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.