<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSEvent/keyEvent(with:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSEvent(cm)keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:"
  },
  "title" : "keyEvent(with:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:)"
}
-->

# keyEvent(with:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:)

Creates and returns a new event object that describes a key event.

```
class func keyEvent(with type: NSEvent.EventType, location: NSPoint, modifierFlags flags: NSEvent.ModifierFlags, timestamp time: TimeInterval, windowNumber wNum: Int, context unusedPassNil: NSGraphicsContext?, characters keys: String, charactersIgnoringModifiers ukeys: String, isARepeat flag: Bool, keyCode code: UInt16) -> NSEvent?
```

## Parameters

`type`

One of the following event-type constants: `NSKeyDown`, `NSKeyUp`, `NSFlagsChanged`. If anything else is specified, an `NSInternalInconsistencyException` is raised.

`location`

The cursor location in the base coordinate system of the window specified by `windowNum`.

`flags`

An integer bit field containing any of the modifier key masks described in `Getting Unicode Values`, combined using the C bitwise OR operator.

`time`

The time the event occurred in seconds since system startup.

`wNum`

An integer that identifies the window device associated with the event, which is associated with the `NSWindow` that will receive the event.

`unusedPassNil`

The display graphics context of the event. Pass `nil` for this parameter.

`keys`

A string of characters associated with the key event. Though most key events contain only one character, it is possible for a single keypress to generate a series of characters.

`ukeys`

The string of characters generated by the key event as if no modifier key had been pressed (except for Shift). This argument is useful for getting the “basic” key value in a hardware-independent manner.

`flag`

<doc://com.apple.documentation/documentation/Swift/true> if the key event is a repeat caused by the user holding the key down, <doc://com.apple.documentation/documentation/Swift/false> if the key event is new.

`code`

A number that identifies the keyboard key associated with the key event. Its value is hardware-independent.

## Return Value

The created `NSEvent` instance or `nil` if the instance could not be created.

## See Also

[`keyCode`](/documentation/AppKit/NSEvent/keyCode)

The virtual code for the key associated with the event.

[`characters`](/documentation/AppKit/NSEvent/characters)

The characters associated with a key-up or key-down event.

  <doc://com.apple.documentation/documentation/Foundation/ProcessInfo/systemUptime>

[`ARepeat`](/documentation/AppKit/NSEvent/isARepeat)

A Boolean value that indicates whether the key event is a repeat.

[`charactersIgnoringModifiers`](/documentation/AppKit/NSEvent/charactersIgnoringModifiers)

The characters generated by a key event as if no modifier key (except for Shift) applies.

[`NSEvent`](/documentation/AppKit/NSEvent)

An object that contains information about an input action, such as a mouse click or a key press.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)