---
title: "init(cgEvent:)"
framework: appkit
role: symbol
role_heading: Initializer
path: "appkit/nsevent/init(cgevent:)-4igjn"
---

# init(cgEvent:)

Creates and returns an event object for a Core Graphics event.

## Declaration

```swift
init?(cgEvent: CGEvent)
```

## Parameters

- `cgEvent`: A doc://com.apple.documentation/documentation/CoreGraphics/CGEvent opaque type that represents an event.

## Return Value

Return Value An autoreleased NSEvent object that is equivalent to cgEvent.

## Discussion

Discussion The returned object retains the CGEventRef object (cgEvent) until it (the Objective-C object) is freed—it then releases the CGEventRef object.  If no Cocoa event corresponds to the CGEventRef object, this method returns nil.

## See Also

### Related Documentation

- [cgEvent](appkit/nsevent/cgevent.md)

### Creating an event object

- [keyEvent(with:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:)](appkit/nsevent/keyevent(with:location:modifierflags:timestamp:windownumber:context:characters:charactersignoringmodifiers:isarepeat:keycode:).md)
- [mouseEvent(with:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure:)](appkit/nsevent/mouseevent(with:location:modifierflags:timestamp:windownumber:context:eventnumber:clickcount:pressure:).md)
- [enterExitEvent(with:location:modifierFlags:timestamp:windowNumber:context:eventNumber:trackingNumber:userData:)](appkit/nsevent/enterexitevent(with:location:modifierflags:timestamp:windownumber:context:eventnumber:trackingnumber:userdata:).md)
- [otherEvent(with:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:)](appkit/nsevent/otherevent(with:location:modifierflags:timestamp:windownumber:context:subtype:data1:data2:).md)
- [init(eventRef:)](appkit/nsevent/init(eventref:).md)
