Skip to content
This repository
Browse code

Fix a spurious key repeat in arrow control.

  • Loading branch information...
commit 3850ed60cad415105bec5c9540c1bfca28386fb8 1 parent 9b412d4
Mike Bostock authored

Showing 2 changed files with 67 additions and 25 deletions. Show diff stats Hide diff stats

  1. +66 24 src/Arrow.js
  2. +1 1  src/start.js
90 src/Arrow.js
@@ -10,17 +10,64 @@ po.arrow = function() {
10 10
11 11 function keydown(e) {
12 12 if (e.ctrlKey || e.altKey || e.metaKey) return;
13   - var now = Date.now();
  13 + var now = Date.now(), dx = 0, dy = 0, dz = 0;
14 14 switch (e.keyCode) {
15   - case 37: if (!key.left) last = now; key.left = 1; break;
16   - case 39: if (!key.right) last = now; key.right = 1; break;
17   - case 38: if (!key.up) last = now; key.up = 1; break;
18   - case 40: if (!key.down) last = now; key.down = 1; break;
19   - case 109: case 189: if (!key.plus) last = now; key.plus = 1; break;
20   - case 61: case 187: if (!key.minus) last = now; key.minus = 1; break;
21   - default: return;
  15 + case 37: {
  16 + if (!key.left) {
  17 + last = now;
  18 + key.left = 1;
  19 + if (!key.right) dx = speed;
  20 + }
  21 + break;
  22 + }
  23 + case 39: {
  24 + if (!key.right) {
  25 + last = now;
  26 + key.right = 1;
  27 + if (!key.left) dx = -speed;
  28 + }
  29 + break;
  30 + }
  31 + case 38: {
  32 + if (!key.up) {
  33 + last = now;
  34 + key.up = 1;
  35 + if (!key.down) dy = speed;
  36 + }
  37 + break;
  38 + }
  39 + case 40: {
  40 + if (!key.down) {
  41 + last = now;
  42 + key.down = 1;
  43 + if (!key.up) dy = -speed;
  44 + }
  45 + break;
  46 + }
  47 + case 109: case 189: {
  48 + if (!key.plus) {
  49 + last = now;
  50 + key.plus = 1;
  51 + if (!key.minus) dz = -1;
  52 + }
  53 + break;
  54 + }
  55 + case 61: case 187: {
  56 + if (!key.minus) {
  57 + last = now;
  58 + key.minus = 1;
  59 + if (!key.plus) dz = 1;
  60 + }
  61 + break;
  62 + }
  63 + default: return;
  64 + }
  65 + if (dz) {
  66 + var z = map.zoom();
  67 + map.zoom(dz < 0 ? Math.ceil(z) - 1 : Math.floor(z) + 1);
  68 + } else if (dx || dy) {
  69 + map.panBy({x: dx, y: dy});
22 70 }
23   - if (last == now) repeat(true);
24 71 if (!repeatTimer && (key.left | key.right | key.up | key.down)) {
25 72 repeatTimer = setInterval(repeat, repeatInterval);
26 73 }
@@ -30,13 +77,13 @@ po.arrow = function() {
30 77 function keyup(e) {
31 78 last = Date.now();
32 79 switch (e.keyCode) {
33   - case 37: key.left = 0; break;
34   - case 39: key.right = 0; break;
35   - case 38: key.up = 0; break;
36   - case 40: key.down = 0; break;
37   - case 109: case 189: key.plus = 0; break;
38   - case 61: case 187: key.minus = 0; break;
39   - default: return;
  80 + case 37: key.left = 0; break;
  81 + case 39: key.right = 0; break;
  82 + case 38: key.up = 0; break;
  83 + case 40: key.down = 0; break;
  84 + case 109: case 189: key.plus = 0; break;
  85 + case 61: case 187: key.minus = 0; break;
  86 + default: return;
40 87 }
41 88 if (repeatTimer && !(key.left | key.right | key.up | key.down)) {
42 89 repeatTimer = clearInterval(repeatTimer);
@@ -59,17 +106,12 @@ po.arrow = function() {
59 106 return arrow;
60 107 };
61 108
62   - function repeat(down) {
  109 + function repeat() {
63 110 if (!map) return;
64   - if (!down && Date.now() < last + repeatDelay) return;
  111 + if (Date.now() < last + repeatDelay) return;
65 112 var dx = (key.left - key.right) * speed,
66   - dy = (key.up - key.down) * speed,
67   - dz = key.minus - key.plus;
  113 + dy = (key.up - key.down) * speed;
68 114 if (dx || dy) map.panBy({x: dx, y: dy});
69   - if (down && dz) {
70   - var z = map.zoom();
71   - map.zoom(dz < 0 ? Math.ceil(z) - 1 : Math.floor(z) + 1);
72   - }
73 115 }
74 116
75 117 return arrow;
2  src/start.js
@@ -2,4 +2,4 @@ if (!org) var org = {};
2 2 if (!org.polymaps) org.polymaps = {};
3 3 (function(po){
4 4
5   - po.version = "1.0.4"; // semver.org
  5 + po.version = "1.0.5"; // semver.org

0 comments on commit 3850ed6

Please sign in to comment.
Something went wrong with that request. Please try again.