Kivy event with keyboard

0

I'm creating a piano with kivy is working fine, but just with the mouse click on the Button, I want to know how to do when typing a button the Button to be executed without having to click 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()
    
asked by anonymous 27.10.2017 / 19:44

1 answer

0

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

from kivy.core.window import Window

So you can get keyboard events from the request_keyboard ". The first parameter of the method is a callback function that will be executed when the keyboard is closed; the second parameter is 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 method return will be an instance of Keyboard , so you can bind to the on_key_down event for a method of your:

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] ]()

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

Reference

link

    
28.10.2017 / 18:50