Performing an arrowdown keyevent when ctrl+n is pushed

Hi,

I want to make a plugin that when ctrl+n is pressed it has the exact same behavior as the down arrow being pressed.

I thought the following would work, but editor.cm.dom.dispatchEvent(event); does not seem to do anything. Any help would be appreciated!

import { Plugin, Editor } from 'obsidian';

export default class CtrlNAsDownArrowPlugin extends Plugin {
  async onload() {
    this.addCommand({
      id: 'ctrl-n-as-down-arrow',
      name: 'Use Ctrl+N as Down Arrow',
      hotkeys: [{ modifiers: ['Ctrl'], key: 'N' }],
      editorCallback: (editor: Editor) => {
        const event = new KeyboardEvent("keydown", {
          key: "ArrowDown"
        });
        editor.cm.dom.dispatchEvent(event);
      },
    });
  }
}

The following worked:

moveCursorDown = (editor: Editor) => {
    document.activeElement.dispatchEvent(new KeyboardEvent("keydown",{"key":"ArrowDown","code":"ArrowDown"}))
  }

This is the function called when ctrl+n is pressed. It creates a keyboard event which emulates the ArrowDown key being pressed. The key was activeElement

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.