Регистрация: Jul 2002
Адрес: Россия
Сообщений: 16
|
вот один маленький универсальный класс заимствованный у Потапенко. Ничо лучше все равно не придумать:
_global.C_ShortcutsCLASS = function()
{
this._listeners = [];
this._shortCutListeners = [];
this.keys = {};
this.init();
}
//-------------------------------------------------------------------------
// create prototype
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype = new Object();
//-------------------------------------------------------------------------
// class default attributes
//-------------------------------------------------------------------------
//*************************************************************************
//-------------------------------------------------------------------------
// class methods
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.init = function()
{
Key.addShortcut = function(listener/*, "CTRL+SHIFT+S, "CTRL+SHIFT+D"*/)
{
this.shortcutsEngine.addListener.apply(this.shortcutsEngine, arguments);
}
Key.removeShortcut = function(listener)
{
this.shortcutsEngine.removeListener(listener);
}
Key.addShortcutListener = function(listener/*, "CTRL+SHIFT+S, "CTRL+SHIFT+D"*/)
{
this.shortcutsEngine.addShortcutListener.apply(this.shortcutsEngine, arguments);
}
Key.removeShortcutListener = function(listener)
{
this.shortcutsEngine.removeShortcutListener(listener);
}
Key.getShortcut = function()
{
return this.shortcutsEngine.getShortcut();
}
Key.addListener(this);
ASSetPropFlags(Key, "addShortcut,removeShortcut", 131);
ASSetPropFlags(Key, "addShortcutListener,removeShortcutListener,getShortcut", 131);
var o = this.nonFunctionalKeys = {};
o[Key.UP] = "UP";
o[Key.DOWN] = "DOWN";
o[Key.LEFT] = "LEFT";
o[Key.RIGHT] = "RIGHT";
o[Key.DELETEKEY] = "DELETE";
o[Key.HOME] = "HOME";
o[Key.END] = "END";
o[Key.PGDN] = "PAGEDOWN";
o[Key.PGUP] = "PAGEUP";
o[Key.INSERT] = "INSERT";
o[Key.SPACE] = "SPACE";
o[Key.BACKSPACE] = "BACKSPACE";
o[Key.ESCAPE] = "INSERT";
o[Key.TAB] = "TAB";
o[Key.ENTER] = "ENTER";
o = this.functionalKeys = {};
o[Key.CONTROL] = "CTRL";
o[Key.SHIFT] = "SHIFT";
o[Key.ALT] = "ALT";
}
//-------------------------------------------------------------------------
// getShortcut
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.getShortcut = function()
{
var a = [];
for(var v in this.keys)
{
a.push(v);
}
return this.format(a);
}
//*************************************************************************
//-------------------------------------------------------------------------
// onKeyDown
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.onKeyDown = function()
{
var isSh = this.registerKey(true);
if(isSh && this.isListeners)
{
var a;
var o;
var shName = this.getShortcut();
a = this._listeners;
for (var i=0;i<a.length;i++)
{
o = a[i];
if(o.shortcutName == shName && o.client.enabled != false)
{
o.client.onShortcut(shName);
}
}
}
this.onShortcutChanged();
}
//-------------------------------------------------------------------------
// onKeyUp
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.onKeyUp = function()
{
this.registerKey(false);
this.onShortcutChanged();
}
//-------------------------------------------------------------------------
// onShortcutChanged
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.onShortcutChanged = function()
{
if(this.isShortcutListeners)
{
var shName = this.getShortcut();
var a = this._shortCutListeners;
var o;
for (var i=0;i<a.length;i++)
{
o = a[i];
if(o.shortcutName == shName)
{
if(!o.calledDown)
{
o.calledDown = true;
o.client.onShortcutDown(shName);
}
}else if(o.calledDown){
o.calledDown = false;
o.client.onShortcutUp(o.shortcutName);
}
}
}
}
//*************************************************************************
//-------------------------------------------------------------------------
// addListener
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.addListener = function(listener)
{
if(listener == undefined)return;
this.removeShortcut(listener);
var o
var a = arguments.slice(1);
for (var i=0;i<a.length;i++)
{
o = {};
o.client = listener;
o.shortcutName = this.format(a[i].toUpperCase().split("+"));
this._listeners.push(o);
}
this.isListeners = (this._listeners.length > 0);
}
//-------------------------------------------------------------------------
// removeListener
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.removeListener = function(listener)
{
if(listener == undefined)return;
var a = this._listeners;
for (var i=0;i<a.length;i++)
{
if(a[i].client == listener || a[i].client == undefined)
{
a.splice(i, 1);
if(i > 0)i--;
}
}
this.isListeners = (this._listeners.length > 0);
}
//-------------------------------------------------------------------------
// addShortcutListener
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.addShortcutListener = function(listener)
{
if(listener == undefined)return;
this.removeShortcutListener(listener);
var o
var a = arguments.slice(1);
for (var i=0;i<a.length;i++)
{
o = {};
o.client = listener;
o.shortcutName = this.format(a[i].toUpperCase().split("+"));
this._shortCutListeners.push(o);
}
this.isShortcutListeners = (this._shortCutListeners.length > 0);
}
//-------------------------------------------------------------------------
// removeShortcutListener
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.removeShortcutListener = function(listener)
{
if(listener == undefined)return;
var a = this._shortCutListeners;
for (var i=0;i<a.length;i++)
{
if(a[i].client == listener || a[i].client == undefined)
{
a.splice(i, 1);
if(i > 0)i--;
}
}
this.isShortcutListeners = (this._shortCutListeners.length > 0);
}
//*************************************************************************
//-------------------------------------------------------------------------
// format
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.format = function(a)
{
var b = [];
for (var i=0;i<a.length;i++)
{
if(a[i] == "ALT")
{
b.push(a.splice(i, 1));
break;
}
}
for (var i=0;i<a.length;i++)
{
if(a[i] == "CTRL")
{
b.push(a.splice(i, 1));
break;
}
}
for (var i=0;i<a.length;i++)
{
if(a[i] == "SHIFT")
{
b.push(a.splice(i, 1));
break;
}
}
return (b.concat(a.sort())).join("+");
}
//-------------------------------------------------------------------------
// register key
//-------------------------------------------------------------------------
C_ShortcutsCLASS.prototype.registerKey = function(down)
{
for(var v in this.keys)
{
if(!Key.isDown(this.keys[v]))
{
delete this.keys[v];
}
}
if(!down)
{
return;
}
var c = Key.getCode();
var k = this.functionalKeys[c];
if(k == undefined)
{
k = this.nonFunctionalKeys[c];
if(k == undefined)
{
k = String.fromCharCode(Key.getAscii()).toUpperCase();
}
if(this.keys[k])
{
return false;
}
this.keys[k] = c;
return true;
}else{
this.keys[k] = c;
return false;
}
}
//-------------------------------------------------------------------------
// register class
//-------------------------------------------------------------------------
Key.shortcutsEngine = new C_ShortcutsCLASS();
ASSetPropFlags(Key, "shortcutsEngine", 131);
ASSetPropFlags(_global, "C_ShortcutsCLASS", 131);
/* вот примеры как юзать
o = {};
o.onShortcutDown = function(sh) {
trace(sh);
_root.test = sh;
};
o.onShortcutUp = function() {
_root.test = "";
};
o.onKeyDown = function () {
trace(Key.getShortcut())
}
Key.addListener(o)
Key.addShortcutListener(o, "ctrl+shift+l", "ctrl+shift+d", "alt+ctrl+l");
*/
__________________
думать надо...
|