Skip to content

Instantly share code, notes, and snippets.

@darobin
Last active March 9, 2020 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darobin/0204811507bb043670b68cf7b1b10614 to your computer and use it in GitHub Desktop.
Save darobin/0204811507bb043670b68cf7b1b10614 to your computer and use it in GitHub Desktop.
Having fun with composition events!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CompositionUpdate</title>
</head>
<body>
<input id="input">
<pre id="log"></pre>
<script>
var input = document.getElementById('input')
, log = document.getElementById('log')
;
['compositionstart', 'compositionupdate', 'compositionend', 'keydown']
.forEach(function (event) {
input.addEventListener(event, function (ev) {
log.textContent += event + ': ' + (ev.data || ev.keyCode) + '\n';
}, true);
})
;
</script>
</body>
</html>

Composition Events

Running the above HTML file (see pen), and composing an à on my Mac keyboard (Alt-`, a) I get the following results:

Gecko Chromium WebKit
keydown: 18 keydown: 18 keydown: 18
keydown: 192 keydown: 229 compositionstart: 0
compositionstart: undefined compositionstart: undefined compositionupdate: `
compositionupdate: ` compositionupdate: ` keydown: 229
compositionupdate: à keydown: 229 compositionend: à
compositionend: à compositionupdate: à keydown: 229
                          | `compositionend`: à           |

I'm using keyCode instead of key because WebKit. The differences between the keydown values are not that important. The difference in event order, and the fact that Firefox triggers no keydown after composition is initiated, Chrome gives you one in the middle, and Safari throws in an extra one at the end is fun!

@alystair
Copy link

alystair commented Mar 9, 2020

Hey @darobin, what party is responsible for what actions trigger the composition events? One would think the emoji inputs (such as Windows 10 or on mobile devices) would trigger this - but they don't. Who should I mention this to?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment