How to click on home back and recent button in android?

please help me to click on these buttons…

thanks in advance.

regards,
Sudhanva.M

You can simulate pressing these keys using the appium driver’s press_keycode method, which calls adb’s “input keyevent”, shown here in Ruby

appium_driver.driver.press_keycode <keycode>

Back is 4
Menu is 82
Recent Apps is 187 (APP_SWITCH)

You can find the full list of keycodes here:
http://developer.android.com/reference/android/view/KeyEvent.html

2 Likes

Hi Can you help with the above code

appium_driver.driver.press_keycode

in Java

appium_driver.driver.pressKeyCode

((AndroidDriver) driver).pressKeyCode(AndroidKeyCode.BACK);
((AndroidDriver) driver).pressKeyCode(AndroidKeyCode.HOME);

the driver which i am using is of AndroidDriver so i guess there is no need to type cast that.
Any which ways the ide is not prompting me with pressKeyCode method :sweat:

I got the solution for the same driver.sendKeyEvent(keycode) for menu button the key code is 82 :smiley:

the same is:

((AndroidDriver) driver).pressKeyCode(AndroidKeyCode.KEYCODE_MENU);

your java-client is old version :slight_smile:

How to press the same in C#… I tried but not able to find any near function to above solutions!!! Help will be appreciated.

Python code for hardware back button
driver.press_keycode(4)

1 Like

driver.navigate().back(); works perfectly for me

This is fine to navigate back…but if i want to click on CAMERA button
pressKeyCode is not working for me even after using java client 5.0
please let me know if someone has implemented the same.

Hello @ranjith_hs,
Which Event Number you are using?

I was using appium java client 3.4.1 with SendKeyEvent

now that i am not able to use PressKeyCode with 3.4.1

Hi willosser,
I’m new to Appium, I have the same question how to use the return button in Ruby, I already tried driver.sendKeyEvent(4) but it gives me error. can you help me with this

undefined local variable or method `appium_driver’

Hi Viswesvar,

You’ll have to provide a bit more information, such as your code.

I’m guessing that you expect the object appium_driver to be your appium driver. Where did you set that?

I am using the above code but its not working for me.

since pressKeyCode has been depreciated you can use

    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.MobileElement;
    import io.appium.java_client.android.AndroidDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import io.appium.java_client.android.nativekey.AndroidKey;
    import io.appium.java_client.android.nativekey.KeyEvent;
   
     //initialize AppiumDriver and DesiredCapabilities

     ((AndroidDriver<MobileElement>) driver).pressKey(new KeyEvent(AndroidKey.HOME));
     ((AndroidDriver<MobileElement>) driver).pressKey(new KeyEvent(AndroidKey.APP_SWITCH));
2 Likes