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博客

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这样的功能,可以使用Python中的pyautogui库。具体步骤如下: 1. 安装pyautogui库:在命令行中输入“pip install pyautogui”即可。 2. 导入pyautogui库:在Python代码中,使用“import pyautogui”语句导入pyautogui库。 3. 获取屏幕分辨率:使用pyautogui库中的“size()”函数获取屏幕的分辨率。 4. 计算需要移动的距离:根据需要移动到的位置,计算出需要移动的距离。 5. 移动鼠标:使用pyautogui库中的“moveTo()”函数移动鼠标到指定位置。 6. 点击鼠标:使用pyautogui库中的“click()”函数模拟鼠标点击事件。 7. 增加随机因素:为了防止被检测到,可以在移动鼠标点击鼠标时增加一些随机因素,使其看起来更像真实的人工操作。 8. 循环执行:使用while语句实现重复执行。 下面是示例代码: ```python import pyautogui import random import time # 获取屏幕分辨率 screenWidth, screenHeight = pyautogui.size() # 循环执行 while True: # 随机生成需要移动到的位置 x = random.randint(0, screenWidth) y = random.randint(0, screenHeight) # 计算需要移动的距离 distance = ((pyautogui.position()[0] - x) ** 2 + (pyautogui.position()[1] - y) ** 2) ** 0.5 # 移动鼠标 pyautogui.moveTo(x, y, duration=distance/1000 + random.uniform(0, 0.5)) # 随机等待一段时间 time.sleep(random.uniform(0, 1)) # 模拟鼠标点击事件 pyautogui.click() # 随机等待一段时间 time.sleep(random.uniform(0, 1)) ``` 这段代码会不断随机移动鼠标到屏幕上的不同位置,并模拟鼠标点击事件。为了防止被检测到,每次移动鼠标点击鼠标时都会增加一些随机因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值