wKeybindings v.0.1
Demo: http://demo.moonsword.info/jslibs/wKeybindings/wKeybindings.html
Leichte Verwaltung von Keybindings für Webseiten. Bindings können schnell über:
keys.addAction(keys,function)
eingebunden werden. Wobei
keys ein Array aus den Ascii zahlen der Keys ist und
function eine beliebige Javascriptanweisung sein
wKeybindings.html zeigt ein Beispiel, wie man sowas einsetzen könnte.
was nicht funktioniert ist: a+b+d....aber es funktioniert: strg+alt+s
warum, weiss ich noch nicht richtig. wer ideen hat, gerne her damit
ausserdem wird in 0.2 noch konstanten kommen, damit man nicht immer den Ascii code hat. Wer grad gute Konstantennamen hat, oder vorschläge hat, gerne per Pn an mich.
Viel Spass damit,
nuit
wKeybindings.html
<html>
<head>
<title></title>
<script type="text/javascript" src="wKeybindings.js"></script>
<script type="text/javascript">
<!--
function cat() {
document.getElementById("output".innerHTML = 'A gedrueckt';
}
// 65 = a ; 17 = Strg ; 37 = Pfeil Links ; 18 = Alt ; 83 = s
keys.addAction([65],'cat()';
keys.addAction([17,37],'document.getElementById("output".innerHTML = "Test";';
keys.addAction([17,18,83],'document.getElementById("output".style.backgroundColor = "red";';
//-->
</script>
</head>
<body>
<b>a</b> oder <b>[STRG]+ArrowLeft</b> oder <b>[STRG]+[ALT]+s</b>
<div id="output" style="padding: 5px; background-color: yellow;"></div>
</body>
</html>
wKeybindings.js
/*
wKeyBindings V0.1
Copyright (C) 2007 Jan Hoersch
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
var wKeybindings = function() {
var pressedKey;
var actions;
this.init = function() {
keys.pressedKey = new Array();
keys.actions = new Array();
document.onkeydown = function(e) {
evt = (e) ? e : ((event) ? event : null);
if(evt) {
var contain = false;
for(i = 0; i < keys.pressedKey.length; i++) {
if(keys.pressedKey[i] == evt.keyCode) {
contain = true;
}
}
if(!contain) {
keys.pressedKey.push(evt.keyCode);
}
keys.check();
}
}
document.onkeyup = function(e) {
evt = (e) ? e : ((event) ? event : null);
if(evt) {
var buffer = [];
for(i = 0; i < keys.pressedKey.length; i++) {
if(keys.pressedKey[i] != evt.keyCode) {
buffer.push(keys.pressedKey[i]);
}
}
keys.pressedKey = buffer;
}
}
}
this.check = function() {
for(i = 0; i < keys.actions.length; i++) {
if(keys.actions[i][0].length == keys.pressedKey.length) {
if(this.compare(keys.actions[i][0],keys.pressedKey)) {
eval(keys.actions[i][1]);
return true;
}
}
}
}
this.compare = function(array_one,array_two) {
buffer = [];
b = 0;
for(a = 0; a < array_one.length; a++) {
for(z = 0; z < array_two.length; z++) {
var contain = false;
for(y = 0; y < buffer.length; y++) {
if(array_two[z] == buffer[y]) {
contain = true;
}
}
if(array_one[a] == array_two[z] && !contain) {
buffer.push(array_two[z]);
}
if(buffer.length == array_one.length) {
return true;
}
}
}
return false;
}
this.addAction = function(pKey,script) {
keys.actions[keys.actions.length] = [pKey,script];
}
}
var keys = new wKeybindings();
keys.init();