Exemplo n.º 1
0
   def _pressModifiers(self, modifiers, pressed=True):
      ''' Press given modifiers (provided in list form)

          Parameters: modifiers list
          Optional:  keypressed state (default is True (down))
          Returns: Unsigned int representing flags to set
      '''
      if (not isinstance(modifiers, list)):
         raise TypeError('Please provide modifiers in list form')

      if (not hasattr(self, 'keyboard')):
         self.keyboard = AXKeyboard.loadKeyboard()

      modFlags = 0

      # To direct output to the correct application need the PSN:
      appPsn = self._getPsnForPid(self._getPid())

      # Press given modifiers
      for nextMod in modifiers:
         if (nextMod not in self.keyboard):
            errStr = 'Key %s not found in keyboard layout'
            self._clearEventQueue()
            raise ValueError(errStr % self.keyboar[nextMod])
         modEvent = Quartz.CGEventCreateKeyboardEvent(None,
                                                      self.keyboard[nextMod],
                                                      pressed)
         if (not pressed):
            # Clear the modflags:
            Quartz.CGEventSetFlags(modEvent, 0)
         self._queueEvent(Quartz.CGEventPostToPSN, (appPsn, modEvent))
         # Add the modifier flags
         modFlags += AXKeyboard.modKeyFlagConstants[nextMod]

      return modFlags
Exemplo n.º 2
0
    def _pressModifiers(self, modifiers, pressed=True):
        ''' Press given modifiers (provided in list form)

          Parameters: modifiers list
          Optional:  keypressed state (default is True (down))
          Returns: Unsigned int representing flags to set
      '''
        if (not isinstance(modifiers, list)):
            raise TypeError('Please provide modifiers in list form')

        if (not hasattr(self, 'keyboard')):
            self.keyboard = AXKeyboard.loadKeyboard()

        modFlags = 0

        # To direct output to the correct application need the PSN:
        appPsn = self._getPsnForPid(self._getPid())

        # Press given modifiers
        for nextMod in modifiers:
            if (nextMod not in self.keyboard):
                errStr = 'Key %s not found in keyboard layout'
                self._clearEventQueue()
                raise ValueError(errStr % self.keyboar[nextMod])
            modEvent = Quartz.CGEventCreateKeyboardEvent(
                None, self.keyboard[nextMod], pressed)
            if (not pressed):
                # Clear the modflags:
                Quartz.CGEventSetFlags(modEvent, 0)
            self._queueEvent(Quartz.CGEventPostToPSN, (appPsn, modEvent))
            # Add the modifier flags
            modFlags += AXKeyboard.modKeyFlagConstants[nextMod]

        return modFlags
Exemplo n.º 3
0
    def _pressModifiers(self, modifiers, pressed=True, globally=False):
        """ Press given modifiers (provided in list form)

          Parameters: modifiers list, global or app specific
          Optional:  keypressed state (default is True (down))
          Returns: Unsigned int representing flags to set
      """
        if not isinstance(modifiers, list):
            raise TypeError("Please provide modifiers in list form")

        if not hasattr(self, "keyboard"):
            self.keyboard = AXKeyboard.loadKeyboard()

        modFlags = 0

        # Press given modifiers
        for nextMod in modifiers:
            if nextMod not in self.keyboard:
                errStr = "Key %s not found in keyboard layout"
                self._clearEventQueue()
                raise ValueError(errStr % self.keyboard[nextMod])
            modEvent = Quartz.CGEventCreateKeyboardEvent(Quartz.CGEventSourceCreate(0), self.keyboard[nextMod], pressed)
            if not pressed:
                # Clear the modflags:
                Quartz.CGEventSetFlags(modEvent, 0)
            if globally:
                self._queueEvent(Quartz.CGEventPost, (0, modEvent))
            else:
                # To direct output to the correct application need the PSN:
                appPsn = self._getPsnForPid(self._getPid())
                self._queueEvent(Quartz.CGEventPostToPSN, (appPsn, modEvent))
            # Add the modifier flags
            modFlags += AXKeyboard.modKeyFlagConstants[nextMod]

        return modFlags
Exemplo n.º 4
0
   def _addKeyToQueue(self, keychr, modFlags=0, globally=False):
      ''' Add keypress to queue

          Parameters: key character or constant referring to a non-alpha-numeric
                      key (e.g. RETURN or TAB)
                      modifiers
                      global or app specific
          Returns: None or raise ValueError exception
      '''
      # Awkward, but makes modifier-key-only combinations possible
      # (since sendKeyWithModifiers() calls this)
      if (not keychr):
         return

      if (not hasattr(self, 'keyboard')):
         self.keyboard = AXKeyboard.loadKeyboard()

      modifiers = None

      if (keychr in self.keyboard['upperSymbols']):
	 modifiers = [AXKeyCodeConstants.SHIFT];
	 modFlags = self._pressModifiers(modifiers)

      if (keychr.isupper()):
	 keychr = keychr.lower()
	 modifiers = [AXKeyCodeConstants.SHIFT];
	 modFlags = self._pressModifiers(modifiers)

      if (keychr not in self.keyboard):
          self._clearEventQueue()
          raise ValueError('Key %s not found in keyboard layout' % keychr)

      # Press the key
      keyDown = Quartz.CGEventCreateKeyboardEvent(None,
                                                  self.keyboard[keychr],
                                                  True)
      # Release the key
      keyUp = Quartz.CGEventCreateKeyboardEvent(None,
                                                self.keyboard[keychr],
                                                False)
      # Set modflags on keyDown (default None):
      Quartz.CGEventSetFlags(keyDown, modFlags)
      # Set modflags on keyUp:
      Quartz.CGEventSetFlags(keyUp, modFlags)

      # Post the event to the given app
      if not globally:
          # To direct output to the correct application need the PSN:
          appPsn = self._getPsnForPid(self._getPid())
          self._queueEvent(Quartz.CGEventPostToPSN, (appPsn, keyDown))
          self._queueEvent(Quartz.CGEventPostToPSN, (appPsn, keyUp))
      else:
          self._queueEvent(Quartz.CGEventPost, (0, keyDown))
          self._queueEvent(Quartz.CGEventPost, (0, keyUp))

      if modifiers != None:
         self._releaseModifiers(modifiers)
Exemplo n.º 5
0
   def _addKeyToQueue(self, keychr, modFlags=0, globally=False):
      ''' Add keypress to queue

          Parameters: key character or constant referring to a non-alpha-numeric
                      key (e.g. RETURN or TAB)
                      modifiers
                      global or app specific
          Returns: None or raise ValueError exception
      '''
      # Awkward, but makes modifier-key-only combinations possible
      # (since sendKeyWithModifiers() calls this)
      if (not keychr):
         return

      if (not hasattr(self, 'keyboard')):
         self.keyboard = AXKeyboard.loadKeyboard()

      if (keychr in self.keyboard['upperSymbols'] and not modFlags):
         self._sendKeyWithModifiers(keychr,
                                    [AXKeyCodeConstants.SHIFT],
                                    globally)
         return

      if (keychr.isupper() and not modFlags):
         self._sendKeyWithModifiers(keychr.lower(),
                                    [AXKeyCodeConstants.SHIFT],
                                    globally)
         return

      if (keychr not in self.keyboard):
          self._clearEventQueue()
          raise ValueError('Key %s not found in keyboard layout' % keychr)

      # Press the key
      keyDown = Quartz.CGEventCreateKeyboardEvent(None,
                                                  self.keyboard[keychr],
                                                  True)
      # Release the key
      keyUp = Quartz.CGEventCreateKeyboardEvent(None,
                                                self.keyboard[keychr],
                                                False)
      # Set modflags on keyDown (default None):
      Quartz.CGEventSetFlags(keyDown, modFlags)
      # Set modflags on keyUp:
      Quartz.CGEventSetFlags(keyUp, modFlags)

      # Post the event to the given app
      if not globally:
          # To direct output to the correct application need the PSN:
          appPsn = self._getPsnForPid(self._getPid())
          self._queueEvent(Quartz.CGEventPostToPSN, (appPsn, keyDown))
          self._queueEvent(Quartz.CGEventPostToPSN, (appPsn, keyUp))
      else:
          self._queueEvent(Quartz.CGEventPost, (0, keyDown))
          self._queueEvent(Quartz.CGEventPost, (0, keyUp))
Exemplo n.º 6
0
    def _sendKeyWithModifiers(self, keychr, modifiers):
        ''' Send one character with the given modifiers pressed

          Parameters: key character, list of modifiers
          Returns: None or raise ValueError exception
      '''
        if (len(keychr) > 1):
            raise ValueError('Please provide only one character to send')

        if (not hasattr(self, 'keyboardLayout')):
            self.keyboard = AXKeyboard.loadKeyboard()

        modFlags = self._pressModifiers(modifiers)

        # Press the non-modifier key
        self._sendKey(keychr, modFlags)

        # Release the modifiers
        self._releaseModifiers(modifiers)

        # Post the queued keypresses:
        self._postQueuedEvents()
Exemplo n.º 7
0
   def _sendKeyWithModifiers(self, keychr, modifiers, globally=False):
      ''' Send one character with the given modifiers pressed

          Parameters: key character, list of modifiers, global or app specific
          Returns: None or raise ValueError exception
      '''
      if (len(keychr) > 1):
         raise ValueError('Please provide only one character to send')

      if (not hasattr(self, 'keyboard')):
         self.keyboard = AXKeyboard.loadKeyboard()

      modFlags = self._pressModifiers(modifiers, globally=globally)

      # Press the non-modifier key
      self._sendKey(keychr, modFlags, globally=globally)

      # Release the modifiers
      self._releaseModifiers(modifiers, globally=globally)

      # Post the queued keypresses:
      self._postQueuedEvents()
Exemplo n.º 8
0
   def _sendKeyWithModifiers(self, keychr, modifiers, globally=False):
      ''' Send one character with the given modifiers pressed

          Parameters: key character, list of modifiers, global or app specific
          Returns: None or raise ValueError exception
      '''
      if (not self._isSingleCharacter(keychr)):
         raise ValueError('Please provide only one character to send')

      if (not hasattr(self, 'keyboard')):
         self.keyboard = AXKeyboard.loadKeyboard()

      modFlags = self._pressModifiers(modifiers, globally=globally)

      # Press the non-modifier key
      self._sendKey(keychr, modFlags, globally=globally)

      # Release the modifiers
      self._releaseModifiers(modifiers, globally=globally)

      # Post the queued keypresses:
      self._postQueuedEvents()