Getting Mouse / Press / Touch Input

Next on our agenda is how to acquire mouse / finger input. In Kivy, these events are just simply called a "touch." For now, we're just going to cover a simple touch, where a touch is just a single touch. That said, Kivy also supports multi-touch operations like "zoom" and "spin" motions that you might be familiar with doing on your phone or tablet.

Let's hop into the code now:

from import App
from kivy.uix.widget import Widget

class TouchInput(Widget):

    def on_touch_down(self, touch):
    def on_touch_move(self, touch):
    def on_touch_up(self, touch):

class SimpleKivy4(App):

    def build(self):
        return TouchInput()

if __name__ == "__main__":

Now we've defined a new class, called TouchInput, which inherits from Widget.

We have three methods in here, on_touch_down (the initial press), on_touch_move (movement following and while there is a press) and on_touch_up (the "release" of a press). We're then printing out these events.

Next, within our root class, we're returning this TouchInput class.

Since we're only concerned about mouse / touch input in this tutorial, we're not really worried about the .kv file here. It is still being loaded, but, since none of the elements are being called to be shown, we don't see anything of the .kv file here.

Upon running, you can test the code by clicking and dragging on the screen. You should see the print out of your mouse's location for all the movement and pressing you do.

An example of the output you might see is somethign like:

<MouseMotionEvent spos=(0.665, 0.3533333333333334) pos=(532.0, 212.00000000000003)>

Here, we can see two forms of output position. "spos" is more of a relative location. Remember the float layout? 0 is 0, and 1 is "full." So, if the mouse is at 0.5, 0.5, it is right in the middle of the screen.

We also have the actual coordinate location (pos) as well.

These are both useful though you're probably going to be using spos more if you're planning on having your application being able to run full screen on many devices and types of devices.

There exists 1 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:

  • Kivy with Python tutorial for Mobile Application Development Part 1
  • Kivy Widgets and Labels
  • The Kivy .kv Language
  • Kivy .kv Language cont'd
  • Dynamic Resizable Placement
  • Layouts: Float Layout
  • Getting Mouse / Press / Touch Input
  • Simple Drawing Application
  • Builder for loading .kv Files
  • Screen Manager for Multiple Screens
  • Drawing Application with Screen Manager
  • Adding better Navigation