moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

PyQt file drag and drop causes mouse cursor lag while pynput listener is running #390

Open adamerose opened 3 years ago

adamerose commented 3 years ago

Description When I use drag-and-drop to copy files into Windows file explorer with PyQt5, if there is a pynput listener running then the mouse lags badly for a few seconds after each drop.

This also happens with the mouse package

Video Demo: https://www.screencast.com/t/ugACiPkOY6

Platform and pynput version pynput 1.7.3 PyQt5 5.15.4 Windows 10 (19042.985)

To Reproduce

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt

import tempfile
import os

from pynput import mouse

# Starting this listener causes mouse lag on drop, removing these two lines removes the lag
listener = mouse.Listener()
listener.start()

# Demo allowing drag and drop of tree items into system file explorer
class CustomTreeWidget(QtWidgets.QTreeWidget):
    def __init__(self):
        super().__init__()

        self.setHeaderLabels(["Name"])
        QtWidgets.QTreeWidgetItem(self, ['Test1'])
        QtWidgets.QTreeWidgetItem(self, ['Test2'])
        QtWidgets.QTreeWidgetItem(self, ['Test3'])

        self.setDragEnabled(True)

    def startDrag(self, actions):
        drag = QtGui.QDrag(self)
        names = [item.text(0) for item in self.selectedItems()]
        mime = QtCore.QMimeData()
        path_list = []
        for name in names:
            path = os.path.join(tempfile.gettempdir(), 'pyqt_drag_temp_files', name + '.txt')
            os.makedirs(os.path.dirname(path), exist_ok=True)

            with open(path, 'w+') as f:
                f.write(f"Contents of {name}")

            path_list.append(QtCore.QUrl.fromLocalFile(path))

        mime.setUrls(path_list)
        mime.setData('application/x-qabstractitemmodeldatalist',
                     self.mimeData(self.selectedItems()).data('application/x-qabstractitemmodeldatalist'))
        drag.setMimeData(mime)
        drag.exec_(Qt.MoveAction)
        super().startDrag(actions)

app = QtWidgets.QApplication([])

nav = CustomTreeWidget()
nav.show()
app.exec_()
BruceLee569 commented 3 years ago

I tried to put pynput in a new process executed through Queue, and lag miss.