|
<!DOCTYPE html> |
|
<html lang="ja"> |
|
<head> |
|
<meta charset="utf-8"> |
|
<title>IME有効/無効の検出</title> |
|
<style> |
|
pre { |
|
background-color: #ccc; |
|
color: black; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<p>last event.type = <span id="last-event-type"></span></p> |
|
<p>last CompositionEvent = <span id="last-composition-event"></span></p> |
|
<p>last isComposing = <span id="last-is-composing"></span></p> |
|
<p>last isComposing(emulated) = <span id="last-emulated-is-composing"></span></p> |
|
<input type="text"> |
|
<pre><code></code></pre> |
|
|
|
<script> |
|
'use strict'; |
|
function handleCompositionEvent (event) { |
|
const type = event.type; |
|
const isComposing = this.isComposing = type !== 'compositionend'; |
|
|
|
document.getElementById('last-event-type').textContent = document.getElementById('last-composition-event').textContent = type; |
|
document.getElementById('last-emulated-is-composing').textContent = isComposing; |
|
} |
|
|
|
function handleInput (event) { |
|
const isComposing = this.compositionListener.isComposing; |
|
const type = event.type; |
|
|
|
// console.log(event.type); |
|
document.getElementById('last-event-type').textContent = type; |
|
document.getElementById('last-is-composing').textContent = event.isComposing; |
|
document.getElementById('last-emulated-is-composing').textContent = isComposing; |
|
|
|
const code = document.querySelector('pre>code'); |
|
|
|
code.appendChild(document.createTextNode((code.firstChild ? '\n' : '') + ['type = ' + type, 'isComposing = '+event.isComposing, 'isComposing(emulated) = '+isComposing])) |
|
} |
|
|
|
const input = document.querySelector('input'), |
|
compositionListener = {isComposing: false, handleEvent: handleCompositionEvent}, |
|
inputListener = {compositionListener: compositionListener, handleEvent: handleInput}, |
|
entries = [ |
|
['input', inputListener], |
|
['keydown', inputListener], |
|
['keyup', inputListener], |
|
['compositionstart',compositionListener], |
|
['compositionupdate',compositionListener], |
|
['compositionend',compositionListener] |
|
]; |
|
|
|
|
|
for (let i = 0, len = entries.length; i < len; ++i) { |
|
const entry = entries[i]; |
|
input.addEventListener(entry[0], entry[1], false); |
|
} |
|
</script> |
|
</body> |
|
</html> |