platypush/platypush/backend/clipboard.py

41 lines
1 KiB
Python
Raw Normal View History

2020-05-23 23:11:42 +02:00
import time
from typing import Optional
import pyperclip
from platypush.backend import Backend
from platypush.message.event.clipboard import ClipboardEvent
class ClipboardBackend(Backend):
"""
This backend monitors for changes in the clipboard and generates even when the user copies a new text.
Requires:
- **pyperclip** (``pip install pyperclip``)
2020-05-23 23:12:39 +02:00
Triggers:
- :class:`platypush.message.event.clipboard.ClipboardEvent` on clipboard update.
2020-05-23 23:11:42 +02:00
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._last_text: Optional[str] = None
def run(self):
self.logger.info('Started clipboard monitor backend')
2020-05-23 23:11:42 +02:00
while not self.should_stop():
text = pyperclip.paste()
if text and text != self._last_text:
self.bus.post(ClipboardEvent(text=text))
self._last_text = text
time.sleep(0.1)
self.logger.info('Stopped clipboard monitor backend')
2020-05-23 23:11:42 +02:00
# vim:sw=4:ts=4:et: