0

I am trying to listen to a keydown and keyup event in a web app and need to retrieve the key. On desktop, this works fine with the following code.

const inputElement = document.querySelector("input");
const keydownList = document.getElementById("keydownList");
const keyupList = document.getElementById("keyupList");

inputElement.addEventListener("keydown", (e) => {
  const li = document.createElement("li");
  li.innerText = e.key;
  keydownList.appendChild(li);
});

inputElement.addEventListener("keyup", (e) => {
  const li = document.createElement("li");
  li.innerText = e.key;
  keyupList.appendChild(li);
});
<h2>Input</h2>
<input type="text">
<h2>Keydown</h2>
<ul id="keydownList"></ul>
<h2>Keyup</h2>
<ul id="keyupList"></ul>

The problem is that on mobile this does not work (at least, I tested on Android). Firefox just outputs "Process" every time instead of the key, and Chrome outputs "Unidentified". The same applies to the corresponding jquery version of the code.

How can one fix this? (I have tried to do some research on this, but was not able to find a solution.)

Script Raccoon
  • 261
  • 2
  • 14
  • I can confirm that no `keydown`/`keyup` is sent on iOS either. I think the best you can do is use `input`, which doesn't give you quite the same information. :-( – T.J. Crowder Nov 10 '21 at 16:45
  • To the person who closed this as duplicate: I already saw that thread, but it does not offer an answer. Please look at the answers there. It just says "keydown works as expected" - no, it doesn't. See also the comment there: "keydown is now broken". – Script Raccoon Nov 10 '21 at 17:16
  • I asked 3 questions on this site so far (including all details), and every one was downvoted, closed, etc. No single helpful answer. – Script Raccoon Nov 10 '21 at 17:19
  • I don't understand what you mean by "it doesn't offer an answer." It quite clearly does: Use `input` instead, you can't get `keydown`/`keyup` on mobile browsers. It's not the answer you want (nor the answer I'd want), but it is an answer. – T.J. Crowder Nov 10 '21 at 17:23
  • If you find your questions are regularly downvoted, I suggest taking the [tour] (you get a badge!), having a look around, and reading through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). If they're regularly closed as duplicates, try [thorough searching](/help/searching) before posting. – T.J. Crowder Nov 10 '21 at 17:25
  • Ok, thank you. The input event works, but I have to tweek the function then to get what I want. – Script Raccoon Nov 10 '21 at 17:51

0 Answers0