New to kivy: need help figuring out how to do keyboard events and polling.

3,190 views
Skip to first unread message

Alejandro Ramirez Escanellas

unread,
Jun 25, 2013, 4:28:19 PM6/25/13
to kivy-...@googlegroups.com
So I started learning kivy about a week or two ago. I finished the pong tutorial and for a bit of extra practice I decided to give it keyboard controls for moving the paddles. I've had little luck figuring out how to do this...

this is what I want to do: I have two separate class for the paddles, yes superfluous, and I want each of these paddles to contain their own keyboard code for moving themselves. For the life of me I just can't find anything on doing this. Can anyone throw some help my way? It would be greatly appreciated!

Gabriel Pettier

unread,
Jun 25, 2013, 6:37:44 PM6/25/13
to kivy-...@googlegroups.com
Hi

did you see the kiv.core.window.Window doc? This is the object that
dispatch key event, you can have your objects bind to
on_keyboard_down/up events on it, to have the method of your choice
called on keyboard events.

Hope that helps.
> --
> You received this message because you are subscribed to the Google Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Alejandro Ramirez Escanellas

unread,
Jun 26, 2013, 12:41:45 AM6/26/13
to kivy-...@googlegroups.com
Alrighty, so I tried applying they keyboard code I found. My two paddles look like this:


class PongPaddleA(Widget):
    score = NumericProperty(0)

    def __init__(self, **kwargs):
        super(PongPaddleA, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
        print 'My keyboard have been closed!'
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
       
        if keycode[1] == 'q':
            self.center_y += 14
        elif keycode[1] == 'a':
            self.center_y -= 14
       
        return True

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y)/(self.height/2)
            bounced = Vector(-1 * vx, vy)
           
            print (bounced.x)
            if bounced.x < 35 and bounced.x > -35:
                vel = bounced * 1.1
            else:
                vel = bounced

            ball.velocity = vel.x, vel.y + offset

#------------------------------------------------------------------------

class PongPaddleB(Widget):
    score = NumericProperty(0)

    def __init__(self, **kwargs):
        super(PongPaddleB, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
        print 'My keyboard have been closed!'
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
       
        if keycode[1] == 'up':
            self.center_y += 14
        elif keycode[1] == 'down':
            self.center_y -= 14
       
        return True

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y)/(self.height/2)
            bounced = Vector(-1 * vx, vy)
           
            print (bounced.x)
            if bounced.x < 35 and bounced.x > -35:
                vel = bounced * 1.1
            else:
                vel = bounced

            ball.velocity = vel.x, vel.y + offset

Only paddleB works...

Also, when I hold the button, there is a slight pause before the paddle continued to go up without pause. Is there a way to take out that pause?

Regardless, thank you.

Gabriel Pettier

unread,
Jun 26, 2013, 2:41:13 AM6/26/13
to kivy-...@googlegroups.com
I don't think you need request_keyboard here, you can directly bind to
Window.on_key_down/up

http://kivy.org/docs/api-kivy.core.window.html?highlight=keyboard#kivy.core.window.WindowBase.on_key_down

I think the way you currently do, takes the keyboard from PongPaddleA to
give et to PongPaddleB, that's why it doesn't work. Using events on
Window (which is a WindowBase instance), multiple widgets can listen at
the same time.
> > an email to kivy-users+...@googlegroups.com <javascript:>.

Alejandro Ramirez Escanellas

unread,
Jun 26, 2013, 11:03:21 AM6/26/13
to kivy-...@googlegroups.com
So the answer is to use on_key_down as an event? I don't suppose you could provide an example. I've been looking all over for some documentation or examples that can help me out and I just can't find any.

Akshay Arora

unread,
Jun 27, 2013, 3:23:40 AM6/27/13
to kivy-...@googlegroups.com


    from kivy.core.window import Window
    ...
    Window.bind(on_key_down=self.my_key_callback)
    ...
    ...
    def my_key_callback(self, keyboard, keycode, text, modifiers):
        ...


On Wed, Jun 26, 2013 at 8:33 PM, Alejandro Ramirez Escanellas <alejandr...@gmail.com> wrote:
So the answer is to use on_key_down as an event? I don't suppose you could provide an example. I've been looking all over for some documentation or examples that can help me out and I just can't find any.

Alejandro Ramirez Escanellas

unread,
Jun 30, 2013, 12:18:46 PM6/30/13
to kivy-...@googlegroups.com
Thank you, though for me I have to define the my_key_callback first and then call Window.bind(on_key_down = my_key_callback) without the self. Odd? Either way, thanks.

Kyle Hardin

unread,
Jan 22, 2016, 10:37:21 AM1/22/16
to Kivy users support
I switch from kivy 1.9.0 to 1.9.1 and my keyboard shortcut routine no longer behaves the same. I have a class CompUI which a kivy.uix.screenmanager.Screen.

I use the following code in the init for the CompUI to bind to the keyboard:

Window.bind(on_keyboard=self.KeyboardShortcut)

I have a KeyboardShortcut method as follows:

def KeyboardShortcut(self, window, key, *args):
   try:
      # d key moves focus to tiwireDiam
      if key == 100:
         self.ids.tiwireDiam.focus = True
         return True
   except:
      return False

When using kivy 1.9.0 all of the keyboard shortcuts located in the KeyboardShortcut method moved the focus to different TextInput objects but did not actually put the keyboard shortcut character into the TextInput that was in focus. My understanding was that the "return True" in the KeyboardShortcut method caused the character to not be used by the system so it would not be placed as text in the TextInput.

In 1.9.1, this behavior has gone away and the shortcut character is being entered into the TextInput that gains focus.

Do I need to change the code for 1.9.1 to handle this differently? Also, I want to avoid using a filter on the TextInputs to look for these shortcuts. I want to handle this at the keyboard listener stage.

Alexander Taylor

unread,
Jan 22, 2016, 1:12:27 PM1/22/16
to Kivy users support
Kyle: Please don't resurrect really old threads for new issues, it's fine to open a new one.

Has this update switched you from pygame to sdl2 as the backend? I remember someone mentioning a bug with sdl2 keyboard arguments, but I don't know if it could be related to this.

Sebastian Adil

unread,
Sep 22, 2017, 4:42:27 PM9/22/17
to Kivy users support
   Hy
   I am also new in kivy, But what I don't understand is that the "on_key_down()" is a method but you use it as a variable "self._keyboard.bind(on_key_down=self._on_keyboard_down)".
  Sorry for my question in a low level.

Ehem Excuse me?

unread,
Feb 12, 2018, 3:28:57 PM2/12/18
to Kivy users support

They are passing in the function, it looks like it's a variable but it's basically pointing at the function saying "Use that" if they did do  self._keyboard.bind(on_key_
down=self._on_keyboard_down())" (with the parentheses) it would give it what the function returns which is not the desired output.
Reply all
Reply to author
Forward
0 new messages