# Overriding Ctrl-Z and Ctrl-Y in a plugin

**URL:** <https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326>\
**Category:** Rhino Developer\
**Tags:** windows\
**Created:** [February 10, 2025, 5:52pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326 "2025-02-10T17:52:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gruedisueli](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gruedisueli/32/556538_2.png) [@gruedisueli](https://discourse.mcneel.com/u/gruedisueli)\
**Post date:** [February 10, 2025, 5:52pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/1 "2025-02-10T17:52:38Z")

</div>

I have a plugin where I want to have a custom undo/redo that is completely independent of Rhino’s undo/redo. Furthermore, I don’t want the Rhino model to do anything when the user presses ctrl-z or ctrl-y. This is because the plugin model itself needs to take care of all the undo/redo actions to avoid corrupting its data.

The current solution is we have a custom rhino command “myplugin\_undo” and “myplugin\_redo” that can be added as a macro in the Rhino options dialog to override the ctrl-z and ctrl-y behavior.

This is not a very scalable solution and is more of a workaround than anything. Is there anything I can do about this so that we don’t need to provide special installation instructions?

---

<div class="post-metadata">

**Author:** ![CallumSykes](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/callumsykes/32/569341_2.png) [@CallumSykes](https://discourse.mcneel.com/u/CallumSykes)\
**Post date:** [February 11, 2025, 12:21pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/2 "2025-02-11T12:21:47Z")

</div>

There’s nothing I’m aware of in the API that’ll let you either block undo/redo in Rhino. But this user managed something like this → [Hook KeyboardEvent when command is running - #11 by Yannou](https://discourse.mcneel.com/t/hook-keyboardevent-when-command-is-running/197664/11)

That being said, I think I’d be curious to know what and why you’re trying to do what you’re trying to do, preventing undo/redo might not be the best approach.

> [@gruedisueli](#):
>
> This is because the plugin model itself needs to take care of all the undo/redo actions to avoid corrupting its data.

Why is this? What happens?

---

<div class="post-metadata">

**Author:** ![gruedisueli](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gruedisueli/32/556538_2.png) [@gruedisueli](https://discourse.mcneel.com/u/gruedisueli)\
**Post date:** [February 11, 2025, 2:32pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/3 "2025-02-11T14:32:11Z")

</div>

Thanks for the link!

I agree that it is not what “should” be done, and I’d never design things this way, but I’m working with an inherited codebase developed by another company and things are the way they are, at least within the bounds of what is reasonable to change without causing too many other issues.

I actually am not entirely clear myself about “corruption” (I’m using the word loosely) and need to investigate further and there may be other ways around the problem that are less invasive. In the most basic terms: we have a 3dm file that is a geometry cache and display for data in a parametric model the user is creating, via the intermediary of a plugin. Separately there is the parametric configuration data. Finally there is a state engine keeping track of user progress. Undoing operations in the Rhino model could theoretically cause any number of synchronization issues between the cached model and the plugin’s parametric configuration and state of flow.

If I think of something more specific I’ll post here.

---

<div class="post-metadata">

**Author:** ![Tigrou](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/tigrou/32/620063_2.png) [@Tigrou](https://discourse.mcneel.com/u/Tigrou)\
**Post date:** [February 11, 2025, 2:54pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/4 "2025-02-11T14:54:56Z")

</div>

Hi, I’m the author of the post provided by @CallumSykes  
Just be aware that my hack is only for macos. It will not work on ms windows 😉

---

<div class="post-metadata">

**Author:** ![Tigrou](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/tigrou/32/620063_2.png) [@Tigrou](https://discourse.mcneel.com/u/Tigrou)\
**Post date:** [February 11, 2025, 8:11pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/5 "2025-02-11T20:11:12Z")

</div>

In addition, for Windows programmming, even if I don’t have any Windows programming experience, I think you can do the same playing with changing **WNDPROC** callback of rhino window to process the message queue. So you can « capture » key events and eventually send message to original rhino window wndproc if your plugin doesn’t process the message.  
It’s Just an idea, I can’t try it as I didn’t own a windows computer.  
Hope it helps !

---

<div class="post-metadata">

**Author:** ![menno](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/menno/32/770384_2.png) [@menno](https://discourse.mcneel.com/u/menno)\
**Post date:** [February 11, 2025, 8:34pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/6 "2025-02-11T20:34:08Z")

</div>

You should be able to do this with a low-level keyboard hook, using functions in kernel32.dll and user32.dll. There’s a lot of examples on the web, [this](https://github.com/AlexRasch/KeyboardHookPoc) is just one of them.

I have used this a long time ago, and I don’t have access to that code anymore. One thing I do remember is that this is a global keyboard hook, that’s also active in other programs. So you want to check if the Rhino main window has focus when you’re processing keyboard events, as you probably don’t want to modify the behavior in other programs.

---

<div class="post-metadata">

**Author:** ![Tigrou](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/tigrou/32/620063_2.png) [@Tigrou](https://discourse.mcneel.com/u/Tigrou)\
**Post date:** [February 11, 2025, 8:47pm UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/7 "2025-02-11T20:47:20Z")

</div>

This is why (global keyboard hook) I prefer « re-route » rhino windows messaging queue to plugin window/control 😉  
The other drawback (or challenge) with global keyboard hook (at least on mac) is that the callback is not in the « gui » thread, so you can’t directly check/access gui controls like windows objects.

[edit]  
Oupss… read the provided link, and on Windows os, no problem to access gui in hook callback 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![CallumSykes](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/callumsykes/32/569341_2.png) [@CallumSykes](https://discourse.mcneel.com/u/CallumSykes)\
**Post date:** [February 12, 2025, 8:34am UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/8 "2025-02-12T08:34:26Z")

</div>

> [@gruedisueli](#):
>
> Undoing operations in the Rhino model could theoretically cause any number of synchronization issues between the cached model and the plugin’s parametric configuration and state of flow.

Hey @gruedisueli, I’d recommend updating the state and handing the sync issues by subscribing to [`Command.UndoReo`](https://developer.rhino3d.com/api/rhinocommon/rhino.commands.command/undoredo) rather than preventing Undo Redo if you can help it. (And if you can’t looks like theres lots of handy folks here with help 😉 )

You can also add your own [`CustomUndoEvents`](https://developer.rhino3d.com/api/rhinocommon/rhino.rhinodoc/addcustomundoevent#(string,eventhandler%3Ccustomundoeventargs%3E,object)) in case you need special things to be undoable.

Regarding code samples the [`EventWatcher`](https://github.com/mcneel/rhino-developer-samples/tree/7/rhinocommon/cs/SampleCsEventWatcher) is always handy to understand what Rhinos doing with its Event Chains.

---

<div class="post-metadata">

**Author:** ![gankeyu](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gankeyu/32/487811_2.png) [@gankeyu](https://discourse.mcneel.com/u/gankeyu)\
**Post date:** [February 12, 2025, 9:00am UTC](https://discourse.mcneel.com/t/overriding-ctrl-z-and-ctrl-y-in-a-plugin/199326/9 "2025-02-12T09:00:58Z")

</div>

You can of course filter window messages by low-level APIs, such as `SetWindowsHookEx`, but much else is left to do.
