Personal tools
You are here: Home ブログ hamabe Stuff コナミコマンド with JavaScript サンプル
« December 2010 »
Su Mo Tu We Th Fr Sa
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Recent comments
Re:コマンド入力を実装しよう inoue 2007-08-14
 
Document Actions

コナミコマンド with JavaScript サンプル

Click here to get the file

Size 5.9 kB - File type text/html

File contents

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="content-style-type" content="text/css">
  <meta http-equiv="content-script-type" content="text/javascript">
  <title>コナミコマンド with JavaScript サンプル</title>
<style type="text/css">
body {
  background-color: #eef;
}

dl {
  width: 700px;
  margin: 1.2em;
}

dt {
  font-weight: bold;
  border-left: 10px solid #777;
  border-bottom: 1px dashed #777;
  margin-bottom: 1ex;
  padding: 3px;
}

dd {
  border-bottom: 1px dotted #aaa;
  margin-bottom: 0.5ex;
}
</style>
</head>
<body>
<h1>コナミコマンドサンプル</h1>
<span style="color:red;">日本語入力がオフになっていることを確認してください</span>

<h3>このページで使用可能なコマンド&ソース</h3>
<dl>
  <dt>[s][u][m][o][m][o][m][o][m][o][m][o][m][o][m][o][m][o][n][o][u][t][i]</dt>
  <dd>結構長めの文字列でも平気!</dd>
  <dd style="color:#060; font-size:85%;">KONAMI.command(function(){ alert('すもももももももものうち') }, 'sumomomomomomomomonouti');</dd>
  <dt>[m][a][i][l][ ][t][o][ ][h][o][g][e]</dt>
  <dd>自然言語っぽくコマンドを実装</dd>
  <dd style="color:#060; font-size:85%;">KONAMI.command(function(){ window.open('mailto:hoge@fuga') }, 'mail to hoge');</dd>
  <dt>[H][e][l][l][o][ ][W][o][r][l][d][*][*]</dt>
  <dd>ワイルドカード使用可(使用できないキーは自動でワイルドカードに置き換わる)</dd>
  <dd style="color:#060; font-size:85%;">KONAMI.command(function(){ alert('Hello World!!') }, 'Hello World!!');</dd>
  <dt>[down][right][p]</dt>
  <dd>特殊キー使用可</dd>
  <dd style="color:#060; font-size:85%;">KONAMI.command(function(){ alert('波動拳!') }, 'down,right,p');</dd>
  <dt>[up][up][down][down][left][right][left][right][b][a]</dt>
  <dd>コマンドを指定しない場合はコナミコマンド</dd>
  <dd style="color:#060; font-size:85%;">KONAMI.command(function(){ document.write('&lt;style&gt;*{position:relative}&lt;/style&gt;&lt;table&gt;&lt;input&gt;&lt;/table&gt;') });</dd>
  <dd>(↑IE6のみ有効) - <a href="http://d.hatena.ne.jp/Hamachiya2/20070804/browser_crasher" target="_blank">IEを華麗に撃墜する一行</a></dd>
</dl>

<dl>
  <dt>inputやtextarea上ではコマンド受けつけません</dt>
  <dd><input type="text" /></dd>
  <dd><textarea></textarea></dd>
</dl>

<dl>
  <dt>一応コマンド入力受けつけのdisabledを設定できます</dt>
  <dd>コマンド入力<a href="javascript:KONAMI.disabled()">禁止</a></dd>
  <dd>コマンド入力<a href="javascript:KONAMI.enabled()">許可</a></dd>
</dl>

</body>

<script type="text/javascript">
<!--
var KONAMI = function() {
  var command_list = [];
  var disabled = false;
  var permit_key = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var func_key = {
     8: 'back',  9: 'tab',    13: 'enter',    16: 'shift', 17: 'ctrl', 18: 'alt',
    27: 'esc',  33: 'pageup', 34: 'pagedown', 35: 'end',   36: 'home',
    37: 'left', 38: 'up',     39: 'right',    40: 'down',  46: 'delete'
  };

  var make_key = function(key) {
    if (!key) return ['up','up','down','down','left','right','left','right','b','a'];
    if (typeof(key) == 'string') key = key.split(',');
    var temp = [];
    for (var i = 0, len = key.length; i < len; i++) temp = temp.concat(parse(key[i]));
    return temp;
  };

  var parse = function(key) {
    for (var i in func_key) if (key == func_key[i]) return [key];
    var arr_char = key.split('');
    for (var i = 0, len = arr_char.length; i < len; i++) {
      if (permit_key.indexOf(arr_char[i]) == -1) arr_char[i] = '*';
    }
    return arr_char;
  };
  
  var kc2char = function(kc) {
    if (kc >= 48 && kc <= 57) return String.fromCharCode(kc);
    if (kc >= 96 && kc <= 105) return String.fromCharCode(kc-48);
    if (kc >= 65 && kc <= 90) return String.fromCharCode(kc+32);
    if (!!func_key[kc]) return func_key[kc];
    return (kc == 32) ? ' ' : '*';
  };
  
  var check_key = function(evt) {
    if (disabled) return;
    if(/input|textarea/i.test((evt.target||evt.srcElement).tagName)) return;

    var c = kc2char(evt.keyCode);
    if (c.length == 1 && evt.shiftKey) c = c.toUpperCase();

    for (var i = 0, len = command_list.length; i < len; i++) {
      var command = command_list[i];
      if (!command.cache) command.cache = [].concat(command.key);

      if (command.cache[0] == '*' || command.cache[0] == c) {
        command.cache.shift();
        if (command.cache.length == 0) {
          command.action();
          command.cache = [].concat(command.key);
        }
      } else if (c == 'shift' || c == 'ctrl' || c == 'alt') {
        continue;
      } else {
        command.cache = [].concat(command.key);
        if (command.cache[0] == c) command.cache.shift();
      }
    }
  };

  if (document.addEventListener) { document.addEventListener('keyup', check_key, true); }
  else if (document.attachEvent) {
    document.attachEvent('onkeyup', check_key);
    var unload = function(){ document.detachEvent('onkeyup', check_key); window.detachEvent('onunload', this); };
    window.attachEvent('onunload', unload);
  }

  return {
    command: function(action, key) { command_list.push({ action:action, key:make_key(key) }); },
    disabled: function() { disabled = true; },
    enabled: function() { disabled = false; }
  };
}();

KONAMI.command(function(){ alert('すもももももももものうち') }, 'sumomomomomomomomonouti');
KONAMI.command(function(){ window.open('mailto:hoge@fuga') }, 'mail to hoge');
KONAMI.command(function(){ alert('Hello World!!') }, 'Hello World!!');
KONAMI.command(function(){ alert('波動拳!') }, 'down,right,p');
KONAMI.command(function(){ document.write('<style>*{position:relative}</style><table><input></table>') });

//-->
</script>
</html>

Copyright(C) 2001 - 2006 Ariel Networks, Inc. All rights reserved.