# Trigger InputRule on enter?

**URL:** <https://discuss.prosemirror.net/t/trigger-inputrule-on-enter/1118>\
**Category:** Uncategorized\
**Created:** [December 23, 2017, 10:33pm UTC](https://discuss.prosemirror.net/t/trigger-inputrule-on-enter/1118 "2017-12-23T22:33:19Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![bhl](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bhl/32/4074_2.png) [@bhl](https://discuss.prosemirror.net/u/bhl)\
**Post date:** [October 12, 2020, 8:23am UTC](https://discuss.prosemirror.net/t/trigger-inputrule-on-enter/1118/5 "2020-10-12T08:23:16Z")

</div>

For posterity, an easy way to implement this is to add a `handleKeyDown` to the `inputRules` function of `prosemirror-inputrules`. Original source code [here](https://github.com/ProseMirror/prosemirror-inputrules/blob/master/src/inputrules.js#L52-L80).

```auto
export function inputRules({rules}) {
  let plugin = new Plugin({
    state: {
      init() { return null },
      apply(tr, prev) {
        let stored = tr.getMeta(this)
        if (stored) return stored
        return tr.selectionSet || tr.docChanged ? null : prev
      }
    },

    props: {
      handleTextInput(view, from, to, text) {
        return run(view, from, to, text, rules, plugin)
      },
      handleDOMEvents: {
        compositionend: (view) => {
          setTimeout(() => {
            let {$cursor} = view.state.selection
            if ($cursor) run(view, $cursor.pos, $cursor.pos, "", rules, plugin)
          })
        }
      },
      handleKeyDown(view, event) {
        if (event.key !== "Enter") return false;
        let {$cursor} = view.state.selection
        if ($cursor) return run(view, $cursor.pos, $cursor.pos, "\n", rules, plugin)
        return false;
      }
    },

    isInputRules: true
  })
  return plugin
}

```

Within a specific inputrule, you would then want to add the `\n` character at the end. For example for codeblocks,

````auto
textblockTypeInputRule(/^```([^\s]*)[\s\n]$/, nodeType, match => ({...match[1] && {lang: match[1]}}))

````

Edit: gif of working example

 ![enterinputrule](https://discuss.prosemirror.net/uploads/secondsite/original/2X/5/5a519381aa3a85ed5704adb121892d7eb9560101.gif)

---

_[View the full topic](https://discuss.prosemirror.net/t/trigger-inputrule-on-enter/1118)._
