[Sugar-devel] ASCII keypresses

James Cameron quozl at laptop.org
Mon Aug 15 22:41:02 EDT 2011


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