Beispiel #1
0
    def send_string(self, s):
        """

        Args:
            s: The string to emulate.

        We can send keys by keycodes or by SetUnicodeString.
        Setting the string is less ideal, but necessary for things like emoji.
        We want to try to group modifier presses, where convenient.
        So, a string like 'THIS dog [dog emoji]' might be processed like:
            'Raw: Shift down t h i s shift up,
            Raw: space d o g space,
            String: [dog emoji]'
        There are 3 groups, the shifted group, the spaces and dog string,
        and the emoji.
        """
        # Key plan will store the type of output
        # (raw keycodes versus setting string)
        # and the list of keycodes or the goal character.
        key_plan = []

        # apply_raw's properties are used to store
        # the current keycode sequence,
        # and add the sequence to the key plan when called as a function.
        def apply_raw():
            if hasattr(apply_raw, 'sequence') \
                    and len(apply_raw.sequence) is not 0:
                apply_raw.sequence.extend(apply_raw.release_modifiers)
                key_plan.append((self.RAW_PRESS, apply_raw.sequence))
            apply_raw.sequence = []
            apply_raw.release_modifiers = []
        apply_raw()

        last_modifier = None
        for c in characters(s):
            for keycode, modifier in self._layout.char_to_key_sequence(c):
                if keycode is not None:
                    if modifier is not last_modifier:
                        # Flush on modifier change.
                        apply_raw()
                        last_modifier = modifier
                        modifier_keycodes = self._modifier_to_keycodes(
                            modifier)
                        apply_raw.sequence.extend(down(modifier_keycodes))
                        apply_raw.release_modifiers.extend(
                            up(modifier_keycodes))
                    apply_raw.sequence.extend(down_up([keycode]))
                else:
                    # Flush on type change.
                    apply_raw()
                    key_plan.append((self.STRING_PRESS, c))
        # Flush after string is complete.
        apply_raw()

        # We have a key plan for the whole string, grouping modifiers.
        for press_type, sequence in key_plan:
            if press_type is self.STRING_PRESS:
                self._send_string_press(sequence)
            elif press_type is self.RAW_PRESS:
                self._send_sequence(sequence)
Beispiel #2
0
 def send_string(self, s):
     self._refresh_keyboard_layout()
     for char in characters(s):
         if char in self.keyboard_layout.char_to_vk_ss:
             # We know how to simulate the character.
             self._key_press(char)
         else:
             # Otherwise, we send it as a Unicode string.
             self._key_unicode(char)
Beispiel #3
0
 def send_string(self, s):
     self._refresh_keyboard_layout()
     for char in characters(s):
         if char in self.keyboard_layout.char_to_vk_ss:
             # We know how to simulate the character.
             self._key_press(char)
         else:
             # Otherwise, we send it as a Unicode string.
             self._key_unicode(char)
Beispiel #4
0
 def test_characters(self):
     self.assertEqual(list(misc.characters('STKPWHR')),
                      ['S', 'T', 'K', 'P', 'W', 'H', 'R'])
     self.assertEqual(list(misc.characters('')), [])
     self.assertEqual(list(misc.characters(u'✂⌨')), [u'✂', u'⌨'])
Beispiel #5
0
 def test_characters(self):
     self.assertEqual(list(misc.characters('STKPWHR')),
                      ['S', 'T', 'K', 'P', 'W', 'H', 'R'])
     self.assertEqual(list(misc.characters('')), [])
     self.assertEqual(list(misc.characters(u'✂⌨')), [u'✂', u'⌨'])