Event Key Charcode
## KeyboardEvent charCode Property
The `charCode` property returns the Unicode character code of the key that was pressed during an `onkeypress` event.
A Unicode character code is a numeric representation of a character (for example, the number `97` represents the lowercase letter `"a"`).
> **Deprecated Notice:** The `charCode` property is deprecated in modern web standards. For modern applications, it is highly recommended to use the [`key`](#related-pages) property instead.
---
## Syntax
```javascript
event.charCode
```
### Technical Details
| Metric | Description |
| :--- | :--- |
| **Return Value** | A Number representing the Unicode character code of the pressed key. Returns `0` if the event is not a keypress. |
| **DOM Version** | DOM Level 2 Events |
| **Read-Only** | Yes |
---
## How It Works and Key Considerations
* **Event Compatibility:** The `charCode` property is only populated during the `onkeypress` event. If used with `onkeydown` or `onkeyup` events, it will always return `0`.
* **Converting Codes to Characters:** To convert a Unicode value returned by `charCode` back into a readable character string, use the static method `String.fromCharCode()`:
```javascript
var character = String.fromCharCode(event.charCode);
```
* **Cross-Browser Compatibility:**
* Legacy browsers (such as Internet Explorer 8 and earlier) do not support the `charCode` or `which` properties. They use `keyCode` instead.
* Conversely, the `keyCode` property is sometimes unreliable or behaves differently during `onkeypress` events in older versions of Firefox.
* To ensure robust cross-browser compatibility for older environments, use the following fallback pattern:
```javascript
var code = event.charCode || event.keyCode;
```
* **Modifier Keys:** To detect if modifier keys like `ALT`, `CTRL`, `META`, or `SHIFT` were held down during the key event, use the [`altKey`](#), [`ctrlKey`](#), [`metaKey`](#), or [`shiftKey`](#) properties.
---
## Browser Support
The numbers in the table specify the first browser version that fully supports the `charCode` property.
| Property | Chrome | Internet Explorer | Firefox | Safari | Opera |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **charCode** | Yes | 9.0 | Yes | Yes | Yes |
---
## Code Examples
### Example 1: Basic Usage
Get the Unicode value of the pressed keyboard key:
```javascript
// Example output: 119 (which represents the letter "w")
var x = event.charCode;
```
### Example 2: Cross-Browser Compatibility Fallback
A safe way to retrieve the character code across all browsers, including Internet Explorer 8 and earlier:
```javascript
// Fallback to keyCode if charCode is undefined
var x = event.charCode || event.keyCode;
```
### Example 3: Detecting a Specific Key Press
Trigger an alert when the user presses the "O" key (either lowercase "o" or uppercase "O"):
```javascript
function myFunction(event) {
// Get the character code with cross-browser fallback
var x = event.charCode || event.keyCode;
// 111 is 'o', 79 is 'O'
if (x == 111 || x == 79) {
alert("You pressed the 'O' key!");
}
}
```
### Example 4: Converting Unicode to a Character
Retrieve the Unicode value and convert it into a readable string character:
```javascript
// Get the Unicode value
var code = event.charCode || event.keyCode;
// Convert the Unicode value to a string character
var character = String.fromCharCode(code);
```
---
## Related Pages
* HTML DOM Reference: (event-key-key.html)
* HTML DOM Reference: (event-key-keycode.html)
* HTML DOM Reference: (event-key-which.html)
YouTip