Ejemplo n.º 1
0
    def __init__(self, keycode, modifiers, keysym):
        """Create an event instance.

        Arguments:

        keycode -- The keycode that identifies a physical key.

        modifiers -- An 8-bit mask. A set bit means the corresponding
        modifier is active. See Xlib.X.ShiftMask, Xlib.X.LockMask,
        Xlib.X.ControlMask, and Xlib.X.Mod1Mask through
        Xlib.X.Mod5Mask.

        keysym -- The symbol obtained when the key corresponding to
        keycode without any modifiers. The KeyboardEmulation class
        does not track modifiers such as Shift and Control.

        """
        self.keycode = keycode
        self.modifiers = modifiers
        self.keysym = keysym
        # Only want printable characters.
        if keysym < 255 or keysym in (XK.XK_Return, XK.XK_Tab):
            self.keystring = XK.keysym_to_string(keysym)
            if self.keystring == '\x00':
                self.keystring = None
        else:
            self.keystring = None
Ejemplo n.º 2
0
def wait(p_keys_list):
    """Block the whole keyboard!!! And wait until some key from p_keys_list
    is pressed. By now p_keys_list is a list of strings, so use single
    ascii symbols.
    There is a way out of this hell - hit 'Escape'.
    The function returns hit button`s string representation
    Eg. for p_keys_list == ['1','2','3'] the function will hand untill
    1,2 or 3 key is preseed or Escape is pressed."""
    ds = display
    window.grab_keyboard(1, X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)
    while True:
        ev = ds.next_event()
        if ev.type == X.KeyPress:
            keysym = ds.keycode_to_keysym(ev._data['detail'], 0)
            keystr = XK.keysym_to_string(keysym)
            print("Got keysym/keystr: "+str(keysym)+ ' / '+str(keystr))
            if keystr in p_keys_list:
                ds.ungrab_keyboard(X.CurrentTime)
                ds.flush()
                return keystr
            elif str(keysym) in p_keys_list:
                ds.ungrab_keyboard(X.CurrentTime)
                ds.flush()
                return keysym
            elif keysym == 65307:
                ds.ungrab_keyboard(X.CurrentTime)
                ds.flush()
                return 'Escape'
Ejemplo n.º 3
0
def main(options):
    # current display
    pid_file = "/var/lock/easyxmotion.pid"
    # kill any old versions that are still running,
    # we do it this way so the current one has input focus.
    # might be a better way to just exit and give focus to the old one.
    try:
        with open(pid_file, "r") as fp:
            pid = int(fp.read())
            try:
                os.kill(pid, signal.SIGTERM)
            except OSError:
                # other isn't running
                pass
    except IOError:
        # first ever run
        pass
    with open(pid_file, "w") as fp:
        fp.write(str(os.getpid()))

    osds, windows = display_osd(options)
    disp = Display()
    root = disp.screen().root
    root.change_attributes(event_mask=X.KeyPressMask)
    root.grab_keyboard(False, X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)

    event = disp.next_event()
    keycode = event.detail
    if event.type == X.KeyPress:
        key = XK.keysym_to_string(disp.keycode_to_keysym(keycode, 0))
        if key and key in string.lowercase and string.lowercase.index(key) < len(windows):
            windows[string.lowercase.index(key)].activate(timestamp)
        disp.ungrab_keyboard(X.CurrentTime)
        sys.exit()
Ejemplo n.º 4
0
def main(options):
    # current display
    pid_file = '/var/lock/easyxmotion.pid'
    # kill any old versions that are still running,
    # we do it this way so the current one has input focus.
    # might be a better way to just exit and give focus to the old one.
    try:
        with open(pid_file, 'r') as fp:
            pid = int(fp.read())
            try:
                os.kill(pid, signal.SIGTERM)
            except OSError:
                #other isn't running
                pass
    except IOError:
        # first ever run
        pass
    with open(pid_file, 'w') as fp:
        fp.write(str(os.getpid()))

    osds, windows = display_osd(options)
    disp = Display()
    root = disp.screen().root
    root.change_attributes(event_mask = X.KeyPressMask)
    root.grab_keyboard(False, X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)

    event = disp.next_event()
    keycode = event.detail
    if event.type == X.KeyPress:
        key = XK.keysym_to_string(disp.keycode_to_keysym(keycode, 0))
        if key and key in string.lowercase and string.lowercase.index(key) < len(windows):
            windows[string.lowercase.index(key)].activate(timestamp)
        disp.ungrab_keyboard(X.CurrentTime)
        sys.exit()
Ejemplo n.º 5
0
    def __init__(self, keycode, modifiers, keysym):
        """Create an event instance.

        Arguments:

        keycode -- The keycode that identifies a physical key.

        modifiers -- An 8-bit mask. A set bit means the corresponding
        modifier is active. See Xlib.X.ShiftMask, Xlib.X.LockMask,
        Xlib.X.ControlMask, and Xlib.X.Mod1Mask through
        Xlib.X.Mod5Mask.

        keysym -- The symbol obtained when the key corresponding to
        keycode without any modifiers. The KeyboardEmulation class
        does not track modifiers such as Shift and Control.

        """
        self.keycode = keycode
        self.modifiers = modifiers
        self.keysym = keysym
        # Only want printable characters.
        if keysym < 255 or keysym in (XK.XK_Return, XK.XK_Tab):
            self.keystring = XK.keysym_to_string(keysym)
            if self.keystring == '\x00':
                self.keystring = None
        else:
            self.keystring = None
Ejemplo n.º 6
0
    def handle_keypress_sym(self, k):
        name = XK.keysym_to_string(k)

        if k in self.modifier_map:
            self.modifier_map[k] = True
            return

        # in buffer mvmt
        if k == XK.XK_Right and self.is_ctrl():
            self.shift_cursor_end_word()
        elif k == XK.XK_Right:
            self.shift_cursor_right()
        elif k == XK.XK_Left and self.is_ctrl():
            self.shift_cursor_start_word()
        elif k == XK.XK_Left:
            self.shift_cursor_left()

        # del bk
        elif k == XK.XK_Delete:
            self.forward_delete()
        elif k == XK.XK_BackSpace:
            self.backward_delete()

        # printable char
        elif name is not None:
            if self.is_ctrl() and is_well_known_ctrl_hotkey(name):
                self.reset_buff()
            else:
                c = name.upper() if self.is_shift() else name
                self.type_char(c)
        else:
            print("other", k, name)
            self.reset_buff()
Ejemplo n.º 7
0
def wait(p_keys_list):
    """Block the whole keyboard!!! And wait until some key from p_keys_list
    is pressed. By now p_keys_list is a list of strings, so use single
    ascii symbols.
    There is a way out of this hell - hit 'Escape'.
    The function returns hit button`s string representation
    Eg. for p_keys_list == ['1','2','3'] the function will hand untill
    1,2 or 3 key is preseed or Escape is pressed."""
    ds = display
    window.grab_keyboard(1, X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)
    while True:
        ev = ds.next_event()
        if ev.type == X.KeyPress:
            keysym = ds.keycode_to_keysym(ev._data['detail'], 0)
            keystr = XK.keysym_to_string(keysym)
            print("Got keysym/keystr: " + str(keysym) + ' / ' + str(keystr))
            if keystr in p_keys_list:
                ds.ungrab_keyboard(X.CurrentTime)
                ds.flush()
                return keystr
            elif str(keysym) in p_keys_list:
                ds.ungrab_keyboard(X.CurrentTime)
                ds.flush()
                return keysym
            elif keysym == 65307:
                ds.ungrab_keyboard(X.CurrentTime)
                ds.flush()
                return 'Escape'
Ejemplo n.º 8
0
    def handler(self, reply):
        """ Self function is called when a xlib event is fired """
        data = reply.data
        while len(data):
            event, data = rq.EventField(None).parse_binary_value(data,
                                                                 self.disp.display,
                                                                 None, None)

            if event.type == X.KeyPress:
                if event.detail == 36:
                    self.record("enter")
                elif event.detail == 22:
                    self.record("backspace")
                elif event.detail == 37:
                    self.record("control")
                elif event.detail == 64:
                    self.record("alt")
                elif event.detail == 108:
                    self.record("alt-gr")
                        
                key = XK.keysym_to_string(
                    self.disp.keycode_to_keysym(event.detail, event.state))

                if key:
                    self.record(key)
Ejemplo n.º 9
0
def interpret_keysym(keysym):
    '''Convert keysym to keyname'''
    if keysym in KEYSYM_TABLE:
        # Special keys
        return KEYSYM_TABLE[keysym]
    else:
        # Other keys (if not found, `None` will be returned)
        return XK.keysym_to_string(keysym)
Ejemplo n.º 10
0
 def OnKeyPress(self, event):
     if event.Key == "space":
         self.completer.complete_whitespace()
     elif event.Key == "BackSpace":
         self.completer.complete_backspace()
     else:
         try:
             key_number = getattr(XK, "XK_" + event.Key)
             key = XK.keysym_to_string(key_number)
             if key is not None:
                 self.completer.complete_letter(key)
         except AttributeError:
             pass
Ejemplo n.º 11
0
def event_to_string(self, event):
    mods = []
    if event.state & X.ShiftMask:
        mods.append('Shift')

    if event.state & X.ControlMask:
        mods.append('Control')

    keycode = event.detail
    keysym = self.disp.keycode_to_keysym(keycode, 0)
    char = XK.keysym_to_string(keysym)

    return ''.join(mod + '+' for mod in mods) + (char if char else '?')
Ejemplo n.º 12
0
 def keycode_to_string(self, key_code):
     codes_mod=[50,62,64,108,37,105,113,111,114,116,110,115,112,117,118,119,107,
                67,68,69,70,71,72,73,74,75,76,95,96
               ]
     names_mod=['Shift_L', 'Shift_R', 'Alt_L', 'Alt_R', 'Control_L', 'Control_R',
                'Left', 'Up', 'Right', 'Down', 'Home', 'End', 'Page_Up', 'Page_Down',
                'Insert', 'Delete', 'Print',
                'F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12'
               ]
     if key_code in codes_mod:
        return names_mod[codes_mod.index(key_code)]
     #2nd: 0 is unshifted, 1 is shifted, 2 is alt grid, and 3 is shiftalt grid
     return XK.keysym_to_string(self.disp.keycode_to_keysym(key_code, 0))        
Ejemplo n.º 13
0
 def listen(self):
     self.grab()
     while True:
         evt = self.disp.next_event()
         if evt.type in [X.KeyPress, X.KeyRelease]:
             keycode = evt.detail
             keysym = self.disp.keycode_to_keysym(keycode, 0)
             char = XK.keysym_to_string(keysym)
             self.disp.allow_events(X.ReplayKeyboard, X.CurrentTime)
             self.mode(self, evt, char)
         if evt.type == X.DestroyNotify:
             if evt.window.id == self.id:
                 self.ungrab()
                 return
Ejemplo n.º 14
0
def keycode_to_string(key_code):
    syms_mod=[65505,65506,65513,65514,65507,65508,
              65361,65362,65363,65364,65360,65367,65365,65366,
              65379,65535,65491,
              65470,65471,65472,65473,65474,65475,65476,65477,65478,65479,65480,65481
             ]
    names_mod=['Shift_L', 'Shift_R', 'Alt_L', 'Alt_R', 'Control_L', 'Control_R',
               'Left', 'Up', 'Right', 'Down', 'Home', 'End', 'Page_Up', 'Page_Down',
               'Insert', 'Delete', 'Print',
               'F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12'
               '\r'
              ]
    #2nd: 0 is unshifted, 1 is shifted, 2 is alt grid, and 3 is shiftalt grid
    key_sym=disp.keycode_to_keysym(key_code, 0)
    if key_sym in syms_mod:
       return names_mod[syms_mod.index(key_sym)]
    return XK.keysym_to_string(key_sym)
Ejemplo n.º 15
0
    def handler(self, reply):
        """ Self function is called when a xlib event is fired """
        data = reply.data
        while len(data):
            event, data = rq.EventField(None).parse_binary_value(
                data, self.disp.display, None, None)

            if event.type == X.KeyPress:
                if event.detail == 36:
                    self.record("enter")
                elif event.detail == 22:
                    self.record("backspace")
                elif event.detail == 37:
                    self.record("control")
                elif event.detail == 64:
                    self.record("alt")
                elif event.detail == 108:
                    self.record("alt-gr")

                key = XK.keysym_to_string(
                    self.disp.keycode_to_keysym(event.detail, event.state))

                if key:
                    self.record(key)
Ejemplo n.º 16
0
def get_keysym(key_code):
    key_sym = disp.keycode_to_keysym(key_code, 0)
    return XK.keysym_to_string(key_sym)
Ejemplo n.º 17
0
 def code_to_char(cls, code, modifiers=0):
     if cls.device is "button":
         return "<%s>" % cls.key_to_name(code)
     if not cls.display:
         cls.display = Display()
     return XK.keysym_to_string(cls.display.keycode_to_keysym(code + 8, modifiers))
Ejemplo n.º 18
0
    w.draw_message("uniclick: scanning screen...")  # undraw
    w.draw(word_boxes, None)
    w.display.sync()

    filtered_boxes = word_boxes.copy()
    selection = None
    search_term = ""
    found = False
    while not found:
        e = w.display.next_event()

        if e.type == X.KeyRelease:
            w.draw(filtered_boxes, selection)  # undraw current state

            keysym = w.display.keycode_to_keysym(e.detail, 0)
            string = XK.keysym_to_string(keysym)

            if keysym == XK.XK_BackSpace and len(search_term) >= 1:
                search_term = search_term[0:-1]

            elif keysym == XK.XK_Escape:
                raise SystemExit

            elif string is not None and string in ALPHABET:
                search_term += string

            elif selection is not None and keysym == XK.XK_Return:
                found = True

            elif keysym == XK.XK_Tab:
                if selection is not None:
Ejemplo n.º 19
0
 def keycode_to_string(self, detail):
     return XK.keysym_to_string(self.dpy.keycode_to_keysym(detail, 0))