pressKeyCode has been deprecated - Java-client 6.0.0

Hi,
I’m wondering what is the substitute for deprecated pressKeyCode on Java-client 6.0.0. I’m currently using

((AndroidDriver) driver.getKeyboard()).pressKeyCode(AndroidKeyCode.DEL)

…to press DELETE button. Any examples?

1 Like

((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.HOME));

/**
 * Send a key event to the device.
 *
 * @deprecated use {@link #pressKey(KeyEvent)} instead
 *
 * @param key code for the key pressed on the device.
 */
@Deprecated
default void pressKeyCode(int key) {
    CommandExecutionHelper.execute(this, pressKeyCodeCommand(key));
}
3 Likes

Thank you Ben, it works.

Hi Ben ,
I am getting error “The constructor KeyEvent(AndroidKey) is undefined”.
Can you please tell me what am I missing?

What version of Appium Java Client are you using? In 6.1.0 it should be there:

public class KeyEvent {
    private Integer keyCode;
    private Integer metaState;
    private Integer flags;

    public KeyEvent() {
    }

    public KeyEvent(AndroidKey key) {
        this.keyCode = key.getCode();
    }

Use
driver.pressKey(new KeyEvent(AndroidKey.DEL));

Imports:
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;

i have tried all of these but none of them worked for me

((PressesKeyCode) driver).pressKeyCode(AndroidKeyCode.MENU); ((PressesKeyCode) driver).pressKeyCode(AndroidKeyCode.MENU, AndroidKeyMetastate.META_SHIFT_ON);
AppiumDriver.sendKeyEvent(AndroidKeyCode.MENU);
HashMap swipeObject = new HashMap(); swipeObject.put(“keycode”, 82);
((JavascriptExecutor ) driver).executeScript(“mobile: keyevent”, swipeObject);

Try this code .

((AndroidDriver<?>) driver).pressKey(new KeyEvent(AndroidKey.CAMERA));

KeyEvents list here AndroidKeys

def press_keycode(self: T, keycode: int, metastate: Optional[int] = None, flags: Optional[int] = None) -> T:
    """Sends a keycode to the device.

Is not marked as deprecated in my Python client, so how is this official documentation getting pushed out? I’m also finding this perplexing because the python interface does not map semantically to the java interface for example keyevent is a java object, but it’s a python method that sends(). I get the impression the documentation would solve a lot of questions off the bat?