Even werkend voorbeeldje (IE only, maar is met wat truuken ook wel werkend te krijgen in Mozilla - je moet dan echter onkeypress gebruiken omdat onkeydown in Moz niet te cancellen is):
HTML:
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
32
33
34
35
36
37
38
39
40
41
42
43
| <html>
<head>
<script type="text/javascript">
var target = null;
function getTarget(el) {
target = el;
}
function storeCursor(el) {
if (document.all && el.createTextRange) {
el.cursorPos = document.selection.createRange().duplicate();
}
}
function dokeydown() {
if (target && event.keyCode == 9) {
target.cursorPos.text = '\t';
// cancel event
event.cancelBubble = true;
event.keyCode = 0;
event.returnValue = 0;
target.focus();
storeCursor(target);
}
}
</script>
</head>
<body>
<textarea style="height:200px;width:300px;"
onkeydown="dokeydown()"
onfocus="getTarget(this)"
onselect="storeCursor(this)"
onkeyup="storeCursor(this)"
onclick="storeCursor(this)"></textarea>
</body>
</html> |
In feite kan je bijna alle keys afvangen, inclusief functionkeys