![]() |
API 0.9.5
|
00001 /* 00002 * CPEvent.j 00003 * AppKit 00004 * 00005 * Created by Francisco Tolmasky. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 00025 CPLeftMouseDown = 1; 00026 CPLeftMouseUp = 2; 00027 CPRightMouseDown = 3; 00028 CPRightMouseUp = 4; 00029 CPMouseMoved = 5; 00030 CPLeftMouseDragged = 6; 00031 CPRightMouseDragged = 7; 00032 CPMouseEntered = 8; 00033 CPMouseExited = 9; 00034 CPKeyDown = 10; 00035 CPKeyUp = 11; 00036 CPFlagsChanged = 12; 00037 CPAppKitDefined = 13; 00038 CPSystemDefined = 14; 00039 CPApplicationDefined = 15; 00040 CPPeriodic = 16; 00041 CPCursorUpdate = 17; 00042 CPScrollWheel = 22; 00043 CPOtherMouseDown = 25; 00044 CPOtherMouseUp = 26; 00045 CPOtherMouseDragged = 27; 00046 00047 // iPhone Event Types 00048 CPTouchStart = 28; 00049 CPTouchMove = 29; 00050 CPTouchEnd = 30; 00051 CPTouchCancel = 31; 00052 00053 CPAlphaShiftKeyMask = 1 << 16; 00054 CPShiftKeyMask = 1 << 17; 00055 CPControlKeyMask = 1 << 18; 00056 CPAlternateKeyMask = 1 << 19; 00057 CPCommandKeyMask = 1 << 20; 00058 CPNumericPadKeyMask = 1 << 21; 00059 CPHelpKeyMask = 1 << 22; 00060 CPFunctionKeyMask = 1 << 23; 00061 CPDeviceIndependentModifierFlagsMask = 0xffff0000; 00062 00063 CPLeftMouseDownMask = 1 << CPLeftMouseDown; 00064 CPLeftMouseUpMask = 1 << CPLeftMouseUp; 00065 CPRightMouseDownMask = 1 << CPRightMouseDown; 00066 CPRightMouseUpMask = 1 << CPRightMouseUp; 00067 CPOtherMouseDownMask = 1 << CPOtherMouseDown; 00068 CPOtherMouseUpMask = 1 << CPOtherMouseUp; 00069 CPMouseMovedMask = 1 << CPMouseMoved; 00070 CPLeftMouseDraggedMask = 1 << CPLeftMouseDragged; 00071 CPRightMouseDraggedMask = 1 << CPRightMouseDragged; 00072 CPOtherMouseDragged = 1 << CPOtherMouseDragged; 00073 CPMouseEnteredMask = 1 << CPMouseEntered; 00074 CPMouseExitedMask = 1 << CPMouseExited; 00075 CPCursorUpdateMask = 1 << CPCursorUpdate; 00076 CPKeyDownMask = 1 << CPKeyDown; 00077 CPKeyUpMask = 1 << CPKeyUp; 00078 CPFlagsChangedMask = 1 << CPFlagsChanged; 00079 CPAppKitDefinedMask = 1 << CPAppKitDefined; 00080 CPSystemDefinedMask = 1 << CPSystemDefined; 00081 CPApplicationDefinedMask = 1 << CPApplicationDefined; 00082 CPPeriodicMask = 1 << CPPeriodic; 00083 CPScrollWheelMask = 1 << CPScrollWheel; 00084 CPAnyEventMask = 0xffffffff; 00085 00086 CPUpArrowFunctionKey = "\uF700"; 00087 CPDownArrowFunctionKey = "\uF701"; 00088 CPLeftArrowFunctionKey = "\uF702"; 00089 CPRightArrowFunctionKey = "\uF703"; 00090 CPF1FunctionKey = "\uF704"; 00091 CPF2FunctionKey = "\uF705"; 00092 CPF3FunctionKey = "\uF706"; 00093 CPF4FunctionKey = "\uF707"; 00094 CPF5FunctionKey = "\uF708"; 00095 CPF6FunctionKey = "\uF709"; 00096 CPF7FunctionKey = "\uF70A"; 00097 CPF8FunctionKey = "\uF70B"; 00098 CPF9FunctionKey = "\uF70C"; 00099 CPF10FunctionKey = "\uF70D"; 00100 CPF11FunctionKey = "\uF70E"; 00101 CPF12FunctionKey = "\uF70F"; 00102 CPF13FunctionKey = "\uF710"; 00103 CPF14FunctionKey = "\uF711"; 00104 CPF15FunctionKey = "\uF712"; 00105 CPF16FunctionKey = "\uF713"; 00106 CPF17FunctionKey = "\uF714"; 00107 CPF18FunctionKey = "\uF715"; 00108 CPF19FunctionKey = "\uF716"; 00109 CPF20FunctionKey = "\uF717"; 00110 CPF21FunctionKey = "\uF718"; 00111 CPF22FunctionKey = "\uF719"; 00112 CPF23FunctionKey = "\uF71A"; 00113 CPF24FunctionKey = "\uF71B"; 00114 CPF25FunctionKey = "\uF71C"; 00115 CPF26FunctionKey = "\uF71D"; 00116 CPF27FunctionKey = "\uF71E"; 00117 CPF28FunctionKey = "\uF71F"; 00118 CPF29FunctionKey = "\uF720"; 00119 CPF30FunctionKey = "\uF721"; 00120 CPF31FunctionKey = "\uF722"; 00121 CPF32FunctionKey = "\uF723"; 00122 CPF33FunctionKey = "\uF724"; 00123 CPF34FunctionKey = "\uF725"; 00124 CPF35FunctionKey = "\uF726"; 00125 CPInsertFunctionKey = "\uF727"; 00126 CPDeleteFunctionKey = "\uF728"; 00127 CPHomeFunctionKey = "\uF729"; 00128 CPBeginFunctionKey = "\uF72A"; 00129 CPEndFunctionKey = "\uF72B"; 00130 CPPageUpFunctionKey = "\uF72C"; 00131 CPPageDownFunctionKey = "\uF72D"; 00132 CPPrintScreenFunctionKey = "\uF72E"; 00133 CPScrollLockFunctionKey = "\uF72F"; 00134 CPPauseFunctionKey = "\uF730"; 00135 CPSysReqFunctionKey = "\uF731"; 00136 CPBreakFunctionKey = "\uF732"; 00137 CPResetFunctionKey = "\uF733"; 00138 CPStopFunctionKey = "\uF734"; 00139 CPMenuFunctionKey = "\uF735"; 00140 CPUserFunctionKey = "\uF736"; 00141 CPSystemFunctionKey = "\uF737"; 00142 CPPrintFunctionKey = "\uF738"; 00143 CPClearLineFunctionKey = "\uF739"; 00144 CPClearDisplayFunctionKey = "\uF73A"; 00145 CPInsertLineFunctionKey = "\uF73B"; 00146 CPDeleteLineFunctionKey = "\uF73C"; 00147 CPInsertCharFunctionKey = "\uF73D"; 00148 CPDeleteCharFunctionKey = "\uF73E"; 00149 CPPrevFunctionKey = "\uF73F"; 00150 CPNextFunctionKey = "\uF740"; 00151 CPSelectFunctionKey = "\uF741"; 00152 CPExecuteFunctionKey = "\uF742"; 00153 CPUndoFunctionKey = "\uF743"; 00154 CPRedoFunctionKey = "\uF744"; 00155 CPFindFunctionKey = "\uF745"; 00156 CPHelpFunctionKey = "\uF746"; 00157 CPModeSwitchFunctionKey = "\uF747"; 00158 CPEscapeFunctionKey = "\u001B"; 00159 CPSpaceFunctionKey = "\u0020"; 00160 00161 00162 CPDOMEventDoubleClick = "dblclick"; 00163 CPDOMEventMouseDown = "mousedown"; 00164 CPDOMEventMouseUp = "mouseup"; 00165 CPDOMEventMouseMoved = "mousemove"; 00166 CPDOMEventMouseDragged = "mousedrag"; 00167 CPDOMEventKeyUp = "keyup"; 00168 CPDOMEventKeyDown = "keydown"; 00169 CPDOMEventKeyPress = "keypress"; 00170 CPDOMEventCopy = "copy"; 00171 CPDOMEventPaste = "paste"; 00172 CPDOMEventScrollWheel = "mousewheel"; 00173 CPDOMEventTouchStart = "touchstart"; 00174 CPDOMEventTouchMove = "touchmove"; 00175 CPDOMEventTouchEnd = "touchend"; 00176 CPDOMEventTouchCancel = "touchcancel"; 00177 00178 var _CPEventPeriodicEventPeriod = 0, 00179 _CPEventPeriodicEventTimer = nil, 00180 _CPEventUpperCaseRegex = new RegExp("[A-Z]"); 00181 00187 @implementation CPEvent : CPObject 00188 { 00189 CPEventType _type; 00190 CPPoint _location; 00191 unsigned _modifierFlags; 00192 CPTimeInterval _timestamp; 00193 CPGraphicsContext _context; 00194 int _eventNumber; 00195 unsigned _clickCount; 00196 float _pressure; 00197 CPWindow _window; 00198 Number _windowNumber; 00199 CPString _characters; 00200 CPString _charactersIgnoringModifiers 00201 BOOL _isARepeat; 00202 unsigned _keyCode; 00203 DOMEvent _DOMEvent; 00204 00205 float _deltaX; 00206 float _deltaY; 00207 float _deltaZ; 00208 } 00209 00227 + (CPEvent)keyEventWithType:(CPEventType)anEventType location:(CGPoint)aPoint modifierFlags:(unsigned int)modifierFlags 00228 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext 00229 characters:(CPString)characters charactersIgnoringModifiers:(CPString)unmodCharacters isARepeat:(BOOL)repeatKey keyCode:(unsigned short)code 00230 { 00231 return [[self alloc] _initKeyEventWithType:anEventType location:aPoint modifierFlags:modifierFlags 00232 timestamp:aTimestamp windowNumber:aWindowNumber context:aGraphicsContext 00233 characters:characters charactersIgnoringModifiers:unmodCharacters isARepeat:repeatKey keyCode:code]; 00234 } 00235 00251 + (id)mouseEventWithType:(CPEventType)anEventType location:(CGPoint)aPoint modifierFlags:(unsigned)modifierFlags 00252 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext 00253 eventNumber:(int)anEventNumber clickCount:(int)aClickCount pressure:(float)aPressure 00254 { 00255 return [[self alloc] _initMouseEventWithType:anEventType location:aPoint modifierFlags:modifierFlags 00256 timestamp:aTimestamp windowNumber:aWindowNumber context:aGraphicsContext eventNumber:anEventNumber clickCount:aClickCount pressure:aPressure]; 00257 } 00258 00274 + (CPEvent)otherEventWithType:(CPEventType)anEventType location:(CGPoint)aLocation modifierFlags:(unsigned)modifierFlags 00275 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext 00276 subtype:(short)aSubtype data1:(int)aData1 data2:(int)aData2 00277 { 00278 return [[self alloc] _initOtherEventWithType:anEventType location:aLocation modifierFlags:modifierFlags 00279 timestamp:aTimestamp windowNumber:aWindowNumber context:aGraphicsContext subtype:aSubtype data1:aData1 data2:aData2]; 00280 } 00281 00282 - (id)_initWithType:(CPEventType)anEventType 00283 { 00284 if (self = [super init]) 00285 { 00286 _type = anEventType; 00287 00288 // Make sure these are 0 rather than nil. 00289 _deltaX = 0; 00290 _deltaY = 0; 00291 _deltaZ = 0; 00292 } 00293 00294 return self; 00295 } 00296 00297 /* @ignore */ 00298 - (id)_initMouseEventWithType:(CPEventType)anEventType location:(CPPoint)aPoint modifierFlags:(unsigned)modifierFlags 00299 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext 00300 eventNumber:(int)anEventNumber clickCount:(int)aClickCount pressure:(float)aPressure 00301 { 00302 if (self = [self _initWithType:anEventType]) 00303 { 00304 _location = CPPointCreateCopy(aPoint); 00305 _modifierFlags = modifierFlags; 00306 _timestamp = aTimestamp; 00307 _context = aGraphicsContext; 00308 _eventNumber = anEventNumber; 00309 _clickCount = aClickCount; 00310 _pressure = aPressure; 00311 _window = [CPApp windowWithWindowNumber:aWindowNumber]; 00312 } 00313 00314 return self; 00315 } 00316 00317 /* @ignore */ 00318 - (id)_initKeyEventWithType:(CPEventType)anEventType location:(CPPoint)aPoint modifierFlags:(unsigned int)modifierFlags 00319 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext 00320 characters:(CPString)characters charactersIgnoringModifiers:(CPString)unmodCharacters isARepeat:(BOOL)isARepeat keyCode:(unsigned short)code 00321 { 00322 if (self = [self _initWithType:anEventType]) 00323 { 00324 _location = CPPointCreateCopy(aPoint); 00325 _modifierFlags = modifierFlags; 00326 _timestamp = aTimestamp; 00327 _context = aGraphicsContext; 00328 _characters = characters; 00329 _charactersIgnoringModifiers = unmodCharacters; 00330 _isARepeat = isARepeat; 00331 _keyCode = code; 00332 _windowNumber = aWindowNumber; 00333 } 00334 00335 return self; 00336 } 00337 00338 /* @ignore */ 00339 - (id)_initOtherEventWithType:(CPEventType)anEventType location:(CGPoint)aPoint modifierFlags:(unsigned)modifierFlags 00340 timestamp:(CPTimeInterval)aTimestamp windowNumber:(int)aWindowNumber context:(CPGraphicsContext)aGraphicsContext 00341 subtype:(short)aSubtype data1:(int)aData1 data2:(int)aData2 00342 { 00343 if (self = [self _initWithType:anEventType]) 00344 { 00345 _location = CPPointCreateCopy(aPoint); 00346 _modifierFlags = modifierFlags; 00347 _timestamp = aTimestamp; 00348 _context = aGraphicsContext; 00349 _subtype = aSubtype; 00350 _data1 = aData1; 00351 _data2 = aData2; 00352 } 00353 00354 return self; 00355 } 00356 00365 - (CGPoint)locationInWindow 00366 { 00367 return _CGPointMakeCopy(_location); 00368 } 00369 00370 - (CGPoint)globalLocation 00371 { 00372 var theWindow = [self window], 00373 location = [self locationInWindow]; 00374 00375 if (theWindow) 00376 return [theWindow convertBaseToGlobal:location]; 00377 00378 return location; 00379 } 00380 00384 - (unsigned)modifierFlags 00385 { 00386 return _modifierFlags; 00387 } 00388 00392 - (CPTimeInterval)timestamp 00393 { 00394 return _timestamp; 00395 } 00396 00400 - (CPEventType)type 00401 { 00402 return _type; 00403 } 00404 00408 - (CPWindow)window 00409 { 00410 if (!_window) 00411 _window = [CPApp windowWithWindowNumber:_windowNumber]; 00412 00413 return _window; 00414 } 00415 00419 - (int)windowNumber 00420 { 00421 return _windowNumber; 00422 } 00423 00424 // Mouse Event Information 00428 - (int)buttonNumber 00429 { 00430 if (_type === CPRightMouseDown || _type === CPRightMouseUp || _type === CPRightMouseDragged) 00431 return 1; 00432 00433 return 0; 00434 } 00435 00439 - (int)clickCount 00440 { 00441 return _clickCount; 00442 } 00443 00449 - (CPString)characters 00450 { 00451 return _characters; 00452 } 00453 00459 - (CPString)charactersIgnoringModifiers 00460 { 00461 return _charactersIgnoringModifiers; 00462 } 00463 00469 - (BOOL)isARepeat 00470 { 00471 return _isARepeat; 00472 } 00473 00479 - (unsigned short)keyCode 00480 { 00481 return _keyCode; 00482 } 00483 00484 + (CGPoint)mouseLocation 00485 { 00486 // FIXME: this is incorrect, we shouldn't depend on the current event. 00487 var event = [CPApp currentEvent], 00488 eventWindow = [event window]; 00489 00490 if (eventWindow) 00491 return [eventWindow convertBaseToGlobal:[event locationInWindow]]; 00492 00493 return [event locationInWindow]; 00494 } 00495 00496 - (float)pressure 00497 { 00498 return _pressure; 00499 } 00500 00501 /* 00502 @ignore 00503 */ 00504 - (DOMEvent)_DOMEvent 00505 { 00506 return _DOMEvent; 00507 } 00508 00509 // Getting Scroll Wheel Event Information 00513 - (float)deltaX 00514 { 00515 return _deltaX; 00516 } 00517 00521 - (float)deltaY 00522 { 00523 return _deltaY; 00524 } 00525 00529 - (float)deltaZ 00530 { 00531 return _deltaZ; 00532 } 00533 00534 - (BOOL)_triggersKeyEquivalent:(CPString)aKeyEquivalent withModifierMask:aKeyEquivalentModifierMask 00535 { 00536 if (!aKeyEquivalent) 00537 return NO; 00538 00539 if (_CPEventUpperCaseRegex.test(aKeyEquivalent)) 00540 aKeyEquivalentModifierMask |= CPShiftKeyMask; 00541 00542 // Windows and Linux don't have command keys, so just switch it to ctrl. 00543 if (!CPBrowserIsOperatingSystem(CPMacOperatingSystem) && (aKeyEquivalentModifierMask & CPCommandKeyMask)) 00544 { 00545 aKeyEquivalentModifierMask |= CPControlKeyMask; 00546 aKeyEquivalentModifierMask &= ~CPCommandKeyMask; 00547 } 00548 00549 if ((_modifierFlags & (CPShiftKeyMask | CPAlternateKeyMask | CPCommandKeyMask | CPControlKeyMask)) !== aKeyEquivalentModifierMask) 00550 return NO; 00551 00552 // Treat \r and \n as the same key equivalent. See issue #710. 00553 if (_characters === CPNewlineCharacter || _characters === CPCarriageReturnCharacter) 00554 return CPNewlineCharacter === aKeyEquivalent || CPCarriageReturnCharacter === aKeyEquivalent; 00555 00556 return [_characters caseInsensitiveCompare:aKeyEquivalent] === CPOrderedSame; 00557 } 00558 00559 - (BOOL)_couldBeKeyEquivalent 00560 { 00561 if (_type !== CPKeyDown) 00562 return NO; 00563 00564 var characterCount = _characters.length; 00565 00566 if (!characterCount) 00567 return NO; 00568 00569 if (_modifierFlags & (CPCommandKeyMask | CPControlKeyMask)) 00570 return YES; 00571 00572 // Cocoa allows almost any key as a key equivalent unless the first responder is a 00573 // text field (presumably a subclass of NSText.) 00574 var firstResponderIsText = [[_window firstResponder] isKindOfClass:[CPTextField class]]; 00575 00576 // Some keys are accepted as key equivalents even if the first responder is a text 00577 // field. 00578 for (var i = 0; i < characterCount; i++) 00579 { 00580 var c = _characters.charAt(i); 00581 00582 if ((c >= CPUpArrowFunctionKey && c <= CPModeSwitchFunctionKey) || 00583 c === CPEnterCharacter || 00584 c === CPNewlineCharacter || 00585 c === CPCarriageReturnCharacter || 00586 c === CPEscapeFunctionKey) 00587 { 00588 return YES; 00589 } 00590 } 00591 00592 return !firstResponderIsText; 00593 } 00594 00601 + (void)startPeriodicEventsAfterDelay:(CPTimeInterval)aDelay withPeriod:(CPTimeInterval)aPeriod 00602 { 00603 _CPEventPeriodicEventPeriod = aPeriod; 00604 00605 // FIXME: OH TIMERS!!! 00606 _CPEventPeriodicEventTimer = window.setTimeout(function() { _CPEventPeriodicEventTimer = window.setInterval(_CPEventFirePeriodEvent, aPeriod * 1000.0); }, aDelay * 1000.0); 00607 } 00608 00612 + (void)stopPeriodicEvents 00613 { 00614 if (_CPEventPeriodicEventTimer === nil) 00615 return; 00616 00617 window.clearTimeout(_CPEventPeriodicEventTimer); 00618 00619 _CPEventPeriodicEventTimer = nil; 00620 } 00621 00622 @end 00623 00624 function _CPEventFirePeriodEvent() 00625 { 00626 [CPApp sendEvent:[CPEvent otherEventWithType:CPPeriodic location:_CGPointMakeZero() modifierFlags:0 timestamp:0 windowNumber:0 context:nil subtype:0 data1:0 data2:0]]; 00627 } 00628 00629 var CPEventClass = [CPEvent class]; 00630 00631 function _CPEventFromNativeMouseEvent(aNativeEvent, anEventType, aPoint, modifierFlags, aTimestamp, aWindowNumber, aGraphicsContext, anEventNumber, aClickCount, aPressure) 00632 { 00633 aNativeEvent.isa = CPEventClass; 00634 00635 aNativeEvent._type = anEventType; 00636 aNativeEvent._location = aPoint; 00637 aNativeEvent._modifierFlags = modifierFlags; 00638 aNativeEvent._timestamp = aTimestamp; 00639 aNativeEvent._windowNumber = aWindowNumber; 00640 aNativeEvent._window = nil; 00641 aNativeEvent._context = aGraphicsContext; 00642 aNativeEvent._eventNumber = anEventNumber; 00643 aNativeEvent._clickCount = aClickCount; 00644 aNativeEvent._pressure = aPressure; 00645 00646 return aNativeEvent; 00647 }