NotebookEventActions
✖
NotebookEventActions
is a notebook option that gives a list of actions to perform when specified events occur in connection with the notebook.
Details

- NotebookEventActions->{"event1":>action1,"event2":>action2,…} specifies that actioni should be evaluated whenever "eventi" occurs in connection with the notebook.
- The possible event specifications are the same as for EventHandler, with the following addition:
-
"WindowClose" window being closed - Settings for NotebookEventActions are often defined at the level of stylesheets, rather than for individual notebooks.
- By default, any action specified for an event by 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 PassEventsDown or PassEventsUp in the list given as the setting for NotebookEventActions.
Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Set up a certain action to take place when a notebook is closed:

https://wolfram.com/xid/0bniuck9ja9ar-b84rhm

https://wolfram.com/xid/0bniuck9ja9ar-b1vqec
With the notebook open, k is unchanged:

https://wolfram.com/xid/0bniuck9ja9ar-dtxslq

When you close the notebook window, k is incremented:

https://wolfram.com/xid/0bniuck9ja9ar-p8d7q

Scope (3)Survey of the scope of standard use cases

https://wolfram.com/xid/0bniuck9ja9ar-cxse9o
Set NotebookEventActions so that the notebook will change color when clicked:

https://wolfram.com/xid/0bniuck9ja9ar-c0yt0b
Clicking the notebook changes its background:
Make an action occur when the key is pressed:

https://wolfram.com/xid/0bniuck9ja9ar-k5pbox

https://wolfram.com/xid/0bniuck9ja9ar-iv714v
Define actions for "MouseDown" and "MouseUp":

https://wolfram.com/xid/0bniuck9ja9ar-c7ubb

https://wolfram.com/xid/0bniuck9ja9ar-gwciir
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:
Possible Issues (3)Common pitfalls and unexpected behavior
This NotebookEventActions setting blocks the normal key behavior:

https://wolfram.com/xid/0bniuck9ja9ar-dkc491

https://wolfram.com/xid/0bniuck9ja9ar-etcxk6
Pressing changes the background but does not start a new line:
This inserts a newline as part of the action specified for "ReturnKeyDown":

https://wolfram.com/xid/0bniuck9ja9ar-j5if6p

https://wolfram.com/xid/0bniuck9ja9ar-kbglsx
Pressing in this notebook inserts a newline as well as setting the background:
Specify a NotebookEventActions setting that goes away after being activated once:

https://wolfram.com/xid/0bniuck9ja9ar-c7mhe9

https://wolfram.com/xid/0bniuck9ja9ar-ioq3s
A single changes the background without entering a newline:
Also, NotebookEventActions has been reset to None:

https://wolfram.com/xid/0bniuck9ja9ar-dxahz8

Wolfram Research (2007), NotebookEventActions, Wolfram Language function, https://reference.wolfram.com/language/ref/NotebookEventActions.html.
Text
Wolfram Research (2007), NotebookEventActions, Wolfram Language function, https://reference.wolfram.com/language/ref/NotebookEventActions.html.
Wolfram Research (2007), NotebookEventActions, Wolfram Language function, https://reference.wolfram.com/language/ref/NotebookEventActions.html.
CMS
Wolfram Language. 2007. "NotebookEventActions." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NotebookEventActions.html.
Wolfram Language. 2007. "NotebookEventActions." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NotebookEventActions.html.
APA
Wolfram Language. (2007). NotebookEventActions. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NotebookEventActions.html
Wolfram Language. (2007). NotebookEventActions. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NotebookEventActions.html
BibTeX
@misc{reference.wolfram_2025_notebookeventactions, author="Wolfram Research", title="{NotebookEventActions}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/NotebookEventActions.html}", note=[Accessed: 10-May-2025
]}
BibLaTeX
@online{reference.wolfram_2025_notebookeventactions, organization={Wolfram Research}, title={NotebookEventActions}, year={2007}, url={https://reference.wolfram.com/language/ref/NotebookEventActions.html}, note=[Accessed: 10-May-2025
]}