0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pyperclip でクリップボードの監視機能がなくなった

Posted at

pyperclip でクリップボードの監視機能 waitForPaste, waitForNewPaste がなくなったことに気付きました。自前でポーリングするようにとのことです。

自前実装

上記 Issue のコードに少し手を加えました。

import pyperclip, time

def waitForNewPaste(timeout=0):
    current = pyperclip.paste()
    start = time.monotonic()
    while True:
        time.sleep(0.1)
        text = pyperclip.paste()
        if text != current:
            return text
        if timeout > 0 and time.monotonic() - start > timeout:
            raise pyperclip.PyperclipTimeoutException(
                f"waitForPaste() timed out after {timeout} seconds.")

クリップボードの中身が変化するのを待ちます。time.monotonic() は OS での時刻の再設定に影響を受けない経過時間を返します。

同じ内容をコピーしてもスルーされます。これはやや不自然に感じますが、実装が簡単で、過去の実装もこういう挙動でした。

参考

監視機能が存在していた頃の説明です。

pyperclip.waitForPaste()を実行するとクリップボードの中身が空の場合は待機状態になり、新しいテキストがコピーされるとそれを返す。

pyperclip.waitForNewPaste()を実行すると待機状態になり、新しいテキストがコピーされる(= クリップボードのテキストが変更される)とそれを返す。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?