<!--
{
  "availability" : [
    "macCatalyst: -",
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreGraphics",
  "identifier" : "/documentation/CoreGraphics/CGKeyCode",
  "metadataVersion" : "0.1.0",
  "role" : "Type Alias",
  "symbol" : {
    "kind" : "Type Alias",
    "modules" : [
      "Core Graphics"
    ],
    "preciseIdentifier" : "c:@T@CGKeyCode"
  },
  "title" : "CGKeyCode"
}
-->

# CGKeyCode

Represents the virtual key codes used in keyboard events.

```
typealias CGKeyCode = UInt16
```

## Discussion

In macOS, the hardware scan codes generated by keyboards are mapped to a set of virtual key codes that are hardware-independent. Pressing a given key always generates the same virtual key code on any supported keyboard.

As keys are pressed, the system uses the virtual key codes to create low-level keyboard events. For information on how to simulate a keyboard event, see the function [`init(keyboardEventSource:virtualKey:keyDown:)`](/documentation/CoreGraphics/CGEvent/init(keyboardEventSource:virtualKey:keyDown:)).

---

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)
