diff options
author | Adam Chlipala <adam@chlipala.net> | 2012-07-21 10:02:53 -0400 |
---|---|---|
committer | Adam Chlipala <adam@chlipala.net> | 2012-07-21 10:02:53 -0400 |
commit | 3e838a5bf95222adb9add226aa3732c3c28633bc (patch) | |
tree | b3a11523df44e927ac749c5c6aa752744cb83b5d /lib | |
parent | db71493ffd90e2668259efbb549f7a781c2530db (diff) |
New event records for key and mouse handlers
Diffstat (limited to 'lib')
-rw-r--r-- | lib/js/urweb.js | 47 | ||||
-rw-r--r-- | lib/ur/basis.urs | 36 |
2 files changed, 63 insertions, 20 deletions
diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 509ff007..5846863a 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -441,22 +441,55 @@ function servErr(s) { 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_onMouseup(f) { 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) { diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs index bea6e105..cd38c783 100644 --- a/lib/ur/basis.urs +++ b/lib/ur/basis.urs @@ -784,12 +784,22 @@ con bodyTagStandalone = fn (attrs :: {Type}) => val br : bodyTagStandalone [Id = id] con focusEvents = [Onblur = transaction unit, Onfocus = transaction unit] -con mouseEvents = [Onclick = transaction unit, Ondblclick = transaction unit, - Onmousedown = transaction unit, Onmousemove = transaction unit, - Onmouseout = transaction unit, Onmouseover = transaction unit, - Onmouseup = transaction unit] -con keyEvents = [Onkeydown = int -> transaction unit, Onkeypress = int -> transaction unit, - Onkeyup = int -> transaction unit] + +datatype mouseButton = Left | Right | Middle + +type mouseEvent = { ScreenX : int, ScreenY : int, ClientX : int, ClientY : int, + CtrlKey : bool, ShiftKey : bool, AltKey : bool, MetaKey : bool, + Button : mouseButton } + +con mouseEvents = map (fn _ :: Unit => mouseEvent -> transaction unit) + [Onclick, Ondblclick, Onmousedown, Onmousemove, Onmouseout, Onmouseover, Onmouseup] + +type keyEvent = { KeyCode : int, + CtrlKey : bool, ShiftKey : bool, AltKey : bool, MetaKey : bool } + +con keyEvents = map (fn _ :: Unit => keyEvent -> transaction unit) + [Onkeydown, Onkeypress, Onkeyup] + (* Key arguments are character codes. *) con resizeEvents = [Onresize = transaction unit] con scrollEvents = [Onscroll = transaction unit] @@ -955,13 +965,13 @@ val onDisconnect : transaction unit -> transaction unit val onServerError : (string -> transaction unit) -> transaction unit (* More standard document-level JavaScript handlers *) -val onClick : transaction unit -> transaction unit -val onDblclick : transaction unit -> transaction unit -val onKeydown : (int -> transaction unit) -> transaction unit -val onKeypress : (int -> transaction unit) -> transaction unit -val onKeyup : (int -> transaction unit) -> transaction unit -val onMousedown : transaction unit -> transaction unit -val onMouseup : transaction unit -> transaction unit +val onClick : (mouseEvent -> transaction unit) -> transaction unit +val onDblclick : (mouseEvent -> transaction unit) -> transaction unit +val onKeydown : (keyEvent -> transaction unit) -> transaction unit +val onKeypress : (keyEvent -> transaction unit) -> transaction unit +val onKeyup : (keyEvent -> transaction unit) -> transaction unit +val onMousedown : (mouseEvent -> transaction unit) -> transaction unit +val onMouseup : (mouseEvent -> transaction unit) -> transaction unit (* Prevents default handling of current event *) val preventDefault : transaction unit |