# DialogInterface.OnKeyListener

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# OnKeyListener

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/content/DialogInterface.OnKeyListener "View this page in Java")  

```
interface OnKeyListener
```

|------------------------------------------------------------------------------------------------------------------------------------------------|
| [android.content.DialogInterface.OnKeyListener](https://developer.android.com/reference/kotlin/android/content/DialogInterface.OnKeyListener#) |

Interface definition for a callback to be invoked when a key event is dispatched to this dialog. The callback will be invoked before the key event is given to the dialog.

## Summary

|                                                                                                                                                                                                                                                                                                               Public methods                                                                                                                                                                                                                                                                                                                ||
|--------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| abstract[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [onKey](https://developer.android.com/reference/kotlin/android/content/DialogInterface.OnKeyListener#onKey(android.content.DialogInterface,%20kotlin.Int,%20android.view.KeyEvent))`(`dialog:` `[DialogInterface](https://developer.android.com/reference/kotlin/android/content/DialogInterface)!`, `keyCode:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`, `event:` `[KeyEvent](https://developer.android.com/reference/kotlin/android/view/KeyEvent.html#)!`)` Called when a key is dispatched to a dialog. |

## Public methods

### onKey

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
abstract fun onKey(
    dialog: DialogInterface!, 
    keyCode: Int, 
    event: KeyEvent!
): Boolean
```

Called when a key is dispatched to a dialog. This allows listeners to get a chance to respond before the dialog.

|                                                                             Parameters                                                                             ||
|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| `dialog`  | [DialogInterface](https://developer.android.com/reference/kotlin/android/content/DialogInterface)!:the dialog the key has been dispatched to            |
| `keyCode` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):the code for the physical key that was pressed                               |
| `event`   | [KeyEvent](https://developer.android.com/reference/kotlin/android/view/KeyEvent.html#)!:the KeyEvent object containing full information about the event |

|                                                                      Return                                                                       ||
|------------------------------------------------------------------------------------|---------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true`if the listener has consumed the event,`false`otherwise |