[Sugar-devel] ASCII keypresses

Art Hunkins abhunkin at uncg.edu
Tue Aug 23 14:45:49 EDT 2011


James,
Thanks so much for this response; it solved my keypress problem 
magnificently - especially the autorepeat issue.

One problem remained, for my Csound program: a *bonafide repeat keypress* 
could not be recognized. So I modified the python code to send along an 
ASCII "0" when a key is released; this tells Csound that the following 
"repeat" is a bonafide keypress. Here is my final code:

def onKeyPress(self, widget, event):
   if self.p:
     keyval = event.keyval
     if keyval in self.kp:
       return True
     self.kp.append(keyval)
     self.w.set_channel("ascii", keyval)
     return True

 def onKeyRelease(self, widget, event):
   self.kp.remove(event.keyval)
   self.w.set_channel("ascii", 0)
   return True

 def playcsd(self, widget):
  .........
       self.connect("key-press-event", self.onKeyPress)
       self.connect("key-release-event", self.onKeyRelease)

Again, many thanks for your help. I'm really happy with the way this turned 
out.

Art Hunkins

----- Original Message ----- 
From: "James Cameron" <quozl at laptop.org>
To: "Art Hunkins" <abhunkin at uncg.edu>
Cc: <sugar-devel at lists.sugarlabs.org>
Sent: Monday, August 15, 2011 10:41 PM
Subject: Re: [Sugar-devel] ASCII keypresses


> Perhaps your question is "How do I defeat autorepeat?"
>
> When a key is pressed down, you will get a key-press-event for it.
>
> When a key is released, you will get a key-release-event for it.
>
> If a key is pressed, held down, and then released, you will get an
> initial key-press-event, then a key-press-event for each autorepeat,
> followed by the key-release-event.
>
> It is up to your program to process this stream.
>
> I don't know offhand how to defeat autorepeat for a widget, the GTK+
> documentation hasn't been any help on this question.
>
> But here's one potential method; remember that is a key is down, and if
> it is, ignore the new event.  Here is code using a list.
>
> def __init__(self, ...):
>    self.kp = []  # keys pressed
>
> def onKeyPress(self, widget, event):
>    keyval = event.keyval
>    if keyval in self.kp:
> return True  # this is an autorepeat key press event
>    self.kp.append(keyval)
>    if self.p:
>        self.w.set_channel("ascii", keyval)
>    return True
>
> def onKeyRelease(self, widget, event):
>    self.kp.remove(event.keyval)
>    return True
>
> def playcsd(...):
>    ...
>    self.connect("key-press-event", self.onKeyPress)
>    self.connect("key-release-event", self.onKeyRelease)
>
> As you can see, storing the status of all keys that are down is quite
> easy.
>
> -- 
> James Cameron
> http://quozl.linux.org.au/ 



More information about the Sugar-devel mailing list