00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024
00025 #include "CoreGraphics/CGGeometry.h"
00026
00027
00028 CPLeftMouseDown = 1;
00029 CPLeftMouseUp = 2;
00030 CPRightMouseDown = 3;
00031 CPRightMouseUp = 4;
00032 CPMouseMoved = 5;
00033 CPLeftMouseDragged = 6;
00034 CPRightMouseDragged = 7;
00035 CPMouseEntered = 8;
00036 CPMouseExited = 9;
00037 CPKeyDown = 10;
00038 CPKeyUp = 11;
00039 CPFlagsChanged = 12;
00040 CPAppKitDefined = 13;
00041 CPSystemDefined = 14;
00042 CPApplicationDefined = 15;
00043 CPPeriodic = 16;
00044 CPCursorUpdate = 17;
00045 CPScrollWheel = 22;
00046 CPOtherMouseDown = 25;
00047 CPOtherMouseUp = 26;
00048 CPOtherMouseDragged = 27;
00049
00050
00051 CPTouchStart = 28;
00052 CPTouchMove = 29;
00053 CPTouchEnd = 30;
00054 CPTouchCancel = 31;
00055
00056
00057 CPAlphaShiftKeyMask = 1 << 16;
00058 CPShiftKeyMask = 1 << 17;
00059 CPControlKeyMask = 1 << 18;
00060 CPAlternateKeyMask = 1 << 19;
00061 CPCommandKeyMask = 1 << 20;
00062 CPNumericPadKeyMask = 1 << 21;
00063 CPHelpKeyMask = 1 << 22;
00064 CPFunctionKeyMask = 1 << 23;
00065 CPDeviceIndependentModifierFlagsMask = 0xffff0000;
00066
00067 CPLeftMouseDownMask = 1 << CPLeftMouseDown;
00068 CPLeftMouseUpMask = 1 << CPLeftMouseUp;
00069 CPRightMouseDownMask = 1 << CPRightMouseDown;
00070 CPRightMouseUpMask = 1 << CPRightMouseUp;
00071 CPOtherMouseDownMask = 1 << CPOtherMouseDown;
00072 CPOtherMouseUpMask = 1 << CPOtherMouseUp;
00073 CPMouseMovedMask = 1 << CPMouseMoved;
00074 CPLeftMouseDraggedMask = 1 << CPLeftMouseDragged;
00075 CPRightMouseDraggedMask = 1 << CPRightMouseDragged;
00076 CPOtherMouseDragged = 1 << CPOtherMouseDragged;
00077 CPMouseEnteredMask = 1 << CPMouseEntered;
00078 CPMouseExitedMask = 1 << CPMouseExited;
00079 CPCursorUpdateMask = 1 << CPCursorUpdate;
00080 CPKeyDownMask = 1 << CPKeyDown;
00081 CPKeyUpMask = 1 << CPKeyUp;
00082 CPFlagsChangedMask = 1 << CPFlagsChanged;
00083 CPAppKitDefinedMask = 1 << CPAppKitDefined;
00084 CPSystemDefinedMask = 1 << CPSystemDefined;
00085 CPApplicationDefinedMask = 1 << CPApplicationDefined;
00086 CPPeriodicMask = 1 << CPPeriodic;
00087 CPScrollWheelMask = 1 << CPScrollWheel;
00088 CPAnyEventMask = 0xffffffff;
00089
00090 CPDOMEventDoubleClick = "dblclick",
00091 CPDOMEventMouseDown = "mousedown",
00092 CPDOMEventMouseUp = "mouseup",
00093 CPDOMEventMouseMoved = "mousemove",
00094 CPDOMEventMouseDragged = "mousedrag",
00095 CPDOMEventKeyUp = "keyup",
00096 CPDOMEventKeyDown = "keydown",
00097 CPDOMEventKeyPress = "keypress";
00098 CPDOMEventCopy = "copy";
00099 CPDOMEventPaste = "paste";
00100 CPDOMEventScrollWheel = "mousewheel";
00101 CPDOMEventTouchStart = "touchstart";
00102 CPDOMEventTouchMove = "touchmove";
00103 CPDOMEventTouchEnd = "touchend";
00104 CPDOMEventTouchCancel = "touchcancel";
00105
00106 var _CPEventPeriodicEventPeriod = 0,
00107 _CPEventPeriodicEventTimer = nil;
00108
00114 @implementation CPEvent : CPObject
00115 {
00116 CPEventType _type;
00117 CPPoint _location;
00118 unsigned _modifierFlags;
00119 CPTimeInterval _timestamp;
00120 CPGraphicsContext _context;
00121 int _eventNumber;
00122 unsigned _clickCount;
00123 float _pressure;
00124 CPWindow _window;
00125 Number _windowNumber;
00126 CPString _characters;
00127 CPString _charactersIgnoringModifiers
00128 BOOL _isARepeat;
00129 unsigned _keyCode;
00130 DOMEvent _DOMEvent;
00131
00132 float _deltaX;
00133 float _deltaY;
00134 float _deltaZ;
00135 }
00136
00153 + (CPEvent)keyEventWithType:(CPEventType)anEventType location:(CGPoint)aPoint modifierFlags:(unsigned int)modifierFlags
00154 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext
00155 characters:(CPString)characters charactersIgnoringModifiers:(CPString)unmodCharacters isARepeat:(BOOL)repeatKey keyCode:(unsigned short)code
00156 {
00157 return [[self alloc] _initKeyEventWithType:anEventType location:aPoint modifierFlags:modifierFlags
00158 timestamp:aTimestamp windowNumber:aWindowNumber context:aGraphicsContext
00159 characters:characters charactersIgnoringModifiers:unmodCharacters isARepeat:repeatKey keyCode:code];
00160 }
00161
00176 + (id)mouseEventWithType:(CPEventType)anEventType location:(CGPoint)aPoint modifierFlags:(unsigned)modifierFlags
00177 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext
00178 eventNumber:(int)anEventNumber clickCount:(int)aClickCount pressure:(float)aPressure
00179 {
00180 return [[self alloc] _initMouseEventWithType:anEventType location:aPoint modifierFlags:modifierFlags
00181 timestamp:aTimestamp windowNumber:aWindowNumber context:aGraphicsContext eventNumber:anEventNumber clickCount:aClickCount pressure:aPressure];
00182 }
00183
00198 + (CPEvent)otherEventWithType:(CPEventType)anEventType location:(CGPoint)aLocation modifierFlags:(unsigned)modifierFlags
00199 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext
00200 subtype:(short)aSubtype data1:(int)aData1 data2:(int)aData2
00201 {
00202 return [[self alloc] _initOtherEventWithType:anEventType location:aLocation modifierFlags:modifierFlags
00203 timestamp:aTimestamp windowNumber:aWindowNumber context:aGraphicsContext subtype:aSubtype data1:aData1 data2:aData2];
00204 }
00205
00206
00207 - (id)_initMouseEventWithType:(CPEventType)anEventType location:(CPPoint)aPoint modifierFlags:(unsigned)modifierFlags
00208 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext
00209 eventNumber:(int)anEventNumber clickCount:(int)aClickCount pressure:(float)aPressure
00210 {
00211 self = [super init];
00212
00213 if (self)
00214 {
00215 _type = anEventType;
00216 _location = CPPointCreateCopy(aPoint);
00217 _modifierFlags = modifierFlags;
00218 _timestamp = aTimestamp;
00219 _context = aGraphicsContext;
00220 _eventNumber = anEventNumber;
00221 _clickCount = aClickCount;
00222 _pressure = aPressure;
00223 _window = [CPApp windowWithWindowNumber:aWindowNumber];
00224 }
00225
00226 return self;
00227 }
00228
00229
00230 - (id)_initKeyEventWithType:(CPEventType)anEventType location:(CPPoint)aPoint modifierFlags:(unsigned int)modifierFlags
00231 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext
00232 characters:(CPString)characters charactersIgnoringModifiers:(CPString)unmodCharacters isARepeat:(BOOL)isARepeat keyCode:(unsigned short)code
00233 {
00234 self = [super init];
00235
00236 if (self)
00237 {
00238 _type = anEventType;
00239 _location = CPPointCreateCopy(aPoint);
00240 _modifierFlags = modifierFlags;
00241 _timestamp = aTimestamp;
00242 _context = aGraphicsContext;
00243 _characters = characters;
00244 _charactersIgnoringModifiers = unmodCharacters;
00245 _isARepeat = isARepeat;
00246 _keyCode = code;
00247 _windowNumber = aWindowNumber;
00248 }
00249
00250 return self;
00251 }
00252
00253
00254 - (id)_initOtherEventWithType:(CPEventType)anEventType location:(CGPoint)aPoint modifierFlags:(unsigned)modifierFlags
00255 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext
00256 subtype:(short)aSubtype data1:(int)aData1 data2:(int)aData2
00257 {
00258 self = [super init];
00259
00260 if (self)
00261 {
00262 _type = anEventType;
00263 _location = CPPointCreateCopy(aPoint);
00264 _modifierFlags = modifierFlags;
00265 _timestamp = aTimestamp;
00266 _context = aGraphicsContext;
00267 _subtype = aSubtype;
00268 _data1 = aData1;
00269 _data2 = aData2;
00270 }
00271
00272 return self;
00273 }
00274
00283 - (CGPoint)locationInWindow
00284 {
00285 return _CGPointMakeCopy(_location);
00286 }
00287
00288 - (CGPoint)globalLocation
00289 {
00290 var theWindow = [self window],
00291 location = [self locationInWindow];
00292
00293 if (theWindow)
00294 return [theWindow convertBaseToGlobal:location];
00295
00296 return location;
00297 }
00298
00302 - (unsigned)modifierFlags
00303 {
00304 return _modifierFlags;
00305 }
00306
00310 - (CPTimeInterval)timestamp
00311 {
00312 return _timestamp;
00313 }
00314
00318 - (CPEventType)type
00319 {
00320 return _type;
00321 }
00322
00326 - (CPWindow)window
00327 {
00328 if (!_window)
00329 _window = [CPApp windowWithWindowNumber:_windowNumber];
00330
00331 return _window;
00332 }
00333
00337 - (int)windowNumber
00338 {
00339 return _windowNumber;
00340 }
00341
00342
00346 - (int)buttonNumber
00347 {
00348 return _buttonNumber;
00349 }
00350
00354 - (int)clickCount
00355 {
00356 return _clickCount;
00357 }
00358
00363 - (CPString)characters
00364 {
00365 return _characters;
00366 }
00367
00372 - (CPString)charactersIgnoringModifiers
00373 {
00374 return _charactersIgnoringModifiers;
00375 }
00376
00381 - (BOOL)isARepeat
00382 {
00383 return _isARepeat;
00384 }
00385
00390 - (unsigned short)keyCode
00391 {
00392 return _keyCode;
00393 }
00394
00395 + (CGPoint)mouseLocation
00396 {
00397
00398 var event = [CPApp currentEvent],
00399 eventWindow = [event window];
00400
00401 if (eventWindow)
00402 return [eventWindow convertBaseToGlobal:[event locationInWindow]];
00403
00404 return [event locationInWindow];
00405 }
00406
00407 - (float)pressure
00408 {
00409 return _pressure;
00410 }
00411
00412
00413
00414
00415 - (DOMEvent)_DOMEvent
00416 {
00417 return _DOMEvent;
00418 }
00419
00420
00424 - (float)deltaX
00425 {
00426 return _deltaX;
00427 }
00428
00432 - (float)deltaY
00433 {
00434 return _deltaY;
00435 }
00436
00440 - (float)deltaZ
00441 {
00442 return _deltaZ;
00443 }
00444
00445 - (BOOL)_couldBeKeyEquivalent
00446 {
00447
00448 return _type === CPKeyDown &&
00449 _modifierFlags & (CPCommandKeyMask | CPControlKeyMask) &&
00450 [_characters length] > 0;
00451 }
00452
00458 + (void)startPeriodicEventsAfterDelay:(CPTimeInterval)aDelay withPeriod:(CPTimeInterval)aPeriod
00459 {
00460 _CPEventPeriodicEventPeriod = aPeriod;
00461
00462
00463 _CPEventPeriodicEventTimer = window.setTimeout(function() { _CPEventPeriodicEventTimer = window.setInterval(_CPEventFirePeriodEvent, aPeriod * 1000.0); }, aDelay * 1000.0);
00464 }
00465
00469 + (void)stopPeriodicEvents
00470 {
00471 if (_CPEventPeriodicEventTimer === nil)
00472 return;
00473
00474 window.clearTimeout(_CPEventPeriodicEventTimer);
00475
00476 _CPEventPeriodicEventTimer = nil;
00477 }
00478
00479 @end
00480
00481 function _CPEventFirePeriodEvent()
00482 {
00483 [CPApp sendEvent:[CPEvent otherEventWithType:CPPeriodic location:_CGPointMakeZero() modifierFlags:0 timestamp:0 windowNumber:0 context:nil subtype:0 data1:0 data2:0]];
00484 }
00485
00486 var CPEventClass = [CPEvent class];
00487
00488 function _CPEventFromNativeMouseEvent(aNativeEvent, anEventType, aPoint, modifierFlags, aTimestamp, aWindowNumber, aGraphicsContext, anEventNumber, aClickCount, aPressure)
00489 {
00490 aNativeEvent.isa = CPEventClass;
00491
00492 aNativeEvent._type = anEventType;
00493 aNativeEvent._location = aPoint;
00494 aNativeEvent._modifierFlags = modifierFlags;
00495 aNativeEvent._timestamp = aTimestamp;
00496 aNativeEvent._windowNumber = aWindowNumber;
00497 aNativeEvent._window = nil;
00498 aNativeEvent._context = aGraphicsContext;
00499 aNativeEvent._eventNumber = anEventNumber;
00500 aNativeEvent._clickCount = aClickCount;
00501 aNativeEvent._pressure = aPressure;
00502
00503 return aNativeEvent;
00504 }
00505