diff lib/js/urweb.js @ 1783:5bc4fbf9c0fe

New event records for key and mouse handlers
author Adam Chlipala <adam@chlipala.net>
date Sat, 21 Jul 2012 10:02:53 -0400
parents 25824a0e8bf1
children ffd7ed3bc0b7
line wrap: on
line diff
--- a/lib/js/urweb.js	Wed Jul 18 17:29:13 2012 -0400
+++ b/lib/js/urweb.js	Sat Jul 21 10:02:53 2012 -0400
@@ -441,22 +441,55 @@
     window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
 }
 
-// Key events
+// Key and mouse events
 
 var uw_event = null;
 
-function kc() {
-    return window.event ? event.keyCode : (uw_event ? uw_event.which : 0);
+function uw_getEvent() {
+    return window.event ? window.event : uw_event;
 }
 
+function firstGood(x, y) {
+    if (x == undefined || x == 0)
+        return y;
+    else
+        return x;
+}
+
+function uw_mouseEvent() {
+    var ev = uw_getEvent();
+
+    return {_ScreenX : firstGood(ev.screenX, 0),
+            _ScreenY : firstGood(ev.screenY, 0),
+            _ClientX : firstGood(ev.clientX, 0),
+            _ClientY : firstGood(ev.clientY, 0),
+            _CtrlKey : firstGood(ev.ctrlKey, false),
+            _ShiftKey : firstGood(ev.shiftKey, false),
+            _AltKey : firstGood(ev.altKey, false),
+            _MetaKey : firstGood(ev.metaKey, false),
+            _Button : ev.button == 2 ? "Right" : ev.button == 1 ? "Middle" : "Left"};
+}
+
+function uw_keyEvent() {
+    var ev = uw_getEvent();
+
+    return {_KeyCode : firstGood(ev.keyCode, ev.which),
+            _CtrlKey : firstGood(ev.ctrlKey, false),
+            _ShiftKey : firstGood(ev.shiftKey, false),
+            _AltKey : firstGood(ev.altKey, false),
+            _MetaKey : firstGood(ev.metaKey, false)};
+}
+
+
+
 // Document events
 
 function uw_handler(name, f) {
     var old = document[name];
     if (old == undefined)
-        document[name] = function(event) { uw_event = event; execF(f); };
+        document[name] = function(event) { uw_event = event; execF(execF(f, uw_mouseEvent())); };
     else
-        document[name] = function(event) { uw_event = event; old(); execF(f); };
+        document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_mouseEvent())); };
 }
 
 function uw_onClick(f) {
@@ -478,9 +511,9 @@
 function uw_keyHandler(name, f) {
     var old = document[name];
     if (old == undefined)
-        document[name] = function(event) { uw_event = event; execF(execF(f, kc())); };
+        document[name] = function(event) { uw_event = event; execF(execF(f, uw_keyEvent())); };
     else
-        document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); };
+        document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_keyEvent())); };
 }
 
 function uw_onKeydown(f) {