Another Atlas keyboard behavior?
I wrote this keyboard behavior a while ago and I just realized that Wilco published something very similar long before me. Oh well, just use the one you like best...
Here's how you use the behavior:
<textBox
id="tb">
<behaviors>
<keyPressBehavior
key="Esc"
keyPress="onEscKeyPress"/>
<keyPressBehavior
key="b"
shiftKey="true"
keyPress="onShiftBKeyPress"/>
</behaviors>
</textBox>
You can also get all keypresses if you don't specify the key.
Here's the source code for it:
Web.UI.Key = {
Backspace: 8,
Tab:
9,
Return: 13,
Esc: 27,
PageUp: 33,
PageDown: 34,
End: 35,
Home: 36,
Left: 37,
Up: 38,
Right: 39,
Down: 40,
F1: 112,
F2: 113,
F3: 114,
F4: 115,
F5: 116,
F6: 117,
F7: 118,
F8: 119,
F9: 120,
F10: 121,
F11: 122,
F12: 123,
Delete: 127
};
// del is 46 on Windows
Web.UI.Key.parse =
function(s) {
if (s.length
== 1) {
return s;
}
var result =
parseInt(s);
if (!isNaN(result)) {
return result;
}
for (var
f in
this) {
if ((f == s) &&
(typeof(this[f]) == 'number')) {
return
this[f];
}
}
throw 'Invalid Key
Value';
}
Web.UI.Key.toString
= function(value) {
if (typeof(value) == 'string') {
return value;
}
for (var
v in
this) {
if (this[v] == value) {
return v;
}
}
return value.toString();
}
Web.UI.KeyPressEventArgs =
function(key, shiftKey,
controlKey, altKey) {
Web.UI.KeyPressEventArgs.initializeBase(this, []);
var _key = key;
var _shiftKey =
shiftKey;
var _controlKey =
controlKey;
var _altKey =
altKey;
this.get_altKey =
function() {
return
_altKey;
}
this.get_controlKey =
function() {
return
_controlKey;
}
this.get_key =
function() {
return _key;
}
this.get_shiftKey =
function() {
return
_shiftKey;
}
this.getDescriptor =
function() {
var td =
Web.UI.KeyPressEventArgs.callBaseMethod(this, 'getDescriptor');
td.addProperty('altKey',
Boolean,
true);
td.addProperty('controlKey',
Boolean,
true);
td.addProperty('key', Web.UI.Key,
true);
td.addProperty('shiftKey',
Boolean,
true);
return td;
}
Web.UI.KeyPressEventArgs.registerBaseMethod(this, 'getDescriptor');
}
Type.registerClass('Web.UI.KeyPressEventArgs',
Web.CancelEventArgs);
Web.UI.KeyPressBehavior
= function() {
Web.UI.KeyPressBehavior.initializeBase(this);
var
_keyHandler;
var _key;
var _shiftKey;
var _controlKey =
false;
var _altKey =
false;
this.get_altKey =
function() {
return
_altKey;
}
this.set_altKey =
function(value) {
if (_altKey != value)
{
_altKey = value;
this.raisePropertyChanged('altKey');
}
}
this.get_controlKey =
function() {
return
_controlKey;
}
this.set_controlKey =
function(value) {
if (_controlKey !=
value) {
_controlKey = value;
this.raisePropertyChanged('controlKey');
}
}
this.get_key =
function() {
return _key;
}
this.set_key
= function(value) {
if (_key != value) {
if (this.get_isInitialized()) {
disposeKeyHandler.call(this)();
_keyHandler =
Function.createDelegate(this,
keyHandler);
this.control.element.attachEvent((typeof(value) == 'number') ? 'onkeyup' : 'onkeypress',
_keyHandler);
}
_key =
value;
this.raisePropertyChanged('key');
}
}
this.get_shiftKey =
function() {
return
_shiftKey;
}
this.set_shiftKey =
function(value) {
if (_shiftKey != value)
{
_shiftKey = value;
this.raisePropertyChanged('shiftKey');
}
}
this.getDescriptor =
function() {
var td =
new
Web.TypeDescriptor();
td.addProperty('key', Web.UI.Key);
td.addProperty('shiftKey',
Boolean);
td.addProperty('controlKey',
Boolean);
td.addProperty('altKey',
Boolean);
td.addEvent('keyPress',
true);
return td;
}
this.keyPress =
this.createEvent();
this.dispose =
function() {
disposeKeyHandler.call(this);
Web.UI.KeyPressBehavior.callBaseMethod(this, 'dispose');
}
function
disposeKeyHandler() {
if (_keyHandler) {
this.control.element.detachEvent((typeof(_key) == 'number') ? 'onkeyup' : 'onkeypress',
_keyHandler);
_keyHandler =
null;
}
}
this.initialize =
function() {
Web.UI.KeyPressBehavior.callBaseMethod(this, 'initialize');
_keyHandler =
Function.createDelegate(this,
keyHandler);
this.control.element.attachEvent((typeof(_key) == 'number') ? 'onkeyup' : 'onkeypress',
_keyHandler);
}
function keyHandler()
{
var evt
= event;
var keyCode =
evt.charCode ? evt.charCode : evt.keyCode;
// Special-case delete as the Windows delete key has
a non-standard keyCode.
// See
http://www.w3.org/TR/1999/WD-DOM-Level-2-19990923/events.html#Events-KeyEvent
for a list of standard key codes.
// See http://www.w3.org/2002/09/tests/keys.html for
a keyCode testing tool.
// Cancelling for Delete doesn't work in IE.
if ((keyCode == 46)
&& (_key == Web.UI.Key.Delete)) {
keyCode = Web.UI.Key.Delete;
}
if ((evt.altKey ==
_altKey) &&
(evt.ctrlKey ==
_controlKey) &&
((_shiftKey ==
null) || (typeof(_shiftKey) == 'undefined') || (evt.shiftKey ==
_shiftKey)) &&
(((typeof(_key) == 'number') && (keyCode == _key)) ||
((typeof(_key) ==
'string') && (String.fromCharCode(keyCode).toLowerCase() ==
_key.toLowerCase())) ||
(_key ==
null) || (typeof(_key) == 'undefined'))) {
var args =
new
Web.UI.KeyPressEventArgs(_key ? _key :
String.fromCharCode(keyCode), evt.shiftKey, evt.ctrlKey,
evt.altKey);
this.keyPress.invoke(this,
args);
if
(args.get_canceled()) {
event.returnValue =
false;
event.cancelBubble =
true;
}
}
}
}
Type.registerSealedClass('Web.UI.KeyPressBehavior',
Web.UI.Behavior);
Web.TypeDescriptor.addType('script',
'keyPressBehavior', Web.UI.KeyPressBehavior);