Re: [Tutor] adjust key bindings
Luke, Thanks for the response. I am trying to help someone out on the Pygtk list. I forgot to enclose his reasons for wanting to assign the key press. They are below: It looks like he likes his keys set out in a certain way. I am happy with mine the way they are! I used z in isolation just to get the logic right. I know I can assign a function to Z and make the Treeview move down one row and select the row below but I don't want to have to do this for all the different buttons. I am trying to do it without having to assign and call a whole load of different functions. 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! I have attempted to answer his question but I am not sure I am on the right track. Is there a better way to do it? Regards, John. -Original Message- From: Luke Paireepinart [mailto:[EMAIL PROTECTED] Sent: 27 November 2006 01:30 To: [EMAIL PROTECTED] Cc: tutor@python.org Subject: Re: [Tutor] adjust key bindings John CORRY wrote: 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. I highly doubt that what you want to do when someone hits a 'z' is to generate a 'down' keypress. What if the user assigned the down key to z? then you'd have a z - down - z - down - infinite loop. What I expect you want is that each key, z and down, perform the same action. In other words, they both call the same function. So basically what you'd want is something like this (non-pyGTK specific code) def aFunc(): print Hello, World! bindKey = {'down':aFunc} keypress = raw_input(What keypress do you want to perform?) bindKey[keypress]()#this will call the 'aFunc' function if they type 'down', otherwise, it'll probably crash. bindKey['z'] = aFunc bindKey['z']()# now performs the same function as bindkey['down']()#this does. If you really do want to generate 'down' keypresses when someone hits 'z', please explain why, and I will try to the best of my abilities to help you in that regard! Good Luck! -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] adjust key bindings
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of John CORRY Sent: 25 November 2006 17:23 To: 'Fabian Braennstroem'; pygtk@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 cutpaste 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 cutpaste 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Fabian Braennstroem Sent: 18 November 2006 19:14 To: pygtk@daa.com.au Subject: [pygtk] Re: adjust key bindings Hi,
Re: [Tutor] adjust key bindings
John CORRY wrote: 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. I highly doubt that what you want to do when someone hits a 'z' is to generate a 'down' keypress. What if the user assigned the down key to z? then you'd have a z - down - z - down - infinite loop. What I expect you want is that each key, z and down, perform the same action. In other words, they both call the same function. So basically what you'd want is something like this (non-pyGTK specific code) def aFunc(): print Hello, World! bindKey = {'down':aFunc} keypress = raw_input(What keypress do you want to perform?) bindKey[keypress]()#this will call the 'aFunc' function if they type 'down', otherwise, it'll probably crash. bindKey['z'] = aFunc bindKey['z']()# now performs the same function as bindkey['down']()#this does. If you really do want to generate 'down' keypresses when someone hits 'z', please explain why, and I will try to the best of my abilities to help you in that regard! Good Luck! -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor