josemaria.alkala avatar josemaria.alkala committed 379e202

Clutter: capturing key presses

Comments (0)

Files changed (4)

clutter_key_events.rst

+Clutter: capturing key events
+=============================
+
+The following example shows how to capture key presses in Clutter. The example if heavily based on snippet from `Nick Veitch <http://www.eurion.net/python-snippets/snippet/Process%20keypresses%20in%20Clutter.html>`_::
+
+  #!/usr/bin/env python
+  #! -*- coding: utf-8 -*- 
+
+  from gi.repository import Clutter
+  import sys
+
+  # define some colours (takes RGBA values)
+  red = Clutter.Color().new(255,0,0,255)
+  green = Clutter.Color().new(0,255,0,255)
+  blue = Clutter.Color().new(0,0,255,255)
+  black = Clutter.Color().new(0,0,0,255)
+  white = Clutter.Color().new(255,255,255,255)
+
+  # Define a callback method for key presses
+  def keyPress(self, event):
+      # Parses the keyboard input generated by a callback from the Stage object
+      # The event object has the property 'keyval'
+      # clutter also defines constants for keyvals
+      # so we can use these to compare
+      if event.keyval == Clutter.q:
+	  #if the user pressed "q" quit the test
+	  Clutter.main_quit()
+      elif event.keyval == Clutter.r:
+	  #if the user pressed "r" make the actor red
+	  text_actor.set_color(red)
+	  text_actor.set_text("This is Red")
+      elif event.keyval == Clutter.g:
+	  #if the user pressed "g" make the actor green
+	  text_actor.set_color(green)
+	  text_actor.set_text("This is Green")
+      elif event.keyval == Clutter.b:
+	  #if the user pressed "b" make the actor blue
+	  text_actor.set_color(blue)
+	  text_actor.set_text("This is Blue")
+      elif event.keyval == Clutter.Up:
+	  # 'Up' is equal to the cursor up key
+	  stage.set_color(black)
+      elif event.keyval == Clutter.Down:
+	  # 'Down' is equal to the cursor down key
+	  stage.set_color(white)
+      #event feedback, useful for working out values of tricky keys
+      print('event processed',  event.keyval)
+
+  if __name__ == '__main__':
+     Clutter.init( sys.argv )
+
+     # Stage creation
+     stage = Clutter.Stage()
+     stage.set_size(450, 180)
+     stage.set_color(black)
+
+     # Text Actor
+     text_actor=Clutter.Text()
+     text_actor.set_color(red)
+     text_actor.set_position(20,70)
+     text_actor.set_font_name('Sans 24')
+     text_actor.set_text('Press a Key:\nUp, Down, r, g, b, q')
+
+     # Add the actor to the stage
+     stage.add_actor( text_actor )
+
+     # Show the stage
+     stage.show_all()
+
+     # Connect signals
+     stage.connect("destroy",lambda w: Clutter.main_quit() )
+     stage.connect('key-press-event', keyPress)
+
+     # Pass execution to the main clutter loop
+     Clutter.main()
+
    You can adapt this file completely to your liking, but it should at least
    contain the root `toctree` directive.
 
-Welcome to Clutter and Mx's documentation!
-==========================================
+This is a Clutter and Mx introduction for Python3
+=================================================
 
 Contents:
 
 
    introduction
    basic_usage
+   clutter_key_presses
 
    resources
 
 Mx
 --
 
-It is a toolkit developed on top of Clutter for the former Meego.
+It is a toolkit developed on top of Clutter. Originally developped for Maemo/Moblin then merged into Moblin (now dead and evolved into `Tizen <https://www.tizen.org/>`_), but as for now is an independent project.
 
+Other technologies
+------------------
 
+It seems that this can be used with other technologies such as:
 
+* Gtk
+* Gstreamer
+* Cairo
 
+
+
+
+
+
 Resources
 =========
 
-* `Clutter Cookbook <http://docs.clutter-project.org/docs/clutter-cookbook/1.0/>`_
+Clutter
+-------
+
+* `Cookbook <http://docs.clutter-project.org/docs/clutter-cookbook/1.0/>`_
+* `Reference Manual <http://developer.gnome.org/clutter/stable/>`_
+* `Wiki <http://wiki.clutter-project.org/wiki/Main_Page>`_
+* `Example <http://wiki.clutter-project.org/wiki/ExamplePython>`_
+* `Old but complete examples <https://github.com/pmarti/clutter-tutorial/tree/master/examples`_
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.