Browse Source

fix key issues

Kyle Perik 5 months ago
parent
commit
fff92e2c87
1 changed files with 13 additions and 11 deletions
  1. 13 11
      events.js

+ 13 - 11
events.js

@@ -1,9 +1,10 @@
1 1
 
2 2
 // === Keep track of keystrokes ===
3 3
 pc.listen('keydown', (last, e) => {
4
-    if (e.key === 'Tab') { e.preventDefault(); }
5
-    if (last.keys.includes(e.key)) { return; }
6
-    var craft_it = e.key == 'Enter';
4
+    var key = e.key.toLowerCase();
5
+    if (key === 'tab') { e.preventDefault(); }
6
+    if (last.keys.includes(key)) { return; }
7
+    var craft_it = key == 'enter';
7 8
     var recipes = game_data.recipes.filter(r => r.inputs.every(
8 9
         input => last.player.resources[input.type] && last.player.resources[input.type] >= input.amount
9 10
     )).map(
@@ -16,7 +17,7 @@ pc.listen('keydown', (last, e) => {
16 17
         );
17 18
     }
18 19
     // FOR TESTING ONLY
19
-    switch (e.key){
20
+    switch (key){
20 21
         case '1': 
21 22
             GameEvents.add('meteorite', last);
22 23
             break;
@@ -27,8 +28,8 @@ pc.listen('keydown', (last, e) => {
27 28
             GameEvents.add('dustStorm', last);
28 29
             break;
29 30
     } 
30
-    var menu_call = e.key == 'Escape';
31
-    var info_call = e.key == 'i';
31
+    var menu_call = key == 'escape';
32
+    var info_call = key == 'i';
32 33
 
33 34
     // - on/off window, known recipies windows -
34 35
     if (menu_call && !menu_screen.isCalled) {
@@ -46,12 +47,12 @@ pc.listen('keydown', (last, e) => {
46 47
         Messages.infoIsShown = false;
47 48
     }
48 49
 
49
-    var menu_select = (e.key == 'ArrowUp' || e.key == 'ArrowDown');
50
+    var menu_select = (key == 'arrowup' || key == 'arrowdown');
50 51
 
51 52
     // - arrows to navigate menu -
52 53
     if (menu_select && menu_screen.isCalled) {
53 54
         var activeButton = menu_screen.options.find(o => o.isSelected);   
54
-        var selectButton = e.key === 'ArrowDown' ? 1 : -1;
55
+        var selectButton = key === 'arrowdown' ? 1 : -1;
55 56
         var buttonNames = menu_screen.options.map( o => o = o.title);
56 57
         var selectedButtonIndex = buttonNames.findIndex(i => i === activeButton.title) + selectButton;
57 58
 
@@ -69,7 +70,7 @@ pc.listen('keydown', (last, e) => {
69 70
 
70 71
     return {
71 72
         ...last,
72
-        selected_recipe: (last.selected_recipe + (e.key == 'Tab' ? 1 : 0)).mod(recipes.length) || 0,
73
+        selected_recipe: (last.selected_recipe + (key == 'Tab' ? 1 : 0)).mod(recipes.length) || 0,
73 74
         player: craft_it && recipe ? {
74 75
             ...last.player,
75 76
             resources: Object.keys(last.player.resources).map(
@@ -91,13 +92,14 @@ pc.listen('keydown', (last, e) => {
91 92
                 return resources;
92 93
             }, {})
93 94
         } : last.player,
94
-        keys: last.keys.concat([e.key])
95
+        keys: last.keys.concat([key])
95 96
     };
96 97
 });
97 98
 
98 99
 pc.listen('keyup', (last, e) => {
100
+    var key = e.key.toLowerCase();
99 101
     return {
100 102
         ...last,
101
-        keys: last.keys.filter(x => e.key != x)
103
+        keys: last.keys.filter(x => key != x)
102 104
     };
103 105
 });