| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 |
"http://www.w3.org/TR/html4/strict.dtd"> |
| 3 |
|
| 4 |
<html lang="en"> |
| 5 |
<head> |
| 6 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 7 |
<!--<meta name="viewport" content="width=device-width,height=device-height" />--> |
| 8 |
<title>Text Events</title> |
| 9 |
<meta name="author" content="Fabian Jakobs"> |
| 10 |
|
| 11 |
<style type="text/css" media="screen"> |
| 12 |
|
| 13 |
#container { |
| 14 |
border: 1px solid black; |
| 15 |
width: 600px; |
| 16 |
} |
| 17 |
|
| 18 |
#canvas { |
| 19 |
border: 1px solid black; |
| 20 |
margin: 4px; |
| 21 |
width: 590px; |
| 22 |
height: 400px; |
| 23 |
} |
| 24 |
|
| 25 |
#text { |
| 26 |
position: absolute; |
| 27 |
} |
| 28 |
|
| 29 |
</style> |
| 30 |
|
| 31 |
</head> |
| 32 |
<body> |
| 33 |
|
| 34 |
<div id="container"> |
| 35 |
<textarea id="text"></textarea> |
| 36 |
<div id="canvas"></div> |
| 37 |
</div> |
| 38 |
|
| 39 |
<input type="button" value="Clear" id="some_name" onclick="document.getElementById('logger').innerHTML = ''"> |
| 40 |
<div id="logger"> |
| 41 |
|
| 42 |
</div> |
| 43 |
|
| 44 |
<script type="text/javascript" charset="utf-8"> |
| 45 |
|
| 46 |
if (!window.console) window.console = {}; |
| 47 |
if (!console.log) { |
| 48 |
var logger = document.getElementById("logger"); |
| 49 |
console.log = function() { |
| 50 |
logger.innerHTML += Array.prototype.join.call(arguments, ", ") + "<br>"; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
function addListener(elem, type, callback) { |
| 55 |
if (elem.addEventListener) { |
| 56 |
return elem.addEventListener(type, callback, false); |
| 57 |
} |
| 58 |
if (elem.attachEvent) { |
| 59 |
elem.attachEvent("on" + type, function() { |
| 60 |
callback(window.event); |
| 61 |
}); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
var container = document.getElementById("container"); |
| 67 |
var canvas = document.getElementById("canvas"); |
| 68 |
var text = document.getElementById("text"); |
| 69 |
|
| 70 |
function log(e) { |
| 71 |
console.log(e.type, e); |
| 72 |
} |
| 73 |
|
| 74 |
function logKey(e) { |
| 75 |
console.log(e.type, e.charCode, e.keyCode, e); |
| 76 |
} |
| 77 |
|
| 78 |
addListener(canvas, "mousedown", function(e) { |
| 79 |
text.focus(); |
| 80 |
e.preventDefault(); |
| 81 |
}, false); |
| 82 |
|
| 83 |
var pos; |
| 84 |
addListener(canvas, "touchstart", function(e) { |
| 85 |
text.value = "" |
| 86 |
pos = e.touches[0] |
| 87 |
text.focus(); |
| 88 |
}, false); |
| 89 |
addListener(canvas, "contextmenu", function(e) { |
| 90 |
text.value = "xxxxx"; |
| 91 |
var rect = canvas.getBoundingClientRect() |
| 92 |
// text.selectionStart = 0 |
| 93 |
// text.selectionEnd = 20 |
| 94 |
text.style.opacity = 0 |
| 95 |
text.style.top = pos.clientY - 15 - rect.top + "px" |
| 96 |
text.style.left = pos.clientX - 15 - rect.left + "px" |
| 97 |
// canvas.style.fontSize = "300px" |
| 98 |
// text.readOnly = true |
| 99 |
// text.focus(); |
| 100 |
text.style.width = "50px" |
| 101 |
text.style.height = "50px" |
| 102 |
text.select(); |
| 103 |
setTimeout(function() { |
| 104 |
text.readOnly = false |
| 105 |
// text.style.top = pos.clientY - 5 + "px" |
| 106 |
// text.style.left = pos.clientY - 5 + "px" |
| 107 |
}, 100) |
| 108 |
//e.preventDefault(); |
| 109 |
}, false); |
| 110 |
|
| 111 |
addListener(text, "keydown", logKey, false); |
| 112 |
addListener(text, "keyup", logKey, false); |
| 113 |
addListener(text, "keypress", logKey, false); |
| 114 |
|
| 115 |
addListener(text, "textInput", function(e) { |
| 116 |
console.log(e.type, e.data, e); |
| 117 |
}, false); |
| 118 |
|
| 119 |
var i = 0; |
| 120 |
function fillSelection() { |
| 121 |
text.value = "Juhu Kinners " + (i++); |
| 122 |
text.select(); |
| 123 |
} |
| 124 |
|
| 125 |
addListener(text, "copy", fillSelection, false); |
| 126 |
addListener(text, "paste", log, false); |
| 127 |
addListener(text, "cut", fillSelection, false); |
| 128 |
|
| 129 |
addListener(text, "beforecopy", log, false); |
| 130 |
addListener(text, "beforepaste", log, false); |
| 131 |
addListener(text, "beforecut", log, false); |
| 132 |
|
| 133 |
addListener(text, "compositionstart", log, false); |
| 134 |
addListener(text, "compositionupdate", log, false); |
| 135 |
addListener(text, "compositionend", log, false); |
| 136 |
|
| 137 |
addListener(text, "propertychange", function(e) { |
| 138 |
console.log(e.type, e.propertyName, e); |
| 139 |
}, false); |
| 140 |
|
| 141 |
</script> |
| 142 |
|
| 143 |
</body> |
| 144 |
</html> |