Source

VPlayer / shortcuts.py

# -*- coding: utf-8 -*-

import sys
import time
from threading import Thread

from Xlib import X, XK
from Xlib.display import Display
from Xlib.keysymdef import miscellany as _m
from Xlib.keysymdef import latin1 as _l

from PyQt4 import QtCore

from vplayer.logger import log

class Shortcuts(QtCore.QThread):

    def __init__(self):
        sets = QtCore.QSettings()

        play_st = [ v.toInt()[0] for v in sets.value("hotkeys/x/play", QtCore.QVariant()).toList() ] + ['play']
        stop_st = [ v.toInt()[0] for v in sets.value("hotkeys/x/stop", QtCore.QVariant()).toList() ] + ['stop']
        pause_st = [ v.toInt()[0] for v in sets.value("hotkeys/x/pause", QtCore.QVariant()).toList() ] + ['pause']
        prev_st = [ v.toInt()[0] for v in sets.value("hotkeys/x/prev", QtCore.QVariant()).toList() ] + ['previous']
        next_st = [ v.toInt()[0] for v in sets.value("hotkeys/x/next", QtCore.QVariant()).toList() ] + ['next']
        self.keys = [ play_st, stop_st, pause_st, prev_st, next_st ]
        self.disp = Display()
        self.root = self.disp.screen().root
        self.root.change_attributes(event_mask = X.PropertyChangeMask| X.KeyPressMask)
        QtCore.QThread.__init__(self)

    def bind_keys(self):
        self.root.change_attributes(event_mask = X.KeyPressMask)
        for key in self.keys:
            try:
                self.root.grab_key(key[0], key[1],
                                   True, X.GrabModeAsync, X.GrabModeAsync)
                self.root.grab_key(key[0], key[1] | 1<<4,
                                   True, X.GrabModeAsync, X.GrabModeAsync)
            except Exception as e:
                print dir(e)
                log.error('could not register hotkey ' + str(key))


    def run(self):
        self.active = True
        self.bind_keys()
        self.handle()

    def handle(self):
        while self.active:
            event = self.root.display.next_event()
            log.debug("Got event %s" % str(event))
            if event.type == X.KeyPress:
                keycode = event.detail
                for key in self.keys:
                    if key[0] == keycode:
                        self.emit(QtCore.SIGNAL("control"), key[2])

    def stop(self):
        self.active = False