DEV: Use `key` over the deprecated `keyCode` (#13795)

DEV: Use key over the deprecated keyCode (#13795)

Makes the code a bit more readable. Inspired by Stop using deprecated `keyCode` on `KeyboardEvent` by chancancode · Pull Request #19185 · emberjs/ember.js · GitHub

diff --git a/app/assets/javascripts/admin/addon/components/permalink-form.js b/app/assets/javascripts/admin/addon/components/permalink-form.js
index da8f947..2784615 100644
--- a/app/assets/javascripts/admin/addon/components/permalink-form.js
+++ b/app/assets/javascripts/admin/addon/components/permalink-form.js
@@ -28,8 +28,7 @@ export default Component.extend({
 
     schedule("afterRender", () => {
       $(this.element.querySelector(".external-url")).keydown((e) => {
-        // enter key
-        if (e.keyCode === 13) {
+        if (e.key === "Enter") {
           this.send("submit");
         }
       });
diff --git a/app/assets/javascripts/admin/addon/components/screened-ip-address-form.js b/app/assets/javascripts/admin/addon/components/screened-ip-address-form.js
index 40dd71a..5636c30 100644
--- a/app/assets/javascripts/admin/addon/components/screened-ip-address-form.js
+++ b/app/assets/javascripts/admin/addon/components/screened-ip-address-form.js
@@ -83,7 +83,7 @@ export default Component.extend({
   _init() {
     schedule("afterRender", () => {
       $(this.element.querySelector(".ip-address-input")).keydown((e) => {
-        if (e.keyCode === 13) {
+        if (e.key === "Enter") {
           this.send("submit");
         }
       });
diff --git a/app/assets/javascripts/admin/addon/components/value-list.js b/app/assets/javascripts/admin/addon/components/value-list.js
index aa89de7..2daacfd 100644
--- a/app/assets/javascripts/admin/addon/components/value-list.js
+++ b/app/assets/javascripts/admin/addon/components/value-list.js
@@ -33,7 +33,7 @@ export default Component.extend({
   },
 
   keyDown(event) {
-    if (event.keyCode === 13) {
+    if (event.key === "Enter") {
       this.send("addValue", this.newValue);
     }
   },
diff --git a/app/assets/javascripts/admin/addon/components/watched-word-form.js b/app/assets/javascripts/admin/addon/components/watched-word-form.js
index 93dbdda..7bdd313 100644
--- a/app/assets/javascripts/admin/addon/components/watched-word-form.js
+++ b/app/assets/javascripts/admin/addon/components/watched-word-form.js
@@ -118,7 +118,7 @@ export default Component.extend({
   _init() {
     schedule("afterRender", () => {
       $(this.element.querySelector(".watched-word-input")).keydown((e) => {
-        if (e.keyCode === 13) {
+        if (e.key === "Enter") {
           this.send("submit");
         }
       });
diff --git a/app/assets/javascripts/admin/addon/mixins/setting-component.js b/app/assets/javascripts/admin/addon/mixins/setting-component.js
index fc6c6c2..b55c045 100644
--- a/app/assets/javascripts/admin/addon/mixins/setting-component.js
+++ b/app/assets/javascripts/admin/addon/mixins/setting-component.js
@@ -138,7 +138,7 @@ export default Mixin.create({
       "keydown.setting-enter",
       ".input-setting-string",
       (e) => {
-        if (e.keyCode === 13) {
+        if (e.key === "Enter") {
           // enter key
           this.send("save");
         }
diff --git a/app/assets/javascripts/discourse/app/components/choose-topic.js b/app/assets/javascripts/discourse/app/components/choose-topic.js
index 6786c94..d7d164d 100644
--- a/app/assets/javascripts/discourse/app/components/choose-topic.js
+++ b/app/assets/javascripts/discourse/app/components/choose-topic.js
@@ -43,7 +43,7 @@ export default Component.extend({
     this._super(...arguments);
     schedule("afterRender", () => {
       $("#choose-topic-title").keydown((e) => {
-        if (e.keyCode === 13) {
+        if (e.key === "Enter") {
           return false;
         }
       });
diff --git a/app/assets/javascripts/discourse/app/components/create-account.js b/app/assets/javascripts/discourse/app/components/create-account.js
index 84cc94a..f61d76b 100644
--- a/app/assets/javascripts/discourse/app/components/create-account.js
+++ b/app/assets/javascripts/discourse/app/components/create-account.js
@@ -44,7 +44,7 @@ export default Component.extend({
     }
 
     $(this.element).on("keydown.discourse-create-account", (e) => {
-      if (!this.disabled && e.keyCode === 13) {
+      if (!this.disabled && e.key === "Enter") {
         e.preventDefault();
         e.stopPropagation();
         this.action();
diff --git a/app/assets/javascripts/discourse/app/components/login-modal.js b/app/assets/javascripts/discourse/app/components/login-modal.js
index 52f0ace..c6d0f93 100644
--- a/app/assets/javascripts/discourse/app/components/login-modal.js
+++ b/app/assets/javascripts/discourse/app/components/login-modal.js
@@ -21,7 +21,7 @@ export default Component.extend({
       $(
         "#login-account-password, #login-account-name, #login-second-factor"
       ).keydown((e) => {
-        if (e.keyCode === 13) {
+        if (e.key === "Enter") {
           this.action();
         }
       });
diff --git a/app/assets/javascripts/discourse/app/components/share-popup.js b/app/assets/javascripts/discourse/app/components/share-popup.js
index 85e815c..94776d2 100644
--- a/app/assets/javascripts/discourse/app/components/share-popup.js
+++ b/app/assets/javascripts/discourse/app/components/share-popup.js
@@ -156,7 +156,7 @@ export default Component.extend({
       return;
     }
 
-    if (event.keyCode === 27) {
+    if (event.key === "Escape") {
       this.send("close");
     }
   },
diff --git a/app/assets/javascripts/discourse/app/controllers/flag.js b/app/assets/javascripts/discourse/app/controllers/flag.js
index 7834743..e221f5e 100644
--- a/app/assets/javascripts/discourse/app/controllers/flag.js
+++ b/app/assets/javascripts/discourse/app/controllers/flag.js
@@ -56,7 +56,7 @@ export default Controller.extend(ModalFunctionality, {
   @bind
   keyDown(event) {
     // CTRL+ENTER or CMD+ENTER
-    if (event.keyCode === 13 && (event.ctrlKey || event.metaKey)) {
+    if (event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
       if (this.submitEnabled) {
         this.send("createFlag");
         return false;
diff --git a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js
index 1bcb1b6..4bc5223 100644
--- a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js
+++ b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js
@@ -539,7 +539,7 @@ export default {
       const selection = document.querySelector(selector);
 
       // Special case: We're binding to enter.
-      if (e && e.keyCode === 13) {
+      if (e && e.key === "Enter") {
         // Binding to enter should only be effective when there is something
         // to select.
         if (!selection) {
diff --git a/app/assets/javascripts/discourse/app/mixins/card-contents-base.js b/app/assets/javascripts/discourse/app/mixins/card-contents-base.js
index 2b036eb..bda3d3f 100644
--- a/app/assets/javascripts/discourse/app/mixins/card-contents-base.js
+++ b/app/assets/javascripts/discourse/app/mixins/card-contents-base.js
@@ -358,8 +358,7 @@ export default Mixin.create({
   },
 
   keyUp(e) {
-    if (e.keyCode === 27) {
-      // ESC
+    if (e.key === "Escape") {
       const target = this.cardTarget;
       this._close();
       target.focus();
diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
index 5cbdf0b..190017f 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
@@ -102,6 +102,7 @@ acceptance("Composer", function (needs) {
     const event = document.createEvent("Event");
     event.initEvent("keydown", true, true);
     event[mac ? "metaKey" : "ctrlKey"] = true;
+    event.key = "B";
     event.keyCode = 66;
 
     run(() => textarea.dispatchEvent(event));
diff --git a/app/assets/javascripts/discourse/tests/acceptance/flag-post-test.js b/app/assets/javascripts/discourse/tests/acceptance/flag-post-test.js
index 926e27b..0fbfa0a 100644

[... diff too long, it was truncated ...]

GitHub sha: e68c1d5ba5e6060977c37743d3ffa8448efdb316

This commit appears in #13795 which was approved by ZogStriP. It was merged by CvX.