Skip to content

Commit

Permalink
Merge pull request openlayers#478 from pgiraud/metaKey
Browse files Browse the repository at this point in the history
Adding support support for metaKey (Mac Cmd key)
  • Loading branch information
Pierre GIRAUD committed May 21, 2012
2 parents 348dffc + a997334 commit 88d927a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
15 changes: 11 additions & 4 deletions lib/OpenLayers/Handler.js
Expand Up @@ -139,7 +139,8 @@ OpenLayers.Handler = OpenLayers.Class({
var keyModifiers =
(evt.shiftKey ? OpenLayers.Handler.MOD_SHIFT : 0) |
(evt.ctrlKey ? OpenLayers.Handler.MOD_CTRL : 0) |
(evt.altKey ? OpenLayers.Handler.MOD_ALT : 0);
(evt.altKey ? OpenLayers.Handler.MOD_ALT : 0) |
(evt.metaKey ? OpenLayers.Handler.MOD_META : 0);

/* if it differs from the handler object's key mask,
bail out of the event handler */
Expand Down Expand Up @@ -232,12 +233,12 @@ OpenLayers.Handler = OpenLayers.Class({
* to get more information about the event that the handler is
* processing.
*
* This allows modifier keys on the event to be checked (alt, shift,
* and ctrl cannot be checked with the keyboard handler). For a
* This allows modifier keys on the event to be checked (alt, shift, ctrl,
* and meta cannot be checked with the keyboard handler). For a
* control to determine which modifier keys are associated with the
* event that a handler is currently processing, it should access
* (code)handler.evt.altKey || handler.evt.shiftKey ||
* handler.evt.ctrlKey(end).
* handler.evt.ctrlKey || handler.evt.metaKey(end).
*
* Parameters:
* evt - {Event} The browser event.
Expand Down Expand Up @@ -285,4 +286,10 @@ OpenLayers.Handler.MOD_CTRL = 2;
*/
OpenLayers.Handler.MOD_ALT = 4;

/**
* Constant: OpenLayers.Handler.MOD_META
* If set as the <keyMask>, <checkModifiers> returns false if Cmd is down.
*/
OpenLayers.Handler.MOD_META = 8;


16 changes: 10 additions & 6 deletions tests/Handler.html
Expand Up @@ -123,7 +123,7 @@
}

function test_Handler_setEvent(t) {
t.plan(4);
t.plan(5);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
Expand All @@ -135,7 +135,8 @@
xy: new OpenLayers.Pixel(Math.random(), Math.random()),
altKey: (Math.random() > 0.5),
shiftKey: (Math.random() > 0.5),
ctrlKey: (Math.random() > 0.5)
ctrlKey: (Math.random() > 0.5),
metaKey: (Math.random() > 0.5)
}
map.events.triggerEvent("click", testEvent);
t.ok(handler.evt.xy.x == testEvent.xy.x &&
Expand All @@ -147,6 +148,8 @@
"handler.evt.shiftKey correct");
t.eq(handler.evt.ctrlKey, testEvent.ctrlKey,
"handler.evt.ctrlKey correct");
t.eq(handler.evt.metaKey, testEvent.metaKey,
"handler.evt.metaKey correct");
}

function test_Handler_destroy(t) {
Expand All @@ -173,7 +176,7 @@
}

function test_Handler_checkModifiers(t) {
t.plan(26);
t.plan(62);
var handler = new OpenLayers.Handler({});
handler.keyMask = null;
var proceed = handler.checkModifiers({});
Expand All @@ -192,7 +195,8 @@
MOD_NONE: null,
MOD_SHIFT: "shiftKey",
MOD_CTRL: "ctrlKey",
MOD_ALT: "altKey"
MOD_ALT: "altKey",
MOD_META: "metaKey"
}
var proceed, evt, value, c, k;
for(c in constants) {
Expand Down Expand Up @@ -220,8 +224,8 @@
* is OpenLayers.Handler.MOD_SHIFT, checkModifiers should return
* true.
*/
var constants = ["MOD_SHIFT", "MOD_CTRL", "MOD_ALT"];
var keys = ["shiftKey", "ctrlKey", "altKey"];
var constants = ["MOD_SHIFT", "MOD_CTRL", "MOD_ALT", "MOD_META"];
var keys = ["shiftKey", "ctrlKey", "altKey", "metaKey"];
var proceed, evt, c1, c2, k1, k2;
for(var i=0; i<constants.length-1; ++i) {
c1 = constants[i];
Expand Down

0 comments on commit 88d927a

Please sign in to comment.