<commit_before>---
title: 3. Handling Editor Events
---

The following set of tutorials is meant to be an introduction to actions activated by editor events.
The IntelliJ Platform SDK provides a set of callbacks for handling events related to the Editor.

[Source code](https://github.com/JetBrains/intellij-sdk-docs/tree/master/code_samples/editor_basics)


## 3.1. Handling keystrokes in the Editor

To handle keystrokes and provide custom reactions interface
[TypedActionHandler](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedActionHandler.java)
may be used.
Series of steps below shows how to change standard behaviour of the editor and make it react on typing differently instead of simply displaying a typed character in the editor area.

### 3.1.2 Implementing *TypedActionHandler*

First we need to implement an instance of
[TypedActionHandler](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedActionHandler.java):

```java
public class MyTypedHandler implements TypedActionHandler {
    @Override
    public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) {
    }
}
```

### 3.1.3. Implementing logic for handling keystrokes

`public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext);`

method should contain the main logical part for handling keystrokes. It will be called every time a key is pressed.
In the following example our typed handler is meant insert a string at the zero offset in the editor after a keystroke occurs:

```java
public class MyTypedHandler implements TypedActionHandler {
    @Override
    public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) {
        final Document document = editor.getDocument();
        Project project = editor.getProject();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                document.insertString(0, "Typed\n");
            }
        };
        WriteCommandAction.runWriteCommandAction(project, runnable);
    }
}
```

### 3.1.4. Setting up *TypedActionHandler*

To enable a custom implementation of *TypedActionHandler* in the plugin we need to create a new instance of it and pass to
`public TypedActionHandler setupHandler(TypedActionHandler handler);` method of the
[TypedAction](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedAction.java)
class. By doing it we replace the typing handler with the specified handler.

```java
public class EditorIllustration extends AnAction {
    static {
        final EditorActionManager actionManager = EditorActionManager.getInstance();
        final TypedAction typedAction = actionManager.getTypedAction();
        typedAction.setupHandler(new MyTypedHandler());
    }
}
```

After compiling and running the code snippet above typing in the editor will be handled with inserting an extra string at the 0 position.

## 3.2. Working with EditorActionHandler

Class
[EditorActionHandler.java](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorActionHandler.java)
stays for actions activated by keystrokes in the editor.
Series of steps below show how access *EditorActionManager* and pass it actions to be executed.
In this example we will use *EditorActionHandler* to insert one extra caret above the current caret if available.

### 3.2.1. Pre-requirements

Create an action:

```java
public class EditorHandlerIllustration extends AnAction {
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
    }
    @Override
    public void update(@NotNull final AnActionEvent anActionEvent) {
    }
}<commit_msg>reverse a partial reversal, loosing a line<commit_after>---
title: 3. Handling Editor Events
---

The following set of tutorials is meant to be an introduction to actions activated by editor events.
The IntelliJ Platform SDK provides a set of callbacks for handling events related to the Editor.

[Source code](https://github.com/JetBrains/intellij-sdk-docs/tree/master/code_samples/editor_basics)


## 3.1. Handling keystrokes in the Editor

To handle keystrokes and provide custom reactions interface
[TypedActionHandler](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedActionHandler.java)
may be used.
Series of steps below shows how to change standard behaviour of the editor and make it react on typing differently instead of simply displaying a typed character in the editor area.

### 3.1.2 Implementing *TypedActionHandler*

First we need to implement an instance of
[TypedActionHandler](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedActionHandler.java):

```java
public class MyTypedHandler implements TypedActionHandler {
    @Override
    public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) {
    }
}
```

### 3.1.3. Implementing logic for handling keystrokes

`public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext);`

method should contain the main logical part for handling keystrokes. It will be called every time a key is pressed.
In the following example our typed handler is meant insert a string at the zero offset in the editor after a keystroke occurs:

```java
public class MyTypedHandler implements TypedActionHandler {
    @Override
    public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) {
        final Document document = editor.getDocument();
        Project project = editor.getProject();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                document.insertString(0, "Typed\n");
            }
        };
        WriteCommandAction.runWriteCommandAction(project, runnable);
    }
}
```

### 3.1.4. Setting up *TypedActionHandler*

To enable a custom implementation of *TypedActionHandler* in the plugin we need to create a new instance of it and pass to
`public TypedActionHandler setupHandler(TypedActionHandler handler);` method of the
[TypedAction](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/TypedAction.java)
class. By doing it we replace the typing handler with the specified handler.

```java
public class EditorIllustration extends AnAction {
    static {
        final EditorActionManager actionManager = EditorActionManager.getInstance();
        final TypedAction typedAction = actionManager.getTypedAction();
        typedAction.setupHandler(new MyTypedHandler());
    }
}
```

After compiling and running the code snippet above typing in the editor will be handled with inserting an extra string at the 0 position.

## 3.2. Working with EditorActionHandler

Class
[EditorActionHandler.java](upsource:///platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorActionHandler.java)
stays for actions activated by keystrokes in the editor.
Series of steps below show how access *EditorActionManager* and pass it actions to be executed.
In this example we will use *EditorActionHandler* to insert one extra caret above the current caret if available.

### 3.2.1. Pre-requirements

Create an action:

```java
public class EditorHandlerIllustration extends AnAction {
    @Override                                        
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
    }
    @Override
    public void update(@NotNull final AnActionEvent anActionEvent) {
    }
}
{'fraction_non_alphanumeric': 0.07481098288897732, 'fraction_numerical': 0.0050404562939381885, 'mean_word_length': 4.1573187414500685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6600811', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1917, 'n_words': 753}