react-hotkeys-hook

  1. pnpm add react-hotkeys-hook
  2. import { useHotkeys } from 'react-hotkeys-hook'
  3. export const App = () => {
  4. const [count, setCount] = useState(0)
  5. useHotkeys('ctrl+k', () => setCount(count + 1), [count])
  6. return (
  7. <p>
  8. Pressed {count} times.
  9. </p>
  10. )
  11. }

hotkeys-js

https://github.com/jaywcjlove/hotkeys-js
https://wangchujiang.com/hotkeys-js/

  1. pnpm add hotkeys-js
  2. <script src="https://unpkg.com/hotkeys-js/dist/hotkeys.min.js"></script>
  3. import hotkeys from 'hotkeys-js';
  4. hotkeys('f5', function(event, handler){
  5. // Prevent the default refresh event under WINDOWS system
  6. event.preventDefault()
  7. alert('you pressed F5!')
  8. });
  9. hotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){
  10. switch (handler.key) {
  11. case 'ctrl+a': alert('you pressed ctrl+a!');
  12. break;
  13. case 'ctrl+b': alert('you pressed ctrl+b!');
  14. break;
  15. case 'r': alert('you pressed r!');
  16. break;
  17. case 'f': alert('you pressed f!');
  18. break;
  19. default: alert(event);
  20. }
  21. });

image.png

快捷键案例

image.png