*

Welcome, Guest. Please login or register.

Get your own OLPC - Buy an XO laptop on eBay!
Pages: [1]
Print
Author Topic: remapping game-pad keys in Read Activity  (Read 10233 times)

remapping game-pad keys in Read Activity

braingram
Commenter

Posts: 7


January 13, 2008, 07:27:39 PM

I've edited the Read activity to zoom in and out when the game-pad keys are pressed (square and check). However, I'm not sure how to remove the other key-handler (so that pressing square also goes to the beginning of the document). I think it may involve doing something to the gtk.ScrolledWindow that contains the Evince view, but here is where I'm lost.

Any ideas? I think that the ability to zoom in and out while in ebook mode would be very useful.
Logged

#1 Re: remapping game-pad keys in Read Activity

tdang
Senior Contributor
**
Posts: 166


January 13, 2008, 09:05:25 PM

Hey braingram-

Do you think you could post your code changes here? Id' love to see them, both for the sake of repeating them, and for learning.

Thanks

-Timothy
Logged

My XO
If in your neighborhood, I'll appear as "Abiyoyo".

#2 Re: remapping game-pad keys in Read Activity

braingram
Commenter

Posts: 7


January 14, 2008, 05:35:33 AM

Sure thing!

Basically, I opened up /usr/share/activities/Read.Activity/readactivity.py

Went to the end of the file (to the callback for the keypress event, line 255 and changed it to this:
Code:
    def _key_press_event_cb(self, widget, event):
        keyname = gtk.gdk.keyval_name(event.keyval)
        logging.debug("Keyname Press: %s, time: %s", keyname, event.time)
        if keyname == 'KP_End'
                self._view_toolbar._zoom_in_cb(self._view_toolbar._zoom_in)
        if keyname == 'KP_Home'
                self._view_toolbar._zoom_out_cb(self._view_toolbar._zoom_out)
        if keyname == 'c' and event.state & gtk.gdk.CONTROL_MASK:
            self._view.copy()
basically I'm mimicking a press of the 'zoom in' and 'zoom out' buttons on the 'View toolbar' so that the zoom values stay up-to-date.

Hope this helps!
Logged

#3 Re: remapping game-pad keys in Read Activity

Ben James Ben
Senior Contributor
**
Posts: 142



January 14, 2008, 07:46:23 AM

Braingram, I'm unfamiliar with this area, but this Gtk documentation indicates that if a callback returns TRUE, then the event is consumed.

Quote
The value returned from this function indicates whether the event should be propagated further by the GTK event handling mechanism. Returning TRUE indicates that the event has been handled, and that it should not propagate further. Returning FALSE continues the normal event handling.

If this is so, then the fix would be:

Code:
        if keyname == 'KP_End'
                self._view_toolbar._zoom_in_cb(self._view_toolbar._zoom_in)
                return True
        if keyname == 'KP_Home'
                self._view_toolbar._zoom_out_cb(self._view_toolbar._zoom_out)
                return True

The XO-1 uses Python 2.5. The True and False constant bools were added in version 2.3. In versions before that, the language used 1 and 0 to represent TRUE and FALSE. Apparently, if you use return without a value or the function doesn't have a return statement, then the special constant None is returned.
« Last Edit: January 14, 2008, 08:11:54 AM by Ben James Ben » Logged

#4 Re: remapping game-pad keys in Read Activity

braingram
Commenter

Posts: 7


January 14, 2008, 09:31:32 AM

Excellent! Works like a charm Smiley Thanks Ben
Logged

#5 Re: remapping game-pad keys in Read Activity

tdang
Senior Contributor
**
Posts: 166


January 14, 2008, 10:48:21 AM

That's friggin' AWESOME.

Thanks braingram & Ben. I also learned a bit about using the log viewer, since I first entered the code with typos, and the log viewer helped me figure it out.

This is what I hoped for as an excuse for my XO being more than a toy. I'm an academic, and read too many papers in PDF format. These days I usually try not to print them out, and read them on my iBook. But that's not as comfortable as I'd like, not quite as portable as I'd like (particularly since my iBook is old and the battery is crap now), and I can't read outside.

The zoom was the remaining required feature, so now I'm set for the XO being a practical tool for me, as well as fun.

-Timothy
Logged

My XO
If in your neighborhood, I'll appear as "Abiyoyo".

#6 Re: remapping game-pad keys in Read Activity

dlang
New

Posts: 2


January 15, 2008, 08:11:53 AM

what would I need to do to map the game keys to something else.

I have a case where I want to change the game keys to do delete/undelete ('d' and 'u')

is it as simple a saying something like if keyname == 'KP_End' then keyname = 'd'?
Logged

#7 Re: remapping game-pad keys in Read Activity

braingram
Commenter

Posts: 7


January 16, 2008, 11:51:18 AM

Not exactly. One way you could do it, is to create and then emit a fake key press event.
You could try something like this
Code:
if keyname == 'KP_END':
  e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
  e.state = 0
  e.keyval =  gtk.gdk.keyval_from_name('KP_Delete')# look in keysyms.py maybe??
  e.hardware_keycode = 107 # this may be wrong
  e.window = self.window # I don't know if this is the right window
  e.time = 0
  self.emit('key_press_event',e)
You'd probably have to flush out exactly what values you'd need but that's the general format.

Or you could just map the game pad keypress to a delete action
Code:
if keyname=='KP_END'
  your_delete_function()
if keyname=='KP_HOME'
  your_undelete_function()
Logged

#8 Re: remapping game-pad keys in Read Activity

dlang
New

Posts: 2


January 16, 2008, 06:46:44 PM

in my case I'm trying to change how they work in a terminal window so I can't call my own delete function.
Logged

#9 Re: remapping game-pad keys in Read Activity

baloo22
Commenter

Posts: 10


January 21, 2008, 07:52:34 PM

Braingram, I'm unfamiliar with this area, but this Gtk documentation indicates that if a callback returns TRUE, then the event is consumed.

Quote
The value returned from this function indicates whether the event should be propagated further by the GTK event handling mechanism. Returning TRUE indicates that the event has been handled, and that it should not propagate further. Returning FALSE continues the normal event handling.

If this is so, then the fix would be:

Code:
        if keyname == 'KP_End'
                self._view_toolbar._zoom_in_cb(self._view_toolbar._zoom_in)
                return True
        if keyname == 'KP_Home'
                self._view_toolbar._zoom_out_cb(self._view_toolbar._zoom_out)
                return True

The XO-1 uses Python 2.5. The True and False constant bools were added in version 2.3. In versions before that, the language used 1 and 0 to represent TRUE and FALSE. Apparently, if you use return without a value or the function doesn't have a return statement, then the special constant None is returned.



For the newBies like me the code is incorrect. you need semi-colon (Smiley at the end of the line with an if. For more experimented user it's a no brainer but for me well took me a day... Kiss

here is the final code, if you type this exactly the remapping will be a success.

if keyname == 'KP_End':
                self._view_toolbar._zoom_in_cb(self._view_toolbar._zoom_in)
                return True
        if keyname == 'KP_Home':
                self._view_toolbar._zoom_out_cb(self._view_toolbar._zoom_out)
                return True

Thanks a lot guys this is so cool!!!!! I'm really impressed, when i used the Read at first i said i would like zoom instead of first page/ last page and there you are doing it for us

THanks

Fred
« Last Edit: January 22, 2008, 11:03:42 PM by baloo22 » Logged

#10 Re: remapping game-pad keys in Read Activity

baloo22
Commenter

Posts: 10


January 22, 2008, 04:21:38 PM

That's friggin' AWESOME.

Thanks braingram & Ben. I also learned a bit about using the log viewer, since I first entered the code with typos, and the log viewer helped me figure it out.

This is what I hoped for as an excuse for my XO being more than a toy. I'm an academic, and read too many papers in PDF format. These days I usually try not to print them out, and read them on my iBook. But that's not as comfortable as I'd like, not quite as portable as I'd like (particularly since my iBook is old and the battery is crap now), and I can't read outside.

The zoom was the remaining required feature, so now I'm set for the XO being a practical tool for me, as well as fun.

-Timothy


could anyone who suceed to remap the key, send me a mail with the file  the readactivity.py attached?

i would love to try one of yours because i dont see what i did wrong.

Thanks!


Forget it i finally found it see the post just before this one.


« Last Edit: January 22, 2008, 11:06:16 PM by baloo22 » Logged
Pages: [1]
Print
Jump to:  

Members
Total Members: 2406
Latest: sembik
Stats
Total Posts: 31943
Total Topics: 3843
Online Today: 29
Online Ever: 238
(April 18, 2011, 09:48:50 PM)
Users Online
Users: 0
Guests: 15
Total: 15