[Tutor] adjust key bindings
John CORRY
john.corry at ntlworld.com
Sun Nov 26 13:11:47 CET 2006
Hi All,
I have been trying to bind the "z" key to the "Down" key using Python
2.4, Glade 2 and Pygtk. I have posted this problem on the Pygtk list
but have had no response. I was hoping somebody on the tutor list could
help. I think that I am close. I can capture the "z" key press and
assign a "Down" key press but I can't get the "Down" key press to
register on the Treeview in the GUI.
Any thoughts greatly appreciated.
Thanks,
John.
-----Original Message-----
From: pygtk-bounces at daa.com.au [mailto:pygtk-bounces at daa.com.au] On
Behalf Of John CORRY
Sent: 25 November 2006 17:23
To: 'Fabian Braennstroem'; pygtk at daa.com.au
Subject: RE: [pygtk] Re: adjust key bindings
Fabian,
I have had a go at binding the "z" key to the down cursor key. I can
block the Z key press and assign the "down" cursor key press and print
out the "down" key press but I can't redirect the signal back to the
widget. I have enclosed the code below, maybe someone else on the list
can finish off the code.
The code below assumes that you have a window in Glade with one treeview
widget. Both widgets have the "key_press_event" assigned in glade.
import shutil
import pygtk
import findgtk
import gtk
import gtk.glade
import gobject
import os
import sys
class Shopcard:
def __init__(self):
self.wTree = gtk.glade.XML ("project12.glade", "window1")
dic={"on_window1_delete_event" : self.quit100, }
self.wTree.signal_autoconnect (dic)
tree1 = self.wTree.get_widget("treeview1")
windy = self.wTree.get_widget("window1")
tree1.connect("key_press_event",self.callback3000,tree1,windy)
model = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
self.hostmodel = model
host_inter=self.insert_row(model,None,"test",'Test')
host_inter=self.insert_row(model,None,"test1",'Test')
host_inter=self.insert_row(model,None,"test2",'Test')
host_inter=self.insert_row(model,None,"test3",'Test')
host_inter=self.insert_row(model,None,"test4",'Test')
tree1.set_model(model)
renderer=gtk.CellRendererText()
column=gtk.TreeViewColumn("Col1",renderer,text=0)
column.set_resizable(True)
tree1.append_column(column)
renderer=gtk.CellRendererText()
column=gtk.TreeViewColumn("Col2",renderer,text=1)
column.set_resizable(True)
tree1.append_column(column)
def insert_row(self,model,parent,firstcolumn,secondcolumn,
thirdcolumn=None):
myiter=model.insert_after(parent,None)
model.set_value(myiter,0,firstcolumn)
model.set_value(myiter,1,secondcolumn)
if thirdcolumn !=None:
model.set_value(myiter,2,thirdcolumn)
return myiter
def callback3000(self,widget,event,tree1,windy):
import re
"""prevents the possibility of inputting wrong chars"""
## fixme: missing comma, and cut&paste
key = gtk.gdk.keyval_name (event.keyval)
ONLYDIGITS="([0-9.,z]|BackSpace|Left|Right|F1|period|Tab|Up|Down)"
if not re.match (ONLYDIGITS, key):
print "True"
return True
else:
if key == "z":
tree1.emit_stop_by_name("key_press_event") # This blocks
the signal from the key press
event = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
event.keyval = 65364 # This is the code for the down key
event.state = gtk.gdk.CONTROL_MASK
event.time = 0 # assign current time
print gtk.gdk.keyval_name(event.keyval)
tree1.emit('key_press_event', event)
tree1.connect("key_press_event",self.callback3001)
else:
print event.keyval
print "else"
return False
def callback3001(self,widget,event):
import re
"""prevents the possibility of inputting wrong chars"""
## fixme: missing comma, and cut&paste
key = gtk.gdk.keyval_name (event.keyval)
print key
print "made it"
ONLYDIGITS="([0-9.,z]|BackSpace|Left|Right|F1|period|Tab|Up|Down)"
if not re.match (ONLYDIGITS, key):
print "True2"
return True
def quit(self,obj):
gtk.main_quit()
sys.exit(1)
def quit100(self,obj,wind):
gtk.main_quit()
sys.exit(1)
if __name__ == '__main__':
Shopcard()
try:
gtk.threads_init()
except:
print "No threading was enabled when you compiled pyGTK!"
import sys
sys.exit(1)
gtk.threads_enter()
gtk.main()
gtk.threads_leave()
The code outputs the following:
When you hit the down cursor it prints:
65364
else
and when you hit the "z" key it prints:
Down
65364
else
Regards,
John.
-----Original Message-----
From: pygtk-bounces at daa.com.au [mailto:pygtk-bounces at daa.com.au] On
Behalf Of Fabian Braennstroem
Sent: 18 November 2006 19:14
To: pygtk at daa.com.au
Subject: [pygtk] Re: adjust key bindings
Hi,
sorry, is that question to stupid or does nobody has a hint?
* On 14 Nov 2006 * Fabian Braennstroem wrote:
> Hi,
>
> I just start to use pygtk ... so it is just my first
> question :-)
>
> I would like to create a small file manager based on 'lfm'
> (curses based file manager). I used glade for the gui and I
> am able to display the existing files and directories using
> two treeview widgets.
> Now, at the beginning I am kind of stuck with the key bindings. In
'lfm'
> it was pretty easy to define special key bindings:
>
>
> keytable = {
> # movement
> ord('p'): 'cursor_up',
> ord('k'): 'cursor_up',
> ord('K'): 'cursor_up2',
> ord('P'): 'cursor_up',
> curses.KEY_UP: 'cursor_up',
> ord('n'): 'cursor_down',
> ord('j'): 'cursor_down',
> ord('J'): 'cursor_down2',
> ord('N'): 'cursor_down',
> curses.KEY_DOWN: 'cursor_down',
> curses.KEY_PPAGE: 'page_previous',
> curses.KEY_BACKSPACE: 'page_previous',
> 0x08: 'page_previous', # BackSpace
> 0x10: 'page_previous', # Ctrl-P
> curses.KEY_NPAGE: 'page_next',
> ord(' '): 'page_next',
> 0x0E: 'page_next', # Ctrl-N
> curses.KEY_HOME: 'home',
> 0x16A: 'home',
> ord('H'): 'home',
> 0x001: 'home',
> curses.KEY_END: 'end',
> ord('G'): 'end',
> 0x181: 'end',
> 0x005: 'end',
> ord('h'): 'cursor_left',
> ord('l'): 'cursor_right',
> curses.KEY_LEFT: 'cursor_left',
> curses.KEY_RIGHT: 'cursor_right',
> ord('g'): 'goto_dir',
> 0x13: 'goto_file', # Ctrl-S
> 0x14: 'tree', # Ctrl-T
> ord('0'): 'bookmark_0',
> ord('1'): 'bookmark_1',
> ...
>
>
> with such a keytable I am able to bind different 'def's to
> every existing key. As you can see, I like it a lot to use
> 'vim-like' keys for moving around; 'j' and 'k' to move a row
> up and down. In glade I found those 'accelerators', but it
> is just for certain functions.
> Does anyone have an idea about using such a keybinding in
> pygtk? Would be nice!
>
Greetings!
Fabian
_______________________________________________
pygtk mailing list pygtk at daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
_______________________________________________
pygtk mailing list pygtk at daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
More information about the Tutor
mailing list