首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hotkeys-js -如何解绑我的热键?

Hotkeys-js -如何解绑我的热键?
EN

Stack Overflow用户
提问于 2020-01-24 18:33:33
回答 1查看 331关注 0票数 0

我正在使用hotkeys-js和绑定Enter,它会触发提交函数并关闭对话框。在关闭时,我尝试解绑我的热键,但我的热键一直被绑定。我还尝试像handleHotkey: Function = event => hotkeys.unbind('Enter', this.handleHotKey) && this.handleSubmit()一样解除绑定

代码语言:javascript
复制
import React, { Component } from 'react'

export default class Basis extends Component {
  state = {
    open: true,
  }

  handleClose: Function = () => {
    this.setState({ open: false })
  }

  render() {
    const { open } = this.state

    return (
      <>
        {open && <Dialog onClose={this.handleClose}/>}
      </>
    )
  }
}


// @flow
import React, { Component } from 'react'
import hotkeys from 'hotkeys-js'

export default class Dialog extends Component {
  componentDidMount () {
    hotkeys('Enter', this.handleHotkeys)
  }

  componentWillUnmount () {
    hotkeys.unbind('Enter', this.handleHotkeys)
  }

  handleHotkey: Function = event => this.handleSubmit()

  handleSubmit: Function = () => {
    console.log(12)
  }

  render () {
    return (
      <>
        <button onClick={this.handleSubmit}>Submit</button>
      </>
    )
  }
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-24 18:50:55

可能组件无法卸载。尝试将其与onSubmit方法解除绑定

代码语言:javascript
复制
handleSubmit: Function = () => {
    console.log(12);
    hotkeys.unbind('Enter');
  }

但是如果你想在声明之后管理热键,最好使用setScope

代码语言:javascript
复制
setScope
Use the hotkeys.setScope method to set scope. There can only be one active scope besides 'all'. By default 'all' is always active.

// Define shortcuts with a scope
hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function(){
  console.log('do something');
});
hotkeys('o, enter', 'files', function(){ 
  console.log('do something else');
});

// Set the scope (only 'all' and 'issues' shortcuts will be honored)
hotkeys.setScope('issues'); // default scope is 'all'
getScope
Use the hotkeys.getScope method to get scope.

hotkeys.getScope();
deleteScope
Use the hotkeys.deleteScope method to delete a scope. This will also remove all associated hotkeys with it.

hotkeys.deleteScope('issues');
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59894674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档