-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathMouse events with keys pressed.html
More file actions
91 lines (76 loc) · 4.49 KB
/
Copy pathMouse events with keys pressed.html
File metadata and controls
91 lines (76 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mouse events with keys pressed - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to monitor keys that are pressed when mouse events on the map. This same approach can be used with layers as well." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, events, mouse, key" />
<meta name="author" content="Microsoft Azure Maps" /><meta name="version" content="1.0" />
<meta name="screenshot" content="screenshot.jpg" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
<script>
var map, keysPressed = [];
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
view: 'Auto',
language: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
// Use SAS token for authentication
authType: 'sas',
getToken: function (resolve, reject, map) {
// URL to your authentication service that retrieves a SAS Token
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsSasToken';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Monitor the key down event.
document.body.addEventListener('keydown', function (e) {
if (keysPressed.indexOf(e.code) === -1) {
keysPressed.push(e.code);
}
});
//Monitor the key up event.
document.body.addEventListener('keyup', function (e) {
keysPressed.splice(keysPressed.indexOf(e.code), 1);
});
//Add mouse events to the map.
map.events.add('mouseenter', mouseEvent);
map.events.add('mouseleave', mouseEvent);
map.events.add('mousemove', mouseEvent);
map.events.add('mouseout', mouseEvent);
map.events.add('mouseover', mouseEvent);
map.events.add('mousedown', mouseEvent);
map.events.add('mouseup', mouseEvent);
map.events.add('click', mouseEvent);
});
}
function mouseEvent(e) {
document.getElementById('infoPanel').innerHTML = `Event: ${e.type}<br\>Keys pressed: ${keysPressed.join(' + ')}`;
}
</script>
</head>
<body onload="getMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div id="infoPanel" style="position:absolute;top:10px;left:10px;background-color:white;border-radius:10px;padding:10px;"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Mouse events with keys pressed</legend>
This sample shows how to monitor keys that are pressed when mouse events on the map. This same approach can be used with layers as well.
By default mouse events will know if the CTRL, ALT, and SHIFT keys are pressed, but won't know about other keys. This sample monitos the key down and down events to support all keys.
There are certain key down + mouse events that are native to the map as should be mainted to ensure a consistent across all implementations of Azure Maps.
For example, SHIFT + mouse down and drag that will draw out a rectangle area to zoom the map into quickly.
A full list is documented <a href="https://docs.microsoft.com/azure/azure-maps/map-accessibility" target="_blank">here</a>.
</fieldset>
</body>
</html>