Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a keyboard shortcut (Ctrl + Q) to close Kivy application?

Is there an option to create keyboard shortcut Ex.- Ctrl+Q to exit the application / close the window of a desktop application written in Kivy and Python? I am on Windows OS.

Thank you in advance.

  • Arnab
like image 382
Arnab Majumder Avatar asked Sep 03 '25 04:09

Arnab Majumder


1 Answers

Kivy's Window.on_keyboard (doc) event allows you to catch keyboard key pressing event.

Example app that exits if press ctrl+q:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window


class RootWidget(BoxLayout):
    pass


class TestApp(App):
    def build(self):
        Window.bind(on_keyboard=self.on_keyboard)  # bind our handler
        return RootWidget()

    def on_keyboard(self, window, key, scancode, codepoint, modifier):
        if modifier == ['ctrl'] and codepoint == 'q':
            self.stop()


if __name__ == '__main__':
    TestApp().run()
like image 157
Mikhail Gerasimov Avatar answered Sep 05 '25 22:09

Mikhail Gerasimov