Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

EdgeのIEモードだとJavaScriptでCtrl-Nの入力をキャプチャできない

IEではCtrl-Nの入力に反応する処理をonkeydownイベントで実装できたんだけど、EdgeのIEモードではそれが使えなくなっているらしいので実験した。

準備

  • ReplitでこんなHTMLとJavaScriptを用意しておく。
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>replit</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <ul id="parent"></ul>
  <script src="script.js"></script>
</body>

</html>
function consoleKeycode(event) {
  var ul = document.getElementById("parent");
  var li = document.createElement("li");
  li.textContent = event.key + ", Ctrl = " + event.ctrlKey;
  ul.appendChild(li);
  event.stopPropagation();
  event.stopImmediatePropagation();
  event.preventDefault();
  event.keyCode = 0;
  return false;
}

document.onkeydown = consoleKeycode;
(New-Object -ComObject InternetExplorer.Application).Visible = $true
  • 上記Replitのページ(Replitの右ペインでWebviewのアドレスバーに表示されているURL。https://【repl名】.【ユーザ名】.repl.co)をIEとEdgeでそれぞれ開く。Edgeは「Internet Explorerモードで再読み込みする」を実行しIEモードにしておく。

  • 以下の処理を「SendKeys.vbs」として保存する。

Set WshShell = CreateObject("WScript.Shell")
WScript.Sleep(2000)
WshShell.SendKeys ("^n")
  • SendKeys.vbsをダブルクリックで実行したあと、2秒以内にブラウザウィンドウにフォーカスを当てる。

結果

いずれの場合も、まずCtrlキーが入力され、次にCtrl-Nが入力された動きになる。

  • IEでは以下のように表示され、新しいウィンドウは開かない。
•Control, Ctrl = true
•n, Ctrl = true
  • EdgeのIEモードでは以下のように表示され、新しいウィンドウが開く。
•Control, Ctrl = true
  • EdgeをAppモードで開いたうえでIEモードにした場合 *1 も、以下のように表示され、新しいウィンドウが開く。
•Control, Ctrl = true

*1:Edgeの設定で「Internet Explorerページ」にReplitのURLを指定したうえで、PowerShellから「& 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe' --app="https://【repl名】.【ユーザ名】.repl.co"」で起動する。