项目作者: patrikmasiar

项目描述 :
Container and hook with keyboard event listener.
高级语言: JavaScript
项目地址: git://github.com/patrikmasiar/react-keyboard-key.git
创建时间: 2021-03-11T19:29:09Z
项目社区:https://github.com/patrikmasiar/react-keyboard-key

开源协议:MIT License

下载


npm
NPM
NPM

react-keyboard-key

Container and hook with keyboard event listener.

Usage

Types

  1. keyInfo: {
  2. key: string | null;
  3. code: string | null;
  4. keyCode: number | null;
  5. output: KeyboardEvent | null,
  6. };
  7. addKeyListener: () => void;
  8. removeEventListener: () => void;
  9. onKeyDownCallback: (event: KeyboardEvent) => void;

Container

  1. import { KeyListenerContainer } from 'react-keyboard-key';
  2. ...
  3. const ExampleComponent: FC<Props> = ({ children }) => {
  4. return (
  5. <KeyListenerContainer
  6. onKeyDownCallback={e => {
  7. e.preventDefault();
  8. }}
  9. >
  10. {keyInfo => (
  11. <div>
  12. {keyInfo.code}
  13. </div>
  14. )}
  15. </KeyListenerContainer>
  16. );
  17. };

Hook

  1. import { useKeyboardKey } from 'react-keyboard-key';
  2. const ExampleComponent: FC<Props> = () => {
  3. const { keyInfo } = useKeyboardKey(onKeyDownCallback);
  4. const onKeyDownCallback = (e: KeyboardEvent) => {
  5. e.preventDefault
  6. };
  7. return (
  8. <div>
  9. {keyInfo.code}
  10. </div>
  11. );
  12. };

Project