pressKeyCode has been deprecated - Java-client 6.0.0


#1

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?


#2

((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

Thank you Ben, it works.


#4

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


#5

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();
    }

#6

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

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


#7

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);