Javascript 热键js-如何解除热键绑定?
我正在使用热键js和bindJavascript 热键js-如何解除热键绑定?,javascript,reactjs,hotkeys,Javascript,Reactjs,Hotkeys,我正在使用热键js和bindEnter,这会触发提交功能并关闭对话框。关闭时,我尝试解除热键的绑定,但热键一直处于绑定状态。我还尝试像handleHotkey:Function=event=>hotkeys.unbind('Enter',this.handleHotkey)&&this.handleSubmit() import React,{Component}来自“React” 导出默认类基扩展组件{ 状态={ 开放:是的, } handleClose:函数=()=>{ this.setSt
Enter
,这会触发提交功能并关闭对话框。关闭时,我尝试解除热键的绑定,但热键一直处于绑定状态。我还尝试像handleHotkey:Function=event=>hotkeys.unbind('Enter',this.handleHotkey)&&this.handleSubmit()
import React,{Component}来自“React”
导出默认类基扩展组件{
状态={
开放:是的,
}
handleClose:函数=()=>{
this.setState({open:false})
}
render(){
const{open}=this.state
返回(
{打开&&}
)
}
}
//@flow
从“React”导入React,{Component}
从“热键js”导入热键
“导出默认类”对话框扩展组件{
组件安装(){
热键('Enter',this.handle热键)
}
组件将卸载(){
hotkeys.unbind('Enter',this.handleHotkeys)
}
handleHotkey:Function=event=>this.handleSubmit()
handleSubmit:函数=()=>{
控制台日志(12)
}
渲染(){
返回(
提交
)
}
}
可能组件不会卸载。尝试使用onSubmit方法解除绑定
handleSubmit: Function = () => {
console.log(12);
hotkeys.unbind('Enter');
}
但如果您想在声明后管理热键,最好使用setScope
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');
我想您可能必须正确绑定此.handle热键。经典
此范围界定问题。或者,您是否尝试过只使用.unbind('enter')代码>?由于您不使用作用域,您可以全局解除绑定,而不必担心函数的引用是否正确。@如果我使用热键解除绑定,请犹豫不决。解除绑定('Enter')
也不起作用。请尝试设置正确的作用域。由于这是对类的反应,请尝试this.handleHotkeys=this.handleHotkeys.bind(this)在构造函数中使用code>来保留此引用。(与this.handleSubmit相同)这样您就可以将它同时传递给hotkeys()
和unbind()
作为this.handleHotkeys
。如果您在本地绑定它们,最终会得到两个函数,而不是一个。或者使用更多的箭头函数。@犹豫不决地绑定此函数也不起作用。祝您在文档中好运。这两个是我唯一的想法。
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');