Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. from __future__ import print_function, unicode_literals
  2.  
  3. from ctypes import windll
  4.  
  5. from dragonfly import (ActionBase, Choice, Grammar, IntegerRef, Key,
  6. MappingRule, Mouse, Pause, Repeat)
  7. from dragonfly.actions.keyboard import Keyboard, KeySymbols
  8.  
  9. KEYBOARD = Keyboard()
  10.  
  11. GET_ASYNC_KEY_STATE = windll.user32.GetAsyncKeyState
  12.  
  13.  
  14. class ModifierAction(ActionBase):
  15. """
  16. Wrap arbitrary actions within keyboard presses.
  17. Currently Windows-only.
  18. """
  19.  
  20. def __init__(self, action, modifiers=None):
  21. """
  22. Set `Action` instance to execute and prime modifiers list.
  23.  
  24. `modifiers` must be a sequence of valid Windows keyboard codes.
  25. """
  26. super(ModifierAction, self).__init__()
  27. self.action = action
  28. if not (isinstance(modifiers, (list, tuple)) or modifiers is None):
  29. raise TypeError("`modifiers` must be a list, tuple, or `None`.")
  30. self._modifiers = tuple(modifiers) if modifiers else []
  31.  
  32. def _execute(self, data=None):
  33. modifiers = list(self._modifiers)
  34. for key, value in data.items():
  35. if "modifier_" in key:
  36. modifiers.append(value)
  37.  
  38. # Only activate modifier keys which aren't currently being held down,
  39. # the assumption being the keys are being held down for a reason.
  40. modifiers_down = [(modifier, True, .1)
  41. for modifier in modifiers
  42. if GET_ASYNC_KEY_STATE(modifier) & 0x8000 == 0]
  43. KEYBOARD.send_keyboard_events(modifiers_down)
  44.  
  45. self.action.execute(data)
  46.  
  47. # Similar to before, only release keys that are being held and
  48. # we are responsible for pressing.
  49. modifiers_up = [(modifier, False, .1)
  50. for (modifier, i, j) in modifiers_down
  51. if (GET_ASYNC_KEY_STATE(modifier) & 0x8000) != 0]
  52. KEYBOARD.send_keyboard_events(modifiers_up)
  53.  
  54.  
  55. GRAMMAR = Grammar("Modifier Action example")
  56.  
  57. SHIFT_KEY = KeySymbols.SHIFT
  58. SUPER_KEY = KeySymbols.LSUPER
  59. ALT_KEY = KeySymbols.ALT
  60. CONTROL_KEY = KeySymbols.CONTROL
  61. MODIFIERS = {
  62. "[left] alt": ALT_KEY,
  63. "[left] shift": SHIFT_KEY,
  64. "[left] control": CONTROL_KEY,
  65. "[left] (windows|super)": SUPER_KEY,
  66. }
  67.  
  68.  
  69. class ExampleRule(MappingRule):
  70.  
  71. mapping = {
  72. "[<modifier_1>][<modifier_2>][<modifier_3>] extended <fkey>":
  73. ModifierAction(Key("f%(fkey)d")),
  74. "after [this] tab [<n>]":
  75. ModifierAction(Key("tab/10:%(n)d"), modifiers=[CONTROL_KEY]),
  76. "before [this] tab [<n>]":
  77. ModifierAction(
  78. Key("tab/10:%(n)d"), modifiers=[CONTROL_KEY, SHIFT_KEY]),
  79. "[<modifier_1>][<modifier_2>][<modifier_3>] punch [<n>]":
  80. ModifierAction(
  81. (Mouse("left:down") + Pause("10") + Mouse("left:up") +
  82. Pause("10")) * Repeat(extra="n")),
  83. }
  84. extras = [
  85. IntegerRef("n", 1, 10),
  86. IntegerRef("fkey", 1, 25),
  87. Choice("modifier_1", MODIFIERS),
  88. Choice("modifier_2", MODIFIERS),
  89. Choice("modifier_3", MODIFIERS),
  90. ]
  91. defaults = {"n": 1}
  92.  
  93.  
  94. GRAMMAR.add_rule(ExampleRule())
  95. GRAMMAR.load()
  96.  
  97.  
  98. def unload():
  99. global GRAMMAR
  100. if GRAMMAR:
  101. GRAMMAR.unload()
  102. GRAMMAR = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement