# Keyboard Events

## Hide Keyboard:

Create a reusable function and call when required in test script

{% code title="helper.java" %}

```java
 public static void hideKeyboard() throws Exception {
        try {
            driver.hideKeyboard();
        } catch (Exception e) {
            throw new Exception("Keyboard should auto-trigger but didn't");
        }
    }
```

{% endcode %}

To hide keyboard call above function from `helper.java` class

```
test.java

$ helper.hideKeyboard();
```

## Open Keyboard:

To create a reusable function and call in test script

{% code title="helper.java" %}

```java
  // Android 
  public static void getKeyboard() throws Exception {
        if (driver instanceof AndroidDriver) {
            ((AndroidDriver) driver).pressKeyCode(AndroidKeyCode.KEYCODE_PAGE_UP);
        }
    }
```

{% endcode %}

To open keyboard call above function from `helper.java` reusable class

```
test.java

$ helper.getKeyboard();
```

## Press key Enter:

To create a reusable function and call in test script

{% code title="helper.java" %}

```java
  // Android 
   public static void pressKeyEnter() {
        if (driver instanceof AndroidDriver) {
            ((AndroidDriver) driver).pressKeyCode(AndroidKeyCode.ENTER);
        }
    }
```

{% endcode %}

To press key ENTER call above function from `helper.java` reusable class

```
test.java

$ helper.pressKeyEnter();
```

## Press key UP:

To create a reusable function and call in test script

{% code title="helper.java" %}

```java
  // Android 
   public static void pressKeyUp() {
        if (driver instanceof AndroidDriver) {
            ((AndroidDriver) driver).pressKeyCode(AndroidKeyCode.ACTION_UP);
        }
    }

```

{% endcode %}

To press key ENTER call above function from `helper.java` reusable class

```
test.java

$ helper.pressKeyUp();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vinayak-titti.gitbook.io/appium-cookbook/recipes/keyboard-events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
