Created
May 18, 2012 15:33
-
-
Save daisy1754/2725887 to your computer and use it in GitHub Desktop.
KeydownイベントのKeycodeの値を変える(Chrome)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 日本語入力がOnになっている場合,keyupイベントには日本語入力がOffの場合と同じ値が渡りますが, | |
| * keydownイベントのkeyCodeの値は常に229になっています.keydownイベントにもキーに応じたkeyCodeが | |
| * 渡るようにするために,keyupイベントを利用したスクリプトです. | |
| * Chromeのkeyboardイベントクラス実装では,keyCodeはreadOnlyとなっているため,以下のような方法を用いています. | |
| * 参照: https://groups.google.com/forum/#!msg/chrome-api-developers-jp/nZgW_c61dvs/YO5dvpk8iREJ | |
| */ | |
| function dispatchKeyDown(receivedEvent) { | |
| var createdEvent = document.createEvent('Event'); | |
| var options = { | |
| type:'keydown', | |
| canBubble: true, | |
| cancelable: true, | |
| }; | |
| createdEvent.initEvent(options.type, options.canBubble, options.cancelable); | |
| Object.keys(receivedEvent).forEach(function(key) { | |
| if (!options.key) | |
| createdEvent[key] = receivedEvent[key]; | |
| }); | |
| createdEvent.__proto__ = document.createEvent('KeyboardEvent').__proto__; | |
| document.dispatchEvent(createdEvent); | |
| } | |
| document.addEventListener('keyup', dispatchKeyDown); | |
| document.addEventListener('keydown', function(e) {console.log(e.keyCode);}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment