my script from pynput.keyboard import Key, Controller import time import re import sys…
- User
my script
from pynput.keyboard import Key, Controller import time import re import sys import pygetwindow as gw import psutil import pyperclip import win32gui import win32process
def get_numbers(): """ Collect numbers from user input, allowing multiple lines of input. Users can enter numbers separated by semicolons, commas, or newlines. Press Enter on an empty line to finish input. """ print( "\nEnter the numbers to paste in Geo Zeb (separated by semicolons, commas, or newlines):\n" "(Press Enter on an empty line to finish input)" ) numbers = [] while True: try: line = input() if not line.strip(): break # Split the line on semicolons, commas, and strip whitespace split_numbers = re.split(r'[;,]+', line) # Strip each number and filter out empty strings split_numbers = [num.strip() for num in split_numbers if num.strip()] # Validate each number valid_numbers = [num for num in split_numbers if validate_number(num)] invalid_numbers = set(split_numbers) - set(valid_numbers) if invalid_numbers: print(f"Invalid entries skipped: {', '.join(invalid_numbers)}") numbers.extend(valid_numbers) except KeyboardInterrupt: print("\nInput interrupted by user.") break except Exception as e: print(f"An error occurred during input: {e}") break print(f"\nTotal valid numbers collected: {len(numbers)}") return numbers
def validate_number(number): """ Validate if the input string is a valid number (integer or float). """ try: float(number) return True except ValueError: return False
def press_and_release(key, pause=0.01): """ Press and release a key with an optional pause. """ try: keyboard.press(key) keyboard.release(key) time.sleep(pause) except Exception as e: print(f"Error pressing key {key}: {e}")
def press_keys(keys, pause=0.01): """ Press and release multiple keys simultaneously. """ try: for key in keys: keyboard.press(key) time.sleep(pause) for key in reversed(keys): keyboard.release(key) time.sleep(pause) except Exception as e: print(f"Error pressing keys {keys}: {e}")
def ensure_target_focused(window_title, executable_name, timeout=10): """ Ensure that the target application window is focused.
:param window_title: The title of the target application window. :param executable_name: The executable name of the target application. :param timeout: Time in seconds to wait for the window to appear and be focused. :return: True if the window is focused, False otherwise. """ start_time = time.time() while time.time() - start_time < timeout: try: windows = gw.getWindowsWithTitle(window_title) if not windows: print(f"Waiting for '{window_title}' window to appear...") time.sleep(0.5) continue window = windows[0] hwnd = window._hWnd # Window handle if not hwnd: print("Could not retrieve the window handle.") time.sleep(0.5) continue # Get process ID associated with the window try: _, pid = win32process.GetWindowThreadProcessId(hwnd) process = psutil.Process(pid) exe_name = process.name() except psutil.NoSuchProcess: print("No process found with the retrieved process ID.") time.sleep(0.5) continue except Exception as e: print(f"Error retrieving process information: {e}") time.sleep(0.5) continue # Verify executable name if exe_name.lower() != executable_name.lower(): print(f"Found window titled '{window_title}' but executable is '{exe_name}'. Expected '{executable_name}'.") time.sleep(0.5) continue # Bring the window to the front window.activate() print(f"Focusing window: '{window_title}'") time.sleep(0.5) # Wait for the window to come to focus return True except Exception as e: print(f"Error focusing window: {e}") time.sleep(0.5) print(f"Failed to focus the target application '{window_title}' within {timeout} seconds.") return False - Assistant