diff --git a/pd/nw/dialog_iemgui.html b/pd/nw/dialog_iemgui.html
index 5eb9e1d4d57846d0c7f6ca35cd18ef6d2be207c8..991d2f86e9be65c3100def0c44df905babe40cbe 100644
--- a/pd/nw/dialog_iemgui.html
+++ b/pd/nw/dialog_iemgui.html
@@ -572,13 +572,7 @@ function add_events(name) {
         pdgui.remove_dialogwin(pd_object_callback);
         this.close(true);
     });
-    document.onkeydown = function(evt) {
-        if (evt.keyCode === 13) { // enter
-            ok();
-        } else if (evt.keyCode === 27) { // escape
-            cancel();
-        }
-    };
+    pdgui.dialog_bindings(name);
 }
 
   </script>
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 3689b6faa68aa757f4cccd29a71391a4d54935d4..88e70ccf4a4ad0618db0410cd770b439080f88ca 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -3894,3 +3894,15 @@ function gui_find_lowest_and_arrange(cid, reference_element_tag, objtag) {
         }
     }
 }
+
+// Bindings for dialog menu of iemgui, canvas, etc.
+exports.dialog_bindings = function(did) {
+    var dwin = dialogwin[did].window;
+    dwin.document.onkeydown = function(evt) {
+        if (evt.keyCode === 13) { // enter
+            dwin.ok();
+        } else if (evt.keyCode === 27) { // escape
+            dwin.cancel();
+        }
+    };
+}