python-appium

In Appium Python, how to press key code by key name?


I am writing Python Appium code. The code needs to press the HOME button. Right now I have

driver.press_keycode(3)

where 3 is mapped to KEYCODE_HOME according to keycode mapping.

Is there a way that I can refer KEYCODE_HOME in my code so that my code is more readable?

I could have done the following: adding a comment

# HOME Key is 3
driver.press_keycode(3)

or assigning a variable

home_keycode=3
driver.press_keycode(home_keycode)

But I'd more like to see something like

driver.press_keycode(AppiumKey.HOME)

Does such a thing exist?

Thank you


Solution

  • So you can create Enum class and call variables like Keys.HOME etc

        class Keys(Enum):
            """
            List of keys
            """
            HOME = 3
        class PO(Base):
            press_key(Keys.HOME)
    

    Or use an existing solution as well python-client nativekey.py

    press_key(AndroidKey.HOME)