comparison 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
comparison
equal deleted inserted replaced
1782:61c7eb1d3867 1783:5bc4fbf9c0fe
439 439
440 function servErr(s) { 440 function servErr(s) {
441 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0); 441 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
442 } 442 }
443 443
444 // Key events 444 // Key and mouse events
445 445
446 var uw_event = null; 446 var uw_event = null;
447 447
448 function kc() { 448 function uw_getEvent() {
449 return window.event ? event.keyCode : (uw_event ? uw_event.which : 0); 449 return window.event ? window.event : uw_event;
450 } 450 }
451
452 function firstGood(x, y) {
453 if (x == undefined || x == 0)
454 return y;
455 else
456 return x;
457 }
458
459 function uw_mouseEvent() {
460 var ev = uw_getEvent();
461
462 return {_ScreenX : firstGood(ev.screenX, 0),
463 _ScreenY : firstGood(ev.screenY, 0),
464 _ClientX : firstGood(ev.clientX, 0),
465 _ClientY : firstGood(ev.clientY, 0),
466 _CtrlKey : firstGood(ev.ctrlKey, false),
467 _ShiftKey : firstGood(ev.shiftKey, false),
468 _AltKey : firstGood(ev.altKey, false),
469 _MetaKey : firstGood(ev.metaKey, false),
470 _Button : ev.button == 2 ? "Right" : ev.button == 1 ? "Middle" : "Left"};
471 }
472
473 function uw_keyEvent() {
474 var ev = uw_getEvent();
475
476 return {_KeyCode : firstGood(ev.keyCode, ev.which),
477 _CtrlKey : firstGood(ev.ctrlKey, false),
478 _ShiftKey : firstGood(ev.shiftKey, false),
479 _AltKey : firstGood(ev.altKey, false),
480 _MetaKey : firstGood(ev.metaKey, false)};
481 }
482
483
451 484
452 // Document events 485 // Document events
453 486
454 function uw_handler(name, f) { 487 function uw_handler(name, f) {
455 var old = document[name]; 488 var old = document[name];
456 if (old == undefined) 489 if (old == undefined)
457 document[name] = function(event) { uw_event = event; execF(f); }; 490 document[name] = function(event) { uw_event = event; execF(execF(f, uw_mouseEvent())); };
458 else 491 else
459 document[name] = function(event) { uw_event = event; old(); execF(f); }; 492 document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_mouseEvent())); };
460 } 493 }
461 494
462 function uw_onClick(f) { 495 function uw_onClick(f) {
463 uw_handler("onclick", f); 496 uw_handler("onclick", f);
464 } 497 }
476 } 509 }
477 510
478 function uw_keyHandler(name, f) { 511 function uw_keyHandler(name, f) {
479 var old = document[name]; 512 var old = document[name];
480 if (old == undefined) 513 if (old == undefined)
481 document[name] = function(event) { uw_event = event; execF(execF(f, kc())); }; 514 document[name] = function(event) { uw_event = event; execF(execF(f, uw_keyEvent())); };
482 else 515 else
483 document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); }; 516 document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_keyEvent())); };
484 } 517 }
485 518
486 function uw_onKeydown(f) { 519 function uw_onKeydown(f) {
487 uw_keyHandler("onkeydown", f); 520 uw_keyHandler("onkeydown", f);
488 } 521 }