2023-12-27 Python PC获取鼠标位置,移动鼠标到相应的位置 定时自动模拟鼠标点击,用于简单测试app用

一、核心源码如下:

复制代码
import pyautogui
import time

pyautogui.moveTo(600, 800)
for i in range(20):
    time.sleep(0.1)
    x, y = pyautogui.position()
    print("mouse position:", x, y)
    pyautogui.click()

二、定时自动模拟鼠标点击,模拟键盘按键

复制代码
import pyautogui
import datetime
import time
import os
import win32gui,win32con
from win32 import win32api, win32gui, win32print
from win32.lib import win32con
import math
from colorama import Fore,Back,Style
import psutil
import tkinter as tk
import pygetwindow


mem = psutil.virtual_memory()
# 系统总计内存
zj = float(mem.total) / 1024 / 1024 / 1024
# 系统已经使用内存
ysy = float(mem.used) / 1024 / 1024 / 1024

# 系统空闲内存
kx = float(mem.free) / 1024 / 1024 / 1024

R =float(mem.used) * 100 / float(mem.total)
"""获取真实的分辨率"""
hDC = win32gui.GetDC(0)
width = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)  # 横向分辨率
height = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)  # 纵向分辨率
print(width)
print(height)

window = tk.Tk()
hwnd_title = {}

def get_all_hwnd(hwnd, mouse):
    if (win32gui.IsWindow(hwnd)
            and win32gui.IsWindowEnabled(hwnd)
            and win32gui.IsWindowVisible(hwnd)):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})

win32gui.EnumWindows(get_all_hwnd, 0)
for h, t in hwnd_title.items():
    if t :
        print (h, t)


def setWindowTopmost(hwnd):
    '''将某个窗口设置为置顶窗口'''
    win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, width-350,height-175,350,119, win32con.SWP_SHOWWINDOW)
    #win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,0,0,45,3,win32con.SWP_NOSIZE|win32con.SWP_NOMOVE)

#hwnd = win32gui.FindWindow(None,"Windows PowerShell")
#if hwnd != 0:
        #setWindowTopmost(hwnd)

#os.popen('mode con cols=50 lines=3')
win = pygetwindow.getWindowsWithTitle('Py')[0]
win.size = (350, 119)
number=100

#pyautogui.moveTo(0,100)
while True:
    #pyautogui.press('capslock')
    #pyautogui.press('numlock')
    #hwnd = win32gui.FindWindow(None,"Windows PowerShell")
    #setWindowTopmost(hwnd)
    #win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0,win32con.SWP_NOMOVE | win32con.SWP_NOACTIVATE |win32con.SWP_NOOWNERZORDER | win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE)
    time.sleep(1.0)
    os.system('cls')
    current_time = datetime.datetime.now()
    timestamp = current_time.strftime('%Y-%m-%d %H:%M:%S')
    timestamp = timestamp + '.'+str(current_time.microsecond)[0:3]
    #print("" + str(timestamp))
    x, y = pyautogui.position()
    print("mouse position:", x, y)
    if number > 12:
       pyautogui.press('numlock')
       win.moveTo(width-350, height-175-number)
       time.sleep(0.9)
       win.moveTo(width-350, height-175)
       number=0
       pyautogui.moveTo(1212, 676)
       pyautogui.click()
    else:
       number=number+1
       
    print (Fore.GREEN + str(timestamp) + " Memory:",'%d%%' % R)

三、参考文章

Python中鼠标的位置获取方法_笔记大全_设计学院

用Python制作一个自动点击器,三种模块都可以_鼠标自动点击脚本-CSDN博客

相关推荐
大Mod_abfun12 小时前
多显示器窗口分布规律探索(包括WorkerW的区域)
计算机外设·显示器布局·桌面背景层·workerw
猎板PCB 邹亮4 天前
猎板PCB:专业键盘PCB板解决方案供应商
计算机外设·键盘·pcb工艺
点灯小铭5 天前
基于STM32单片机的无线鼠标设计
stm32·单片机·计算机外设·毕业设计·课程设计
Jwest20216 天前
佳维视高亮度工业显示器,强光环境清晰可见
计算机外设
呉師傅8 天前
奔图P2500NW打印机手机无线连接方法
运维·网络·windows·智能手机·计算机外设·电脑
诚实可靠王大锤8 天前
使用MatterJs物理2D引擎实现重力和鼠标交互等功能,有点击事件(盒子堆叠效果)
javascript·计算机外设·交互
FreeDw资源库9 天前
Windows驱动更新下载工具,电脑硬件设备驱动程序自动安装下载更新,可备份还原!键盘鼠标声卡网卡显卡主板硬盘驱动都可以下载,免费使用的神器!
windows·计算机外设·驱动更新·驱动下载
qq_546937279 天前
轻量级鼠标右键增强工具 MousePlus
计算机外设
midou55com9 天前
MousePlus鼠标右键增强工具v5.5.25,支持鼠标轮盘功能
计算机外设·鼠标右键增强
Vic1010110 天前
笔记本电脑联想T14重启后无法识别外置红米屏幕
计算机外设·电脑