You are not logged in.

#1 2008-07-13 08:29:05

Abelian
Member
Registered: 2008-04-23
Posts: 63

python shortcuts

At the moment I have this code in my gtk app:

    def key_press_event(self, widget, event):
        """ key press event dispatcher """

        bindings = {
            gtk.keysyms.Page_Up: self.prev_buffer,
            gtk.keysyms.Page_Down: self.next_buffer,
            gtk.keysyms.h: self.show_help,
            gtk.keysyms.H: self.show_help,
            gtk.keysyms.i: self.show_info,
            gtk.keysyms.I: self.show_info,
            gtk.keysyms.n: self.new_buffer,
            gtk.keysyms.N: self.new_buffer,
            gtk.keysyms.o: self.open_file,
            gtk.keysyms.O: self.open_file,
            gtk.keysyms.p: self.preferences.show,
            gtk.keysyms.P: self.preferences.show,
            gtk.keysyms.q: self.dialog_quit,
            gtk.keysyms.Q: self.dialog_quit,
            gtk.keysyms.s: self.save_file,
            gtk.keysyms.S: self.save_file,
            gtk.keysyms.w: self.close_dialog,
            gtk.keysyms.W: self.close_dialog,
            gtk.keysyms.y: self.redo,
            gtk.keysyms.Y: self.redo,
            gtk.keysyms.z: self.undo,
            gtk.keysyms.Z: self.undo}
        if event.state & gtk.gdk.CONTROL_MASK:

            # Special case for Control-Shift-s

            if event.state & gtk.gdk.SHIFT_MASK:
                print event.keyval
            if event.state & gtk.gdk.SHIFT_MASK and event.keyval\
                 == gtk.keysyms.S:
                self.save_file_as()
                return True
            if bindings.has_key(event.keyval):
                bindings[event.keyval]()
                return True
        return False

That allows me t have those keyboard shortcuts in a gtksourceview - not very elegant I know but it works.

The issue I have now is that other keybaord layouts (chinese for example) may not have a concept of "h" in which case none of those shortcuts  work. I could use keycodes, but then there will be no way to guarentee what the shortcuts will be since the user will have to look up what keycodes are to find out what the shortcuts are and I would rather have it less featured than not know how it will work in some cases.

I'm almost hoping that there is a way of doing gtk.keysym._(h) and allowing it to be translated - but I dont know.

Any help much appreciated.

Offline

Board footer

Powered by FluxBB