uiwjs / react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
https://uiwjs.github.io/react-codemirror/
MIT License
1.69k stars 134 forks source link

About recording key down event. #582

Open Rexime opened 1 year ago

Rexime commented 1 year ago

Is there any way to record every key-down event when using the editor in my webapp?

jaywcjlove commented 1 year ago

@Rexime https://uiwjs.github.io/react-codemirror/#/extensions/events

import CodeMirror from '@uiw/react-codemirror';
import { events } from '@uiw/codemirror-extensions-events';

function App() {
  const [scrollTop, setScrollTop] = useState(0);

  const eventExt = events.scroll({
    scroll: (evn) => {
      setScrollTop(evn.target.scrollTop);
    },
  });

  const eventExt2 = events.content({
    focus: (evn) => {
      console.log('focus');
    },
    blur: (evn) => {
      console.log('blur');
    },
  });

  return <CodeMirror value="console.log('hello world!');" height="200px" extensions={[eventExt, eventExt2]} />;
}
export default App;
Rexime commented 1 year ago

Thanks a lot!!

Rexime commented 1 year ago

@jaywcjlove I wanted to save every key code event, but I did not know how to do it. The example you provided was not for that purpose. Is there any example of that? Thank you so much!

jaywcjlove commented 1 year ago

@Rexime I'm not sure what you need https://github.com/uiwjs/react-codemirror/blob/24285ded13298cf5e283b6b01994542b3c46bba0/core/src/index.tsx#L57-L58

Rexime commented 1 year ago

@jaywcjlove I need to collect this kind of info https://www.toptal.com/developers/keycode. Like everytime I press a key, it will be recorded. I'm not sure if it's possible for the codemirror.

jaywcjlove commented 1 year ago

@Rexime

  const eventExt2 = events.content({
    keydown: (evn) => {
      console.log('keydown');
    },
  });
Rexime commented 1 year ago

@jaywcjlove Thank you soooo much!!!! You really made my day!