5

I want to make keyboard shortcuts like t, that would work, when the main window is closed (but process is running, as the programme has a unity appindicator). I saw a package keybinder, but it seems, one can't use it with Gtk3 and pygobject. Or can? Then how? If not, is there any other way to do that? The application is for linux (ubuntu), I use python 2.7.

Phlya
  • 5,726
  • 4
  • 35
  • 54
  • added note, it works fine for 2.7 too, i just don't want to clutter up my disk. i keep 2.7 pretty pristine and do everything worth doing on py3k. – RobotHumans Feb 20 '14 at 00:54

2 Answers2

5

Keybinder works fine with python3, Gtk3, and pygi. There just wasn't a working example in the source tree.

#!/usr/bin/env python3
"""
example-gi-py3.py

Looked at a pull request that was built for py2.x, but
overwrote the original py instead of making a separate example.
I wouldn't have accepted that pull request either.

The keybinder.init() part wasn't in the original example.

aking1012.com@gmail.com

public domain
"""

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Keybinder', '3.0')

from gi.repository import Gtk
from gi.repository import Keybinder

def callback(keystr, user_data):
    print ("Handling", user_data)
    print ("Event time:", Keybinder.get_current_event_time())
    Gtk.main_quit()

if __name__ == '__main__':
    keystr = "<Ctrl><Alt>M"
    Keybinder.init()
    Keybinder.bind(keystr, callback, "keystring %s (user data)" % keystr)
    print ("Press", keystr, "to handle keybinding and quit")
    Gtk.main()  

Notes: Not thoroughly tested, but as a simple example it seems to work.

RobotHumans
  • 807
  • 10
  • 25
  • Added note, I put in a pull request over on github with this example that didn't walk on the original example. Maybe that one will get pulled in. – RobotHumans Feb 19 '14 at 10:44
0

I use also Keybinder to activate a search entry field in a Gtk3 app:

from gi.repository import Keybinder
…
  class MyApp:
    …   
        Keybinder.init()
        Keybinder.bind("<Ctrl>F", self.set_search_entry_focus)
…
    def set_search_entry_focus(self, keystring):
        self.search_entry.grab_focus()

http://lazka.github.io/pgi-docs/Keybinder-3.0/

But be aware, this will also steal focus if if you are using another app and your app is running in the background.

oxidworks
  • 1,563
  • 1
  • 14
  • 37