react-hotkeys-hook
pnpm add react-hotkeys-hook
import { useHotkeys } from 'react-hotkeys-hook'
export const App = () => {
const [count, setCount] = useState(0)
useHotkeys('ctrl+k', () => setCount(count + 1), [count])
return (
<p>
Pressed {count} times.
</p>
)
}
hotkeys-js
https://github.com/jaywcjlove/hotkeys-js
https://wangchujiang.com/hotkeys-js/
pnpm add hotkeys-js
<script src="https://unpkg.com/hotkeys-js/dist/hotkeys.min.js"></script>
import hotkeys from 'hotkeys-js';
hotkeys('f5', function(event, handler){
// Prevent the default refresh event under WINDOWS system
event.preventDefault()
alert('you pressed F5!')
});
hotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){
switch (handler.key) {
case 'ctrl+a': alert('you pressed ctrl+a!');
break;
case 'ctrl+b': alert('you pressed ctrl+b!');
break;
case 'r': alert('you pressed r!');
break;
case 'f': alert('you pressed f!');
break;
default: alert(event);
}
});