Kivy event with keyboard

Asked

Viewed 592 times

0

I’m creating a piano with kivy working all right, but only with the click of the mouse on the Button , I want to know how to type a key the Button run without I have to click with the mouse

from kivy.uix.button import Button
from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.uix.boxlayout import BoxLayout


class Piano(App):
    def build(self):
        c = Button(text='C',font_size=30,on_release=self.do)
        d = Button(text='D', font_size=30, on_release=self.re)
        e = Button(text='E', font_size=30, on_release=self.mi)
        f = Button(text='F', font_size=30, on_release=self.fa)
        g = Button(text='G', font_size=30, on_release=self.sol)
        a = Button(text='A', font_size=30, on_release=self.la)
        b = Button(text='B', font_size=30, on_release=self.si)
        box = BoxLayout()
        box.add_widget(c)
        box.add_widget(d)
        box.add_widget(e)
        box.add_widget(f)
        box.add_widget(g)
        box.add_widget(a)
        box.add_widget(b)
        return box`

    def do (self,c):
        som = SoundLoader.load('Songs/do.wav')
        som.play()
    def re (self,d):
        som = SoundLoader.load('Songs/re.wav')
        som.play()
    def mi (self,e):
        som = SoundLoader.load('Songs/mi.wav')
        som.play()
    def fa(self,f):
        som = SoundLoader.load('Songs/fa.wav')
        som.play()
    def sol (self,g):
        som = SoundLoader.load('Songs/sol.wav')
        som.play()
    def la (self,a):
        som = SoundLoader.load('Songs/la.wav')
        som.play()
    def si (self,b):
        som = SoundLoader.load('Songs/si.wav')
        som.play()




if __name__ in '__main__':
    Piano().run()
  • Please edit the question and enter the code you already have. The way it is, it will be difficult to answer exactly what you need.

1 answer

1

First, you will need to import the class Window to your code:

from kivy.core.window import Window

So you can get keyboard events from the method request_keyboard. The first method parameter is a function callback which will be executed when the keyboard is closed; the second parameter will be the object that will be associated with the keyboard. So, I believe you can do within your class:

class Piano(App):
    def build(self):

        self.keyboard = Window.request_keyboard(self.keyboard_closed, self)
        self.keyboard.bind(on_key_down=self.on_key_down)

        c = Button(text='C',font_size=30,on_release=self.do)
        d = Button(text='D', font_size=30, on_release=self.re)
        e = Button(text='E', font_size=30, on_release=self.mi)
        f = Button(text='F', font_size=30, on_release=self.fa)
        g = Button(text='G', font_size=30, on_release=self.sol)
        a = Button(text='A', font_size=30, on_release=self.la)
        b = Button(text='B', font_size=30, on_release=self.si)
        box = BoxLayout()
        box.add_widget(c)
        box.add_widget(d)
        box.add_widget(e)
        box.add_widget(f)
        box.add_widget(g)
        box.add_widget(a)
        box.add_widget(b)

        return box`

    def keyboard_closed(self):
        pass

The return of the method will be an instance of Keyboard, so you can do the bind with the event on_key_down for a method of its:

class Piano(App):
    def build(self):

        self.keyboard = Window.request_keyboard(self.keyboard_closed, self)

        c = Button(text='C',font_size=30,on_release=self.do)
        d = Button(text='D', font_size=30, on_release=self.re)
        e = Button(text='E', font_size=30, on_release=self.mi)
        f = Button(text='F', font_size=30, on_release=self.fa)
        g = Button(text='G', font_size=30, on_release=self.sol)
        a = Button(text='A', font_size=30, on_release=self.la)
        b = Button(text='B', font_size=30, on_release=self.si)
        box = BoxLayout()
        box.add_widget(c)
        box.add_widget(d)
        box.add_widget(e)
        box.add_widget(f)
        box.add_widget(g)
        box.add_widget(a)
        box.add_widget(b)

        return box`

    def keyboard_closed(self):
        pass

    def on_key_down(self, keyboard, keycode, text, modifiers):
        teclas = {
            "c": self.do,
            "d": self.re,
            "e": self.mi,
            "f": self.fa,
            "g": self.sol,
            "a": self.la,
            "b": self.si,
        }

        if keycode[1] in teclas:
            teclas[ keycode[1] ]()

Thus, when a key is pressed, the method on_key_down will be executed by checking if the key is one of the ones provided by the application and, if it is, executes the method that plays the audio.

Reference

https://stackoverflow.com/questions/17280341/how-do-you-check-for-keyboard-events-with-kivy

Browser other questions tagged

You are not signed in. Login or sign up in order to post.