({{, (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]) 
is a notebook option that gives a list of actions to perform when specified events occur in connection with the notebook. }})

OpenersNotesSection(Spacer1)Details

(RefLink[NotebookEventActions, paclet:ref/NotebookEventActions])->{"(Subscript[event, 1])":>(Subscript[action, 1]),"(Subscript[event, 2])":>(Subscript[action, 2]),…} specifies that (Subscript[action, i]) should be evaluated whenever "(Subscript[event, i])" occurs in connection with the notebook.

The possible event specifications are the same as for (RefLink[EventHandler, paclet:ref/EventHandler]), with the following addition:

Settings for (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]) are often defined at the level of stylesheets, rather than for individual notebooks.

By default, any action specified for an event by (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]) will override any default action that the Wolfram System would take for that event.

The default behavior can be changed by including rules for (RefLink[PassEventsDown, paclet:ref/PassEventsDown]) or (RefLink[PassEventsUp, paclet:ref/PassEventsUp]) in the list given as the setting for (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (7)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Set up a certain action to take place when a notebook is closed:

k=0;nb=CreateDocument[{a}];

SetOptions[nb,NotebookEventActions->{"WindowClose":>k++}];

With the notebook open, k is unchanged:

k

When you close the notebook window, k is incremented:

k

OpenersExampleSection1(Spacer1)Scope  (3)

Create a notebook window:

nb=CreateDocument[{a}];

Set (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]) so that the notebook will change color when clicked:

SetOptions[nb,NotebookEventActions->{"MouseClicked":>SetOptions[nb,Background->Pink]}];

Clicking the notebook changes its background:

Make an action occur when the (Key0[MacOSXreturnenter]) key is pressed:

nb=CreateDocument[{a}];

SetOptions[nb,NotebookEventActions->{"ReturnKeyDown":>SetOptions[nb,Background->Pink]}];

Define actions for "MouseDown" and "MouseUp":

nb=CreateDocument[{a}];

SetOptions[nb,NotebookEventActions->{"MouseDown":>SetOptions[nb,Background->Pink],"MouseUp":>SetOptions[nb,Background->Inherited]}];

The background will be pink only while the mouse button is held down:

After the mouse is released, the notebook is back to its normal background:

OpenersExampleSection3(Spacer1)Possible Issues  (3)

This (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]) setting blocks the normal (Key0[MacOSXreturnenter]) key behavior:

nb=CreateDocument[{a}];

SetOptions[nb,NotebookEventActions->{"ReturnKeyDown":>SetOptions[nb,Background->Pink]}];

Pressing (Key0[MacOSXreturnenter]) changes the background but does not start a new line:

This inserts a newline as part of the action specified for "ReturnKeyDown":

nb=CreateDocument[{a}];

SetOptions[nb,NotebookEventActions->{"ReturnKeyDown":>(SetOptions[nb,Background->Pink];NotebookWrite[nb,"\n"])}];

Pressing (Key0[MacOSXreturnenter]) in this notebook inserts a newline as well as setting the background:

Specify a (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]) setting that goes away after being activated once:

nb=CreateDocument[{a}];

SetOptions[nb,NotebookEventActions->{"ReturnKeyDown":>(SetOptions[nb,Background->Pink,NotebookEventActions->None])}];

A single (Key0[MacOSXreturnenter]) changes the background without entering a newline:

Also, (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions]) has been reset to (RefLink[None, paclet:ref/None]):

Options[nb,NotebookEventActions]

Now, more (Key0[MacOSXreturnenter]) keystrokes add new lines as usual:

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[EventHandler, paclet:ref/EventHandler])  ▪  (RefLink[CellEventActions, paclet:ref/CellEventActions])  ▪  (RefLink[FrontEndEventActions, paclet:ref/FrontEndEventActions])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Stylesheets, paclet:guide/Stylesheets])}, {▪(RefLinkPlain[Low-Level Interface Control, paclet:guide/LowLevelInterfaceControl])}, {▪(RefLinkPlain[Notebook & Interface Customization, paclet:guide/NotebookAndInterfaceCustomization])}, {▪(RefLinkPlain[Programmatic Notebook & Interface Customization, paclet:guide/ProgrammaticNotebookAndInterfaceCustomization])}})}})