HTML / DOM ã«ããããã¼ãã¼ãã¤ãã³ãå¨ãã®è©±
æè¿ãã¼ãã¼ãã·ã§ã¼ãã«ããã®å®è£ ããããã¨æã£ã¦ãã¼å ¥åã«ããã¤ãã³ãå¨ãã«ã¤ãã¦èª¿ã¹ã¦ã¿ãã®ã ããã©ãæ¥æ¬èªã§ã¾ã¨ã¾ã£ãæ å ±ãè¦ã¤ãããªãã£ãã®ã§ããã¼ãã¼ãã·ã§ã¼ãã«ããã®å®è£ ã«å¿ è¦ãããªå 容ãç°¡åã«ã¾ã¨ãã¦ãããã¨æãã ãã¼ãã¼ãã·ã§ã¼ãã«ããã«éããããã¼å ¥åã«ããã¤ãã³ãå¨ãã®ä½ããããå ´åã«ã¯åèã«ãªãã¨æãã
æ¬è¨äºã§ã¯ãDOM 3 Events spec ã® 2012-09-06 ã®çãåç §ãã¦ãããå°æ¥ã®çã§ã¯å¤æ´ããã¦ããå¯è½æ§ãããã ææ°ã®çã¯ä¸è¨ãªã³ã¯ãã確èªã®ãã¨ã
- Document Object Model (DOM) Level 3 Events Specification (DOM 3 Events spec ã®ææ°å®å®æ¿)
keydown ã¤ãã³ããkeypress ã¤ãã³ããkeyup ã¤ãã³ãã«ã¤ãã¦
- keydown イベント ã¯ããã¼ãæ¼ãããã¨ãã«çºçããã¤ãã³ã
- æ¼ããããã¼ã®ç¨®é¡ã«ãã£ã¦ããã©ã«ãã¢ã¯ã·ã§ã³ãç°ãªã
- keydown ã¤ãã³ãã®ããã©ã«ãã¢ã¯ã·ã§ã³ã®ãã£ã³ã»ã«ã¯ keypress ã¤ãã³ãã®çºçãé»å®³ããããkeyup ã¤ãã³ãã®çºçã¯é»å®³ããªã (åè: キーボードイベントのキャンセル)
- keypress イベント ã¯ã文字値 (character value) ãçæããã®ãé常ã®åä½ã§ãããããªãã¼ãæ¼ãããã¨ãã«çºçããã¤ãã³ã
- keypress ã¤ãã³ããçºçããã®ã¯ keydown ã¤ãã³ãã®ããã©ã«ãã¢ã¯ã·ã§ã³ã¨ãã¦
- DOM 3 Events ã«ãã㨠keypress ã¤ãã³ã㯠Deprecated ãªã®ã§ã使ããªãã¦æ¸ããªã使ãã¹ãã§ã¯ãªã
- ä¾ãã°ãããã¹ãã¨ãªã¢ãªã©ã¸ã®ãã¼å ¥åã®æ¤åºã«ã¯ input イベント ã使ãã¹ã
- keyup イベント ã¯ããã¼ãé¢ãããã¨ãã«çºçããã¤ãã³ã
KeyboardEvent ã¤ã³ã¿ã¼ãã§ã¤ã¹
keydown ã¤ãã³ããkeypress ã¤ãã³ããkeyup ã¤ãã³ãã«å¯¾ããã¤ãã³ããªã¹ãã«ã¯ KeyboardEvent インターフェイス ãå®è£ ãããªãã¸ã§ã¯ãã渡ããã¦ããã ãã®ãªãã¸ã§ã¯ãã¯ãã©ã®ãã¼ãæ¼ãããããç¹å®ããããã® key å±æ§ã¨ char å±æ§ãæã£ã¦ããã
KeyboardEvent#char å±æ§
KeyboardEvent#char 属性 ã«ã¯ãæ¼ããããã¼ã®æåå¤ (character value) ãå ¥ã£ã¦ããã ããããã¼æ¼ä¸ã表示å¯è½ãªè¡¨ç¾ (printed representation) ãæã£ã¦ããå ´åã¯ããã®å±æ§ã¯ç©ºæååã§ã¯ãªãã¦ãã³ã¼ãæååã«ãªã *1ã æ¼ããããã¼ããã¯ãã§è¤æ°æåãæ¿å ¥ãããããªãã®ã§ããå ´åãªã©ããã®å±æ§ã¯è¤æ°æåãããªãæååã«ãªãå¾ãã
æ¼ä¸ããããã¼ã表示å¯è½ãªæåå表ç¾ãæããªãå ´åã¯ã空æååã
KeyboardEvent#key å±æ§
KeyboardEvent#key 属性 ã«ã¯ãæ¼ããããã¼ã®ãã¼å¤ (key value) ãå ¥ã£ã¦ããã ãã®å¤ã表示å¯è½ãªè¡¨ç¾ãæã¤å ´åã¯ãkey å±æ§ã®å¤ã¯ char å±æ§ã«é©åãã (match) å¤ *2 ã«ãªããããã§ãªãå ´å㯠key values set ã§å®ç¾©ããããã¼å¤ã®ãã¡ã® 1 ã¤ã«ãªãã
å®è·µçãªè©±
ãããã char å±æ§ã¨ key å±æ§ã¯ãåºã使ããã¦ãããã©ã¦ã¶ã®ä¸ã§ã¯ã¾ã IE 㨠Opera ã«ããå®è£ ããã¦ããªã (IE 10 㨠Opera 12.14 ã§ç¢ºèª)ã Firefox ã¯å®è£ ä½æ¥ä¸ã£ã½ã (680830, Bugzilla)ã ãªã®ã§ãå¤ãã®ãã©ã¦ã¶ããµãã¼ãããå¿ è¦ãããå ´åã¯ãå¾è¿°ãã charCode ã keyCode ãä½µç¨ããå¿ è¦ãããã
char å±æ§ã¨ key å±æ§ã使ãå ´åãä¾ãã° Enter ãã¼ãå ¥åããããã©ããã調ã¹ãã«ã¯ã以ä¸ã®ããã« key å±æ§ã®å¤ã "Enter" ã§ãããchar å±æ§ã®å¤ã key å±æ§ã®å¤ã¨ç°ãªããã¨ããã§ãã¯ããã®ãè¯ãããã (char å±æ§ã key å±æ§ã¨ç°ãªããã¨ã調ã¹ãã®ã¯ãä¾ãã°ãã¯ã㧠"Enter" ã¨ããæååãå ¥åãããã¼ãåå¨ããå ´åã«ã evt.key 㯠"Enter" ã«ãªã *3 ããã)
elem.addEventListener("keydown", function (evt) { // Enter ãã¼ãæ¼ä¸ããã? if (evt.key === "Enter" && evt.key !== evt.char) { // Enter ãã¼ãæ¼ä¸ããã!! } }, false);
æåã表ããã¼ãå ¥åããããã©ããã調ã¹ãå ´å㯠char å±æ§ã ããè¦ãã°è¯ãããã
ã¬ã¬ã·ã¼ãª KeyboardEvent ã®ããã®è£å©ã¤ã³ã¿ã¼ãã§ã¤ã¹
char å±æ§ã key å±æ§ã KeyboardEvent ã§ãµãã¼ãããã¦ããªãå®è£ ã®å ´åã代ããã« charCode å±æ§ã keyCode å±æ§ã使ãå¿ è¦ãããã ãããã¯ãã¬ã¬ã·ã¼ãª KeyboardEvent ã®ããã®è£å©ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¨ã㦠DOM 3 Events ã§å®ç¾©ããã¦ããã
charCode å±æ§
æåå ¥åãå¼ãèµ·ãã keypress ã¤ãã³ããè£è¶³ããã¨ãã® Event ãªãã¸ã§ã¯ãã® charCode 属性 ã¯ãæåå¤ (character value) ãä¿æããã ãã®å¤ã¯ãå ¥åæåã® Unicode ã³ã¼ããã¤ã³ãã§ããã keydown ã keyup ã¤ãã³ãã§ã¯ãå¤ã¯ 0 ã«ãªãã
keyCode å±æ§
keyCode 属性 ã¯ãã·ã¹ãã ä¾åãã¤å®è£ ä¾åã®ãæ¼ä¸ããããã¼ã®èå¥åã示ãç¡ä¿®é£¾ (unmodified) *4 ã®æ°å¤ã³ã¼ã (numerical code) ãä¿æããã 詳細ã«ã¤ãã¦ã¯æ¬¡ã®ã»ã¯ã·ã§ã³ã«æ¸ããã¦ããã
ã·ã¹ãã ä¾åãã¤å®è£ ä¾åã§ã¯ããã®ã ããã©ãIME (Input Method Editor) ã®å¦çãåãã¦ãã¦ããã¤ã¤ãã³ãã®ç¨®é¡ã keydown ã®å ´åã«ã¯ 229 ã«ãªãã¨ããæ°åã®å ¥åã®éã¯ãã® ASCII ã³ã¼ããè¿ãã¨ããFixed virtual key codes ã®è¡¨ã«ãããã¼ (Enter ãã¼ã¨ãç¢å°ãã¼ã¨ã Detele ãã¼ã¨ããåºæ¬çãªå¶å¾¡ãã¼ãä¸éãå«ã¾ãã¦ãã) ã«ã¤ãã¦ã¯ããã使ãããã¨ããè¤æ°ã®ã·ã¹ãã ãå®è£ ã§ãåæ§ã«æ±ããé¨åãçµæ§ããã
ã¡ãªã¿ã« keypress ã¤ãã³ãã«ã¤ãã¦ã¯ãcharCode å±æ§ã«ã keyCode å±æ§ã«ãåãå¤ãå ¥ã£ã¦ããå®è£ ã¨ãcharCode å±æ§ã«ã®ã¿å¤ãå ¥ã£ã¦ã㦠keyCode å±æ§ã¯å¸¸ã« 0 ã§ãããããªå®è£ ã® 2 種é¡ããã (Firefox 19 ã¯åè ãIE 10 ã¯å¾è ã ã£ã)ãã¨ãããã¨ã« DOM 3 Events ä¸ã§ã¯ãªã£ã¦ããã ãããä»ã«ããcharCode å±æ§ããããããªãã¦ãä»æ§ä¸ã¯ charCode å±æ§ã«å ¥ã£ã¦ããã¹ãå¤ã keyCode å±æ§ã«å ¥ã£ã¦ããå®è£ ããã (IE 6, 8 ã§ç¢ºèªã keypress ã¤ãã³ãã§ã charCode å±æ§ã¯ undefined ã§ãcharCode ã«å ¥ã£ã¦ããã¹ãå¤ã keyCode ã«å ¥ã£ã¦ããã)ã
å®è·µçãªè©±
ã¾ããkeyCode å±æ§ã¨ charCode å±æ§ã®ä½¿ãåãã ãã以ä¸ã®ããã«ããã®ãæ£ãããã
- keydown ã¤ãã³ã㨠keyup ã¤ãã³ãã§ã¯ keyCode å±æ§ãã¿ã
- keypress ã¤ãã³ãã§ã¯ charCode å±æ§ãã¿ã¦ãundefined ã®å ´å㯠keyCode ã使ã
ã¾ããã©ã®ããã«ãã¼ãã¼ãã¤ãã³ããå¦çããã®ãã«ãããããåºæ¬çã«ã¯ä»¥ä¸ã®ããã«ããã®ãè¯ãããã
- Enter ãã¼ã Esc ãã¼ãç¢å°ãã¼ãªã©ã®å¶å¾¡ãã¼ã¯ keydown ã¤ãã³ãã§ææãã¦å¦çãã
- ã¢ã«ãã¡ããããæ°åãã¼ããã®ä»è¨å·ãªã©ã®æåå ¥å㯠keypress ã¤ãã³ãã§ææãã¦å¦çãã
ãã¡ãããkey å±æ§ã char å±æ§ã使ããå ´åã«ã¤ãã¦ã¯ãå ¨ã¦ keydown ã¤ãã³ããææãã¦å¦çãã¹ãã§ããã
ã¤ãã³ãã®çºçé åºã¨ãç¹°ãè¿ãã®è©±
keydown ã¤ãã³ã㨠keypress ã¤ãã³ãã¯é çªã«çºçããã ãã¼ãæ¼ãç¶ãã¦ããå ´åãkeydown ã¤ãã³ã㨠keypress ã¤ãã³ã *5 ãç¹°ãè¿ãçºçããã ãã®ééã¯ãã¡ããã·ã¹ãã ä¾åã ãã¼ãé¢ã㨠keyup ã¤ãã³ããçºçããã
ã¨ããããæè¿ã®ãã©ã¦ã¶ã¯å¤§ä½ããã«ãã£ãå®è£ ã«ãªã£ã¦ãããã *6ã
DOM 3 Events ã«ã¯ãç¹°ãè¿ãã®å ´åã¯ã¤ãã³ããªãã¸ã§ã¯ãã® repeat
å±æ§ã true
ã«ãªãã£ã¦æ¸ãã¦ãããã©ãå®è£
ããã¦ãããã©ã¦ã¶ã¯ãã¾ããªãããã IE 10, Opera 12.14 ã 㨠repeat å±æ§ã¯å¸¸ã« false
ã£ã½ãã Firefox 19 㨠Chrome 25 ã 㨠repeat å±æ§ã¯å®è£
ããã¦ãªãã IE 6, IE 8 ã§ã¯ã¡ããã¨å®è£
ããã¦ãã ãªãã§å¤ã IE ã§å®è£
ããã¦ã¦ IE 10 ã§åãã¦ãªããã ã...ã
å®è·µçã«ã¯ãkeyup ã¤ãã³ããçºçããã¾ã§ã¯ãã®ãã¼ãæ¼ããç¶ãã¦ãããã¨ãããããªå¤ææ¹æ³ãããã®ãè¯ãããã
Input Method Editor ã«é¢ããã¤ãã³ã
Input Method Editor (IME) ãªã©ã® text composition system ã«ããããã¹ãç·¨éä¸ã§ãããã¼ãæ¼ä¸ããã㨠keydown ã¤ãã³ããçºçãã *7ã ããããªãããtext composition system ãåãã¦ããã¨ãã¯ãã¼æ¼ä¸ã«ããã¤ãã³ããã¹ã«ã¼ãããã¨ããå ´é¢ã¯å¾ã ã«ããã¨æãã ä¾ãã°ãããã¹ãå ¥åæ¬ã«ä½ãããã®ãã¼ãã¼ãã·ã§ã¼ãã«ãããä»ãã¦ããå ´å (Enter ãã¼ã§éä¿¡ã¨ã) ãªã©ãããã§ããã
IME ã®ã»ãã·ã§ã³éå§æ (ããã¹ãç·¨ééå§æ) ã«ã¯ compositionstart ã¤ãã³ãããã»ãã·ã§ã³çµäºæã«ã¯ compositionend ã¤ãã³ããçºçããã æåå¤æ´æ㮠詳細ã¯ä¸è¨ãã¼ã¸åç §ã
å¥ã®æ¹æ³
text composition system が動いているときには、keydown イベントで渡されるイベントの keyCode は 229 にするように書かれている ã®ã§ãCompositionEvent ããµãã¼ãããã¦ããªãç°å¢ã§ããkeyCode ãè¦ã¦ 229 ãªã text composition system ãåãã¦ããå¤æãããã¨ãå¯è½ã§ããã (Opera 12.14 ã 㨠CompositionEvent ããµãã¼ãããã¦ããªãã®ã§ãkeyCode ãè¦ãæ¹æ³ãè¯ãããã)
ãã¨ã¯ãtext composition system ãåãã¦ããå ´åã«ã¯ keypress ã¤ãã³ããçºçããªãç°å¢ãå¤ãã¨æãã®ã§ãkeydown ã¤ãã³ãã§å¶å¾¡ãã¼ãåãåã£ã¦ãkeypress ã¤ãã³ãã§å®éã«åãããã¨ãããããªãã¨ãããã°ããã ã¨æããããããã keydown ã¤ãã³ã㧠keyCode ãè¦ãå ´åã¯ãIDE ãåãã¦ãã㨠keyCode 㯠229 ã«ãªãã®ã§ãããªã«æ°ã使ãå¿ è¦ã¯ãªãããã
æ°ãä»ããã¹ã㯠key å±æ§ã char å±æ§ã使ãå ´åã®ã¿ãããªã
æçµçã«
ãã¼ãã¼ãã·ã§ã¼ãã«ãããå®è£ ãããªã以ä¸ã®ãããªæµãã§ã¤ãã³ãå¦çãããã®ãè¯ãããã
- keydown ã¤ãã³ããæ¤ç¥ãã¦
- evt.key, evt.char ãå®è£ ããã¦ããå ´å evt.char ã使ã£ã¦å¦ç
- ããã§ãªãå ´åãevt.keyCode ãè¦ã¦ã
- Fixed virtual key codes ã«å«ã¾ãã¦ãããã®ãªãããã使ã
- ãããªããã° keypress ã¤ãã³ãã«ä»»ãã
- keypresss ã¤ãã³ã㧠String.fromCharCode(evt.charCode || evt.keyCode) ãè¦ã¦ã対象ã¨ãªãæåãªããªãããã
ãã¨ã¯ãããã¹ãå ¥åæ¬ã«ãã¼ãã¼ãã·ã§ã¼ãã«ãããã¤ããå ´å㧠evt.key ã evt.char ã使ãå ´åã«é¢ãã¦ã¯ãtext composition system ã®ç·¨éã®ã»ãã·ã§ã³ä¸ãã©ãããæ°ã«ãã¹ããã£ã¦ã¨ãããã
è£è¶³
IE 10 ã 㨠evt.key ã evt.char ã便å©ã«ä½¿ãããããªã®ã§ãWindows ã¹ãã¢ã¢ããªã§ã·ã§ã¼ãã«ãããã¼ãå®è£ ããã¨ãã«ã¯ãããã®å±æ§ã使ãã¨ããã¨æãã¾ãã Web ä¸ã ã¨ããã¯ãã¾ã ãµãã¼ããã¦ãããã©ã¦ã¶ãå°ãªãã®ã§ãä»ã®ã¨ããã¯ã¬ã¬ã·ã¼ãª API ã ãã使ãããã«ããæ¹ãæ¡ä»¶åå²ãå°ãªãã¦æ¥½ãããããªãã§ããã
追è¨: ã²ã¼ã ã RIA ã®ã·ã§ã¼ãã«ããã®ããã«ã¯ DOM 3 Events ã® API ã§ãä¸è¶³ãã¦ããã¨ãã話
ä»åç§ãå®è£ ãããã¨æã£ã¦ããã·ã§ã¼ãã«ããã¯ãä¾ãã°ããã¹ãå ¥åæ¬ (textarea è¦ç´ ) 㧠Ctrl + Enter ãæ¼ããã¨ãã«æ稿ãããã¨ãããããªãã®ã§ãã·ã§ã¼ãã«ããã使ããªãã£ããå°ãã¨ãããã®ã§ã¯ãªãã£ãã ãªã®ã§ãæ¢åã® API ã§ãã¾ããªãã¨ããªãããªãã¨ãããã®ãªã®ã ã£ãããã ãããã²ã¼ã ã Rich Internet Application (RIA) ã§ä½¿ãã·ã§ã¼ãã«ããã®ããã®æ©è½ã¨ãã¦ã¯ DOM 3 Events ã® API ã§ã¯ä¸è¶³ãã¦ããããã¨ãã話ãè¦ã¤ããã®ã§ç´¹ä»ãã¦ããã
ãã®ããã°è¨äºã§ã®ä¸»å¼µã¯ãè¦ç´ããã¨ä»¥ä¸ã®ãããªãã®ã§ããã
ã·ã§ã¼ãã«ãããã¼ããã¼ã®æåã§æå®ãã¦ãã¾ãã¨ããã®ãã¼ãæã£ã¦ããªããã¼ãã¼ãã§ã¯ä½¿ããªããã¨ãããããªåé¡ãããã ããã§ãæ§ã ãªãã¼é åã«å¯¾å¿ããããã«ä¸è¬çã«ã¯ã¦ã¼ã¶ã¼ããã¼ãã¼ãã·ã§ã¼ãã«ãããè¨å®ã§ããããã«ããã ãã®ã¨ãå¿ è¦ã¨ãªãã®ã¯ä»¥ä¸ã®ãã®ã§ããã
- ã¦ã¼ã¶ã¼ã«ãã¼ã®èª¬æãããéã«è¡¨ç¤ºã§ãããã¼ã®èª¬æ (Shift ãªã©ãåæã«æ¼ãã¦ãã¦ãå¤åããªãããã®ãã¼ã®ãã©ã¤ããªã¼ãªè¡¨ç¤ºãããã®ãã¼ã®æ©è½)
- Shift ãªã©ãåæã«æ¼ãã¦ãã¦ãå¤åããªãããã¼åºæã®èå¥å
æ®å¿µãªãã DOM 3 Events ã® API ã¯ããããæºããã¦ããªãã
ä¸è¨è¨äºã®çè 㯠W3C ã® ML ã«ããã«é¢ããæ稿ãè¡ã£ã¦ããããã®è°è«ã®ä¸ã§ãUI Events ã¨ãã¦æ°ã㪠API ã®ä»æ§çå®ãé²ãã§ãããã¨ãç¥ãããã模æ§ã
- W3C ã® ML ã¸ã®æ稿: Keyboard events for accessible RIAs and Games from Florian Bösch on 2013-01-30 ([email protected] from January to March 2013)
- DOM 3 Events ãæ¡å¼µãã UI Events ä»æ§ã® Editor's Draft: UI Events
æ¤è¨¼ç¨ãã¼ã¸
*1:algorithm for determining the key value ã«å¾ãããã«ãã¦æ±ºããããã
*2:char å±æ§ã空æååã§ãªãå ´åãkey å±æ§ã¯ char å±æ§ã¨åãã§ããå ´åãããããå¿ ãããããã§ã¯ãªãã
*3:ã¯ãã ã¨æã£ã¦ãããéããããããªã
*4:Shift ãã¼ã Alt ãã¼ãæ¼ããã¦ããã¨ãã¦ãå¤åããªãã£ã¦ãã¨ã ã¨æãã
*5:keypress ã¤ãã³ããçºçããªããããªãã¼æ¼ä¸ã®å ´å㯠keydown ã¤ãã³ãã®ã¿
*6:IE 10, 8, 6, Firefox 19, Opera 12.14, Chrome 25 ã§ç¢ºèª
*7:ãããå®è£ ä¾åã§ãIE 10 ã ã¨ãã®ããã«ãªã£ããã© Firefox 19 ã§ã¯éã£ã