我使用 react-hotkeys 库。
我有这个:
class App extends React.Component {
...
render(){
return <Page />
};
}
class Page extends React.Component {
...
render(){
return <DataTable />
};
}
class DataTable extends React.Component {
...
shortCutsKeymap = {
'MOVE_NEXT_PAGE': ['right'],
'MOVE_PREV_PAGE': ['left']
};
handleShortCuts = {
'MOVE_NEXT_PAGE': (event) => {console.log('next')},
'MOVE_PREV_PAGE': (event) => {console.log('prev')},
};
render(){
return (
<HotKeys keyMap={this.shortCutsKeymap} handlers={this.handleShortCuts}>
...
</HotKeys>
);
};
}
快捷方式仅在我单击数据表时才起作用——也就是说,我设置了焦点。我需要键盘快捷键在我的应用程序中的任何位置工作,而无需单击数据表。
你能帮帮我吗?
最佳答案
我不确定您使用的是哪个版本的软件包。我安装的版本
react-hotkeys": "^2.0.0-pre3"
并且它有一个 GlobalHotKeys
组件,您可以像这样导出它:
import { GlobalHotKeys } from 'react-hotkeys'
你可以这样使用它:
<GlobalHotKeys keyMap={keyMap} handlers={handlers} />
关于reactjs - 如何全局使用键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647702/