---
title: "withKeyToggled"
description: "Executes the keyboard sequence specified in the given [block], in between presses to the given
[key]. This key can also be used within the [block], as long as it is not down at the end of the
block.

If the given [key] is already down, an [IllegalStateException] will be thrown."
type: "function"
---

<div class='type'>Function</div>


<a id='references'></a>
<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun KeyInjectionScope.withKeyToggled(key: Key, block: KeyInjectionScope.() -> Unit)
```


Executes the keyboard sequence specified in the given `block`, in between presses to the given
`key`. This key can also be used within the `block`, as long as it is not down at the end of the
block.

If the given `key` is already down, an `IllegalStateException` will be thrown.

#### Parameters

| | |
| --- | --- |
| key | The key to be toggled around the injection of the `block`. |
| block | Sequence of KeyInjectionScope methods to be injected with the given key down. |




