[Python] Kivy converted Android App enabling remote control

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am having a real mare trying to get a remote control or dpad to work to scroll menus in my Kivy app converted to Android APK, No matter what I try I just can't seem to be able to get a remote control or dpad to be able to scrol menus / buttons on the app as soon as its on my Android TV.

    I've tried on_button_press, I've even setup a a key handel

    from kivy.app import App
    from kivy.core.window import Window
    from kivy.uix.button import Button

    try:
    from android import log
    except ImportError:
    def log(tag, msg): print(f"[{tag}] {msg}") # fallback for desktop

    # The Ultimate Key Mapping Dictionary
    KEYCODE_MAP = {
    19: 'up', # KEYCODE_DPAD_UP
    20: 'down', # KEYCODE_DPAD_DOWN
    21: 'left', # KEYCODE_DPAD_LEFT
    22: 'right', # KEYCODE_DPAD_RIGHT
    23: 'select', # KEYCODE_DPAD_CENTER

    273: 'up',
    274: 'down',
    275: 'right',
    276: 'left',
    13: 'select',

    96: 'select',
    97: 'back',
    99: 'select',
    100: 'menu',
    109: 'back',
    110: 'back',
    108: 'select',
    106: 'right',
    105: 'left',
    66: 'select',
    160: 'select',
    4: 'back',
    82: 'menu',
    111: 'escape',

    7: 'select',
    8: 'select',
    9: 'select',
    10: 'select',
    11: 'select',
    12: 'select',
    13: 'select',
    14: 'select',
    15: 'select',
    16: 'select',

    85: 'pause',
    126: 'play',
    127: 'pause',
    86: 'stop',
    87: 'next',
    88: 'prev',
    165: 'info',
    }

    class TVKeyHandler:
    def __init__(self, app):
    self.app = app # Pass in SportsApp instance

    def handle_key(self, keycode):
    action = KEYCODE_MAP.get(keycode)
    log("remote-event", f"KEY RECEIVED: {keycode} → {action}")
    focused = self.get_focused()

    if not focused:
    log("remote-event", "⚠️ No focused widget — attempting fallback")
    try:
    screen = self.app.root.get_screen('MainScreen')
    screen.ids.today_button.focus = True
    log("remote-event", "✅ Focus restored to today_button")
    return True
    except Exception as e:
    log("remote-event", f"❌ Focus recovery failed: {e}")
    return False
    if not action:
    log("remote-event", f"Unhandled keycode: {keycode}")
    return False

    if action == 'up':
    focused.focus_previous('up')
    elif action == 'down':
    focused.focus_next('down')
    elif action == 'right':
    focused.focus_next('right')
    elif action == 'left':
    focused.focus_previous('left')
    elif action == 'select':
    if isinstance(focused, Button):
    focused.trigger_action(duration=0.1)
    elif action == 'back' or action == 'escape':
    App.get_running_app().stop()
    elif action == 'menu':
    log("remote-event", "Menu button pressed — implement overlay or settings")
    else:
    log("remote-event", f"Unhandled mapped action: {action}")
    return False

    return True

    def get_focused(self):
    from kivy.core.window import Window
    return Window.focused_widget or None


    I've tried seting up a custom android key listeaner. If I send commands via adb the menus work, but using the remote control just does nothing.

    It seems the key pad controls just don't make it from the android system to the APP. Is there a solution? I've tried google and I just can't seem to find much documented on using DPAD or remote controls, so is it even possible? Am I missing something very simple?

    Continue reading...

Compartilhe esta Página