---
title: "init(keyboardEventSource:virtualKey:keyDown:)"
framework: coregraphics
role: symbol
role_heading: Initializer
path: "coregraphics/cgevent/init(keyboardeventsource:virtualkey:keydown:)"
---

# init(keyboardEventSource:virtualKey:keyDown:)

Returns a new Quartz keyboard event.

## Declaration

```swift
init?(keyboardEventSource source: CGEventSource?, virtualKey: CGKeyCode, keyDown: Bool)
```

## Parameters

- `source`: An event source taken from another event, or NULL.
- `virtualKey`: The virtual key code for the event.
- `keyDown`: Pass true to specify that the key position is down. To specify that the key position is up, pass false. This value is used to determine the type of the keyboard event—see doc://com.apple.coregraphics/documentation/CoreGraphics/CGEventType.

## Return Value

Return Value A new keyboard event, or NULL if the event could not be created. When you no longer need the event, you should release it using the function CFRelease.

## Discussion

Discussion All keystrokes needed to generate a character must be entered, including modifier keys.  For example, to produce a ‘Z’, the SHIFT key must be down, the ‘z’ key must go down, and then the SHIFT and ‘z’ key must be released: CGEventRef event1, event2, event3, event4; event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true); event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, true); event3 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, false); event4 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);

## See Also

### Functions

- [CGAcquireDisplayFadeReservation(_:_:)](coregraphics/cgacquiredisplayfadereservation(_:_:).md)
- [CGAssociateMouseAndMouseCursorPosition(_:)](coregraphics/cgassociatemouseandmousecursorposition(_:).md)
- [CGBeginDisplayConfiguration(_:)](coregraphics/cgbegindisplayconfiguration(_:).md)
- [CGCancelDisplayConfiguration(_:)](coregraphics/cgcanceldisplayconfiguration(_:).md)
- [CGCaptureAllDisplays()](coregraphics/cgcapturealldisplays().md)
- [CGCaptureAllDisplaysWithOptions(_:)](coregraphics/cgcapturealldisplayswithoptions(_:).md)
- [CGCompleteDisplayConfiguration(_:_:)](coregraphics/cgcompletedisplayconfiguration(_:_:).md)
- [CGConfigureDisplayFadeEffect(_:_:_:_:_:_:)](coregraphics/cgconfiguredisplayfadeeffect(_:_:_:_:_:_:).md)
- [CGConfigureDisplayMirrorOfDisplay(_:_:_:)](coregraphics/cgconfiguredisplaymirrorofdisplay(_:_:_:).md)
- [CGConfigureDisplayMode(_:_:_:)](coregraphics/cgconfiguredisplaymode(_:_:_:).md)
- [CGConfigureDisplayOrigin(_:_:_:_:)](coregraphics/cgconfiguredisplayorigin(_:_:_:_:).md)
- [CGConfigureDisplayStereoOperation(_:_:_:_:)](coregraphics/cgconfiguredisplaystereooperation(_:_:_:_:).md)
- [CGConfigureDisplayWithDisplayMode(_:_:_:_:)](coregraphics/cgconfiguredisplaywithdisplaymode(_:_:_:_:).md)
- [CGCursorIsDrawnInFramebuffer()](coregraphics/cgcursorisdrawninframebuffer().md)
- [CGCursorIsVisible()](coregraphics/cgcursorisvisible().md)
