CGEvent.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. {
  2. * CGEvent.h
  3. * CoreGraphics
  4. *
  5. * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
  6. *
  7. }
  8. { Pascal Translation: Peter N Lewis, <[email protected]>, August 2005 }
  9. {
  10. Modified for use with Free Pascal
  11. Version 200
  12. Please report any bugs to <[email protected]>
  13. }
  14. {$mode macpas}
  15. {$packenum 1}
  16. {$macro on}
  17. {$inline on}
  18. {$CALLING MWPASCAL}
  19. unit CGEvent;
  20. interface
  21. {$setc UNIVERSAL_INTERFACES_VERSION := $0342}
  22. {$setc GAP_INTERFACES_VERSION := $0200}
  23. {$ifc not defined USE_CFSTR_CONSTANT_MACROS}
  24. {$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
  25. {$endc}
  26. {$ifc defined CPUPOWERPC and defined CPUI386}
  27. {$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
  28. {$endc}
  29. {$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
  30. {$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
  31. {$endc}
  32. {$ifc not defined __ppc__ and defined CPUPOWERPC}
  33. {$setc __ppc__ := 1}
  34. {$elsec}
  35. {$setc __ppc__ := 0}
  36. {$endc}
  37. {$ifc not defined __i386__ and defined CPUI386}
  38. {$setc __i386__ := 1}
  39. {$elsec}
  40. {$setc __i386__ := 0}
  41. {$endc}
  42. {$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
  43. {$error Conflicting definitions for __ppc__ and __i386__}
  44. {$endc}
  45. {$ifc defined __ppc__ and __ppc__}
  46. {$setc TARGET_CPU_PPC := TRUE}
  47. {$setc TARGET_CPU_X86 := FALSE}
  48. {$elifc defined __i386__ and __i386__}
  49. {$setc TARGET_CPU_PPC := FALSE}
  50. {$setc TARGET_CPU_X86 := TRUE}
  51. {$elsec}
  52. {$error Neither __ppc__ nor __i386__ is defined.}
  53. {$endc}
  54. {$setc TARGET_CPU_PPC_64 := FALSE}
  55. {$ifc defined FPC_BIG_ENDIAN}
  56. {$setc TARGET_RT_BIG_ENDIAN := TRUE}
  57. {$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
  58. {$elifc defined FPC_LITTLE_ENDIAN}
  59. {$setc TARGET_RT_BIG_ENDIAN := FALSE}
  60. {$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
  61. {$elsec}
  62. {$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
  63. {$endc}
  64. {$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
  65. {$setc CALL_NOT_IN_CARBON := FALSE}
  66. {$setc OLDROUTINENAMES := FALSE}
  67. {$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
  68. {$setc OPAQUE_UPP_TYPES := TRUE}
  69. {$setc OTCARBONAPPLICATION := TRUE}
  70. {$setc OTKERNEL := FALSE}
  71. {$setc PM_USE_SESSION_APIS := TRUE}
  72. {$setc TARGET_API_MAC_CARBON := TRUE}
  73. {$setc TARGET_API_MAC_OS8 := FALSE}
  74. {$setc TARGET_API_MAC_OSX := TRUE}
  75. {$setc TARGET_CARBON := TRUE}
  76. {$setc TARGET_CPU_68K := FALSE}
  77. {$setc TARGET_CPU_MIPS := FALSE}
  78. {$setc TARGET_CPU_SPARC := FALSE}
  79. {$setc TARGET_OS_MAC := TRUE}
  80. {$setc TARGET_OS_UNIX := FALSE}
  81. {$setc TARGET_OS_WIN32 := FALSE}
  82. {$setc TARGET_RT_MAC_68881 := FALSE}
  83. {$setc TARGET_RT_MAC_CFM := FALSE}
  84. {$setc TARGET_RT_MAC_MACHO := TRUE}
  85. {$setc TYPED_FUNCTION_POINTERS := TRUE}
  86. {$setc TYPE_BOOL := FALSE}
  87. {$setc TYPE_EXTENDED := FALSE}
  88. {$setc TYPE_LONGLONG := TRUE}
  89. uses MacTypes,CFBase,CFData,CFMachPort,CGBase,CGDirectDisplay,CGEventTypes,CGGeometry,CGErrors,CGRemoteOperation,CGEventSource;
  90. {$ALIGN POWER}
  91. { Return the CFTypeID for CGEventRefs. }
  92. function CGEventGetTypeID: CFTypeID; external name '_CGEventGetTypeID'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  93. {
  94. * APIs provide the ability to create a NULL event, or to create specialized
  95. * events reflecting a state specified as parameters to the creation functions.
  96. }
  97. { Create a NULL event to be filled in. 'source' may be NULL. }
  98. function CGEventCreate( source: CGEventSourceRef ): CGEventRef; external name '_CGEventCreate'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  99. {
  100. * Functions to flatten and reconstruct a CGEventRef for network transport.
  101. * These may be useful in remote control and helpdesk applications.
  102. *
  103. * Returns a CGEventRef built from the flattened data representation, or NULL
  104. * if the eventData is invalid.
  105. }
  106. function CGEventCreateFromData( allocator: CFAllocatorRef; eventData: CFDataRef ): CGEventRef; external name '_CGEventCreateFromData';
  107. {
  108. * Returns a CFDataRef containing the flattened data representation of the event,
  109. * or NULL if the eventData is invalid.
  110. }
  111. function CGEventCreateData( allocator: CFAllocatorRef; event: CGEventRef ): CFDataRef; external name '_CGEventCreateData';
  112. {
  113. * Create mouse events.
  114. *
  115. * The event source may be taken from another event, or may be NULL.
  116. * mouseType should be one of the mouse event types.
  117. * mouseCursorPosition should be the global coordinates the mouse is at for the event.
  118. * For kCGEventOtherMouseDown, kCGEventOtherMouseDragged, and
  119. * kCGEventOtherMouseUp events, the mouseButton parameter should
  120. * indicate which button is changing state.
  121. *
  122. * The current implemementation of the event system supports a maximum of thirty-two buttons.
  123. * Mouse button 0 is the primary button on the mouse. Mouse button 1 is the secondary
  124. * mouse button (right). Mouse button 2 is the center button, and the remaining
  125. * buttons would be in USB device order.
  126. }
  127. function CGEventCreateMouseEvent( source: CGEventSourceRef; mouseType: CGEventType; mouseCursorPosition: CGPoint; mouseButton: CGMouseButton ): CGEventRef; external name '_CGEventCreateMouseEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  128. {
  129. * Create keyboard events.
  130. *
  131. * The event source may be taken from another event, or may be NULL.
  132. * Based on the virtual key code values entered,
  133. * the appropriate key down, key up, or flags changed events are generated.
  134. *
  135. * All keystrokes needed to generate a character must be entered, including
  136. * SHIFT, CONTROL, OPTION, and COMMAND keys. For example, to produce a 'Z',
  137. * the SHIFT key must be down, the 'z' key must go down, and then the SHIFT
  138. * and 'z' key must be released:
  139. * CGEventCreateKeyboardEvent((CGKeyCode)56, true ); // shift down
  140. * CGEventCreateKeyboardEvent( (CGKeyCode)6, true ); // 'z' down
  141. * CGEventCreateKeyboardEvent( (CGKeyCode)6, false ); // 'z' up
  142. * CGEventCreateKeyboardEvent( (CGKeyCode)56, false ); // 'shift up
  143. }
  144. function CGEventCreateKeyboardEvent( source: CGEventSourceRef; virtualKey: CGKeyCode; keyDown: CBool ): CGEventRef; external name '_CGEventCreateKeyboardEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  145. function CGEventCreateCopy( event: CGEventRef ): CGEventRef; external name '_CGEventCreateCopy'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  146. {
  147. * CFRetain() and CFRelease() may be used to retain and release CGEventRefs.
  148. }
  149. function CGEventGetSource( event: CGEventRef ): CGEventSourceRef; external name '_CGEventGetSource'; (* DEPRECATED_IN_MAC_OS_X_VERSION_10_4_AND_LATER *)
  150. {
  151. * These functions provide access to the event source for an event.
  152. * Event filters may use these to generate events that are compatible
  153. * with an event being filtered.
  154. *
  155. * Note that CGEventCreateSourceFromEvent may return NULL if the event
  156. * was generated with a private CGEventSourceStateID owned by another
  157. * process. Such events should be filtered based on the public state.
  158. }
  159. function CGEventCreateSourceFromEvent( event: CGEventRef ): CGEventSourceRef; external name '_CGEventCreateSourceFromEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  160. procedure CGEventSetSource( event: CGEventRef; source: CGEventSourceRef ); external name '_CGEventSetSource'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  161. {
  162. * The following functions will provide high level access to selected event data.
  163. }
  164. function CGEventGetType( event: CGEventRef ): CGEventType; external name '_CGEventGetType'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  165. procedure CGEventSetType( event: CGEventRef; typ: CGEventType ); external name '_CGEventSetType'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  166. function CGEventGetTimestamp( event: CGEventRef ): CGEventTimestamp; external name '_CGEventGetTimestamp'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  167. procedure CGEventSetTimestamp( event: CGEventRef; timestamp: CGEventTimestamp ); external name '_CGEventSetTimestamp'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  168. function CGEventGetLocation( event: CGEventRef ): CGPoint; external name '_CGEventGetLocation'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  169. procedure CGEventSetLocation( event: CGEventRef; location: CGPoint ); external name '_CGEventSetLocation'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  170. function CGEventGetFlags( event: CGEventRef ): CGEventFlags; external name '_CGEventGetFlags'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  171. procedure CGEventSetFlags( event: CGEventRef; flags: CGEventFlags ); external name '_CGEventSetFlags'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  172. {
  173. * Access to UniChar data in keyboard events.
  174. *
  175. * There are no runtime errors associated with these functions.
  176. * Use on non-keyboard events leaves the event unchanged.
  177. * Note that many frameworks use a fixed length representation
  178. * of a CGEvent, and so are limited to a maximum string length
  179. * of 20 Unicode characters. The CGEventRef itself is limited to 65535
  180. * characters.
  181. *
  182. * Calling CGEventKeyboardGetUnicodeString() with a NULL unicodeString
  183. * or zero maxStringLength will still return the actual count of
  184. * UniCode characters in the event data.
  185. }
  186. procedure CGEventKeyboardGetUnicodeString( event: CGEventRef; maxStringLength: UniCharCount; var actualStringLength: UniCharCount; unicodeString: {variable-size-array} UniCharPtr ); external name '_CGEventKeyboardGetUnicodeString'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  187. procedure CGEventKeyboardSetUnicodeString( event: CGEventRef; stringLength: UniCharCount; {const} unicodeString: {variable-size-array} UniCharPtr ); external name '_CGEventKeyboardSetUnicodeString'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  188. {
  189. * Additional low level functions provide access to specialized fields of the events
  190. }
  191. {
  192. * The CGEventGetDoubleValueField and CGEventSetDoubleValueField
  193. * Functions renormalize certain fixed point and integer values
  194. * to an appropriate floating point range. Keys this is done for
  195. * are:
  196. *
  197. * kCGMouseEventPressure pressure value in the range 0-255
  198. * scaled to [0.0-1.0]
  199. * kCGTabletEventPointPressure scaled pressure value; MAX=(2^16)-1, MIN=0
  200. * kCGTabletEventTiltX tilt range is -((2^15)-1) to (2^15)-1 (-32767 to 32767)
  201. * kCGTabletEventTiltY scaled to [0.0-1.0]
  202. * kCGTabletEventTangentialPressure Same range as tilt
  203. * kCGTabletEventRotation Fixed-point representation of device
  204. * rotation in a 10.6 format
  205. }
  206. function CGEventGetIntegerValueField( event: CGEventRef; field: CGEventField ): SInt64; external name '_CGEventGetIntegerValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  207. function CGEventGetDoubleValueField( event: CGEventRef; field: CGEventField ): Float64; external name '_CGEventGetDoubleValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  208. {
  209. * Before using the Set functions, the event type must be set
  210. * properly by using the appropriate type creation function or by calling
  211. * CGEventSetType().
  212. *
  213. * If this is to be a mouse event generated by a tablet, call
  214. * CGEventSetIntegerValueField(event, kCGMouseEventSubtype) with a value of
  215. * kCGEventMouseSubtypeTabletPoint or kCGEventMouseSubtypeTabletProximity
  216. * before setting other parameters.
  217. *
  218. * The correct type and subtype must be set so that the internal
  219. * event-specific data structures may be properly filled in.
  220. }
  221. procedure CGEventSetIntegerValueField( event: CGEventRef; field: CGEventField; value: SInt64 ); external name '_CGEventSetIntegerValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  222. procedure CGEventSetDoubleValueField( event: CGEventRef; field: CGEventField; value: Float64 ); external name '_CGEventSetDoubleValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  223. {
  224. * Registering an Event Tap
  225. *
  226. * A function registers an event tap, taking a pointer to the
  227. * program's tap function and an arbitrary reference to be passed
  228. * to the tap function, and returning a CFMachPortRef the program
  229. * can add to the appropriate run loop by creating a surce and
  230. * using CFRunLoopAddSource().
  231. *
  232. * Taps may be placed at the point where HIDSystem events enter
  233. * the server, at the point where HIDSystem and remote control
  234. * events enter a session, at the point where events have been
  235. * annotated to flow to a specific application, or at the point
  236. * where events are delivered to the application. Taps may be
  237. * inserted at a specified point at the head of pre-existing filters,
  238. * or appended after any pre-existing filters.
  239. *
  240. * Taps may be passive event listeners, or active filters.
  241. * An active filter may pass an event through unmodified, modify
  242. * an event, or discard an event. When a tap is registered, it
  243. * identifies the set of events to be observed with a mask, and
  244. * indicates if it is a passive or active event filter. Multiple
  245. * event type bitmasks may be ORed together.
  246. *
  247. * Taps may only be placed at kCGHIDEventTap by a process running
  248. * as the root user. NULL is returned for other users.
  249. *
  250. * Taps placed at kCGHIDEventTap, kCGSessionEventTap,
  251. * kCGAnnotatedSessionEventTap, or on a specific process may
  252. * only receive key up and down events if access for assistive
  253. * devices is enabled (Preferences Universal Access panel,
  254. * Keyboard view). If the tap is not permitted to monitor these
  255. * when the tap is being created, then the appropriate bits
  256. * in the mask are cleared. If that results in an empty mask,
  257. * then NULL is returned.
  258. *
  259. * Releasing the CFMachPortRef will release the tap.
  260. *
  261. * The CGEventTapProxy is an opaque reference to state within
  262. * the client application associated with the tap. The tap
  263. * function may pass this reference to other functions, such as
  264. * the event-posting routines.
  265. *
  266. * The event tap callback runs from the CFRunLoop to which the
  267. * tap CFMachPort is added as a source. The thread safety is defined
  268. * by the CFRunLoop and it's environment.
  269. }
  270. { Return the a CFMachPortRef for the event tap. }
  271. function CGEventTapCreate( tap: CGEventTapLocation; place: CGEventTapPlacement; options: CGEventTapOptions; eventsOfInterest: CGEventMask; callback: CGEventTapCallBack; refcon: UnivPtr ): CFMachPortRef; external name '_CGEventTapCreate'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  272. {
  273. * Events being routed to individual applications may be tapped
  274. * using another function. CGEventTapCreateForPSN() will report
  275. * all events being routed to the specified application.
  276. }
  277. { Return the a CFMachPortRef for an event tap for the specified process. }
  278. function CGEventTapCreateForPSN( processSerialNumber: { Temp type, til def moves to CoreServices } UnivPtr; place: CGEventTapPlacement; options: CGEventTapOptions; eventsOfInterest: CGEventMask; callback: CGEventTapCallBack; refcon: UnivPtr ): CFMachPortRef; external name '_CGEventTapCreateForPSN'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  279. {
  280. * Enable or disable an event tap.
  281. *
  282. * The taps are normally enabled when created.
  283. * If a tap becomes unresponsive, or a user requests taps be disabled,
  284. * then an appropriate kCGEventTapDisabled... event is passed to the
  285. * registered CGEventTapCallBack function.
  286. *
  287. * Event taps may be re-enabled by calling this function.
  288. }
  289. procedure CGEventTapEnable( myTap: CFMachPortRef; enable: CBool ); external name '_CGEventTapEnable'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  290. function CGEventTapIsEnabled( myTap: CFMachPortRef ): CBool; external name '_CGEventTapIsEnabled'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  291. {
  292. * A function to post events from a tap is provided. The events
  293. * are posted to the same points that an event returned from an
  294. * event tap would be posted to. If an event tap posts new events,
  295. * the new events enter the system before the event returned by
  296. * the tap enters the system. This order may be changed by
  297. * explicitly posting the filtered event along with new events
  298. * in the desired order, and then setting the returned event
  299. * *pEventOut to NULL.
  300. *
  301. * Events posted into the system will be seen by all taps placed
  302. * after the tap posting the event.
  303. }
  304. { Post an event from the event tap into the event stream. }
  305. procedure CGEventTapPostEvent( proxy: CGEventTapProxy; event: CGEventRef ); external name '_CGEventTapPostEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  306. {
  307. * Functions to post events into the system at various points
  308. * are also provided. Each event posted by these functions
  309. * enters the system at a point immediately before any taps
  310. * instantiated for that point, and will pass through any such taps.
  311. *
  312. * This mechanism permits an external process to establish an
  313. * event routing policy, for example, by tapping events at the
  314. * kCGAnnotatedSessionEventTap and then posting the events to
  315. * the desired PSN.
  316. }
  317. { Post an event from the event tap into the event stream. }
  318. procedure CGEventPost( tap: CGEventTapLocation; event: CGEventRef ); external name '_CGEventPost'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  319. { Post an event from the event tap into the event stream for a specific application. }
  320. procedure CGEventPostToPSN( processSerialNumber: { Temp type, til def moves to CoreServices } UnivPtr; event: CGEventRef ); external name '_CGEventPostToPSN'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  321. {
  322. * Mechanism used to list event taps.
  323. * An array length (maxNumberOfTaps) and array of CGEventTapInformation structures
  324. * are passed in.
  325. * Up to maxNumberOfTaps elements of the array are filled in with event tap information.
  326. * The actual number of elements filled in is returned in eventTapCount.
  327. *
  328. * The call resets the accumulated minUsecLatency and maxUsecLatency fields. Values
  329. * reported in these fields reflect the min and max values seen since the preceding call,
  330. * or the instantiation of the tap.
  331. *
  332. * If the CGEventTapInformation array is NULL, maxNumberOfTaps is ignored, and *eventTapCount
  333. * is filled in with the number of event taps that are currently installed.
  334. }
  335. function CGGetEventTapList( maxNumberOfTaps: CGTableCount; tapList: {variable-size-array} CGEventTapInformationPtr; var eventTapCount: CGTableCount ): CGError; external name '_CGGetEventTapList'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
  336. end.