key_input.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from . import interaction
  18. from .input_device import InputDevice
  19. from .interaction import (Interaction,
  20. Pause)
  21. class KeyInput(InputDevice):
  22. def __init__(self, name):
  23. super(KeyInput, self).__init__()
  24. self.name = name
  25. self.type = interaction.KEY
  26. def encode(self):
  27. return {"type": self.type, "id": self.name, "actions": [acts.encode() for acts in self.actions]}
  28. def create_key_down(self, key):
  29. self.add_action(TypingInteraction(self, "keyDown", key))
  30. def create_key_up(self, key):
  31. self.add_action(TypingInteraction(self, "keyUp", key))
  32. def create_pause(self, pause_duration=0):
  33. self.add_action(Pause(self, pause_duration))
  34. class TypingInteraction(Interaction):
  35. def __init__(self, source, type_, key):
  36. super(TypingInteraction, self).__init__(source)
  37. self.type = type_
  38. self.key = key
  39. def encode(self):
  40. return {"type": self.type, "value": self.key}