win32api.keybd_event

该函数原型:keybd_event(bVk, bScan, dwFlags, dwExtraInfo)

第一个参数:虚拟键码(键盘键码对照表见附录);

第二个参数:硬件扫描码,一般设置为0即可;

第三个参数:函数操作的一个标志位,如果值为KEYEVENTF_EXTENDEDKEY则该键被按下,也可设置为0即可,如果值为KEYEVENTF_KEYUP则该按键被释放;

第四个参数:定义与击键相关的附加的32位值,一般设置为0即可。

例子

  1. import win32api
  2.  
    import win32con
  3.  
    win32api.keybd_event(13,0,0,0)     # enter
  4.  
    win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)  #释放按键

# 按下ctrl+s
win32api.keybd_event(0x11, 0, 0, 0)
win32api.keybd_event(0x53, 0, 0, 0)
win32api.keybd_event(0x53, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
# 按下回车
win32api.keybd_event(0x0D, 0, 0, 0)
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
# 按下ctrl+W
win32api.keybd_event(0x11, 0, 0, 0)
win32api.keybd_event(0x57, 0, 0, 0)
win32api.keybd_event(0x57, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)

# 按下ctrl+a
win32api.keybd_event(0x11, 0, 0, 0)
win32api.keybd_event(0x41, 0, 0, 0)
win32api.keybd_event(0x41, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
# 按下ctrl+v
win32api.keybd_event(0x11, 0, 0, 0)
win32api.keybd_event(0x56, 0, 0, 0)
win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)

实例2 模拟鼠标按键,并且截图

import os

import time

import win32gui

import win32api

import win32con

from PIL import ImageGrab

#os.startfile("D:\\artcut6\\Prog\\Artcut6.exe")

#time.sleep(1)

wdname1=u"文泰刻绘2009[] - [无标题-1]"

w1hd=win32gui.FindWindow(0,wdname1)

print w1hd

w2hd=win32gui.FindWindowEx(w1hd,None,None,None)

print w2hd

def aotohelper_wt(i):

#获取窗口焦点

win32gui.SetForegroundWindow(w2hd)

#快捷键Alt+F

win32api.keybd_event(18,0,0,0) # Alt

win32api.keybd_event(70,0,0,0) # F

win32api.keybd_event(70,0,win32con.KEYEVENTF_KEYUP,0) #释放按键

win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)

#快捷键I

win32api.keybd_event(73,0,0,0) # I

win32api.keybd_event(73,0,win32con.KEYEVENTF_KEYUP,0)

time.sleep(0.2)

wdname3=u"打开"

w3hd=win32gui.FindWindow(None,wdname3) #”打开“ 窗口句柄

#print w3hd

#win32gui.MoveWindow(w3hd, 50, 50, 300, 200, True)

if i<=9:

msg="YC-00"+str(i)

elif 10<=i<=99:

msg="YC-0"+str(i)

else:

msg="YC-"+str(i)

edithd=win32gui.FindWindowEx(w3hd,None,"Edit",None)

win32api.SendMessage(edithd,win32con.WM_SETTEXT,None,msg)

time.sleep(0.1)

#btnhd=win32gui.FindWindowEx(w3hd,None,"BUTTON",None)

#print btnhd

#模拟快捷键Alt+O

win32api.keybd_event(18,0,0,0) # Alt

win32api.keybd_event(79,0,0,0) # O

win32api.keybd_event(79,0,win32con.KEYEVENTF_KEYUP,0) #释放按键

win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)

time.sleep(0.1)

#模拟鼠标操作

win32api.SetCursorPos([30,150]) #为鼠标焦点设定一个位置

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)

win32api.SetCursorPos([500,500])

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)

time.sleep(0.1)

#模拟快捷键F7(极限观察)

win32api.keybd_event(118,0,0,0) # F7

win32api.keybd_event(118,0,win32con.KEYEVENTF_KEYUP,0) #释放按键

time.sleep(0.2)

#利用PIL截屏

path="C:\\Users\\LY\\Desktop\\pic\\"

filename="YC-"+str(i)+".jpg"

im=ImageGrab.grab()

im.save(path+filename)

#模拟快捷键F8(回到原页面大小)

win32api.keybd_event(119,0,0,0) # F8

win32api.keybd_event(119,0,win32con.KEYEVENTF_KEYUP,0) #释放按键

#模拟键盘事件delete

win32api.keybd_event(46,0,0,0) # Delete

win32api.keybd_event(46,0,win32con.KEYEVENTF_KEYUP,0) #释放按键

time.sleep(1)

for i in range(2,85):

aotohelper_wt(i)

print i

print "work done!"

实例3

# coding=utf-8
from selenium import webdriver
import win32api
import win32con
import win32clipboard
from ctypes import *
import time# 浏览器打开百度网页
browser = webdriver.Chrome()
browser.maximize_window()
browser.get("https://www.baidu.com/")
time.sleep(2)# 获取页面title作为文件名
title = browser.title
# 设置路径为:当前项目的绝对路径+文件名
path = (os.path.dirname(os.path.realpath(__file__)) + "\\" + title + ".html")
# 将路径复制到剪切板
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(path)
win32clipboard.CloseClipboard()
# 按下ctrl+s
win32api.keybd_event(0x11, 0, 0, 0)
win32api.keybd_event(0x53, 0, 0, 0)
win32api.keybd_event(0x53, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
# 鼠标定位输入框并点击
windll.user32.SetCursorPos(700, 510)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
time.sleep(1)
# 按下ctrl+a
win32api.keybd_event(0x11, 0, 0, 0)
win32api.keybd_event(0x41, 0, 0, 0)
win32api.keybd_event(0x41, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
# 按下ctrl+v
win32api.keybd_event(0x11, 0, 0, 0)
win32api.keybd_event(0x56, 0, 0, 0)
win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
# 按下回车
win32api.keybd_event(0x0D, 0, 0, 0)
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
browser.close()

w32模块模拟鼠标键盘操作的更多相关文章

  1. 将CodedUI Test 放到控制台程序中,模拟鼠标键盘操作

    CodedUI Test是微软的自动化测试工具,在VS中非常好用.可以用来模拟鼠标点击,键盘输入.但执行的时候必须要用mstest调用,无法传入参数(当然可以写入config文件中,但每次修改十分麻烦 ...

  2. selenium webdriver(4)---模拟鼠标键盘操作

    webdriver提供Actions来模拟鼠标悬浮.拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Action ...

  3. python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

    0.关键实现:程序窗口前置 python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作 1.参考 aut ...

  4. selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等

    selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...

  5. Java+selenium之WebDriver模拟鼠标键盘操作(六)

    org.openqa.selenium.interactions.Actions类,主要定义了一些模拟用户的鼠标mouse,键盘keyboard操作.对于这些操作,使用 perform()方法进行执行 ...

  6. selenium webdriver模拟鼠标键盘操作

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...

  7. selenuim2模拟鼠标键盘操作

    有时候有些元素不便点击或者做其他的操作,这个时候可以借助selenium提供的Actions类,它可以模拟鼠标和键盘的一些操作,比如点击鼠标右键,左键,移动鼠标等操作.对于这些操作,使用perform ...

  8. selenium模拟鼠标键盘操作

    简单操作: 1.点击(鼠标左键)页面按钮:click() 2.清空输入框:clear() 3.输入字符串:send_keys()submit提交表单: 1.一般情况可以点击搜索按钮来搜索 2.也可以用 ...

  9. Delphi下利用WinIo模拟鼠标键盘详解 有参考价值

    https://blog.csdn.net/fgrass_163/article/details/6365296 Delphi下利用WinIo模拟鼠标键盘详解 2011年04月26日 21:03:00 ...

  10. C#模拟鼠标键盘控制其他窗口(一)

    编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的执行,开发 ...

随机推荐

  1. vue项目中 vscode 保存时自动格式化设置,保持单引号和去除多余分号、逗号

    1.settings.json中添加: "prettier.semi": false, // 取消自动加分号 "prettier.singleQuote": t ...

  2. python+selenium+unittest自动化测试

    目前先用这个记录自动化测试相关内容,后期再进行整理: 1.自动化测试:testcase-->test suite  ---> TestRunner 2.TestRunner时,一种将内容打 ...

  3. python 如何实现多线程

    今天本来打算学习学习多进程的,但是由于我现在的电脑没有Linux系统,无法通过Linux系统编辑一些多进程的程序,因此我打算从多线程入手. 多线程 我们的程序一般都是多任务的,如果你没有好好的利用好, ...

  4. redis缓存lua脚本过多导致内存占用很多问题

    现象 生产某集群各节点已使用内存比较大,在清理了大量业务无用数据后,节点已使用内存却未下降. 排查与分析 通过info memory命令查看,内存碎片率略高,但是对某个主节点新添加的从节点内存使用依然 ...

  5. 什么是 SpringMvc

    SpringMvc 是 spring 的一个模块,基于 MVC 的一个框架,无需中间整合层来整合

  6. Mysql 5.7 内存配置 (未完成)

    innodb_buffer_pool_size = 4024024024 innodb 引擎,用于 缓存数据和索引

  7. hello cnb

    Huawei executive Meng Wanzhou freed by Canada arrives home in China 目录 关于git merge冲突时候的想法 Git修改commi ...

  8. C语言和C++的区别与联系(详细)

    文章转自:https://blog.csdn.net/cherrydreamsover/article/details/81835976 在学习了C语言和C++之后,这两者之间的区别我们需要仔细的捋一 ...

  9. AtCoder-abc230_g GCD Permutation 容斥

    J - GCD Permutation 传送门: J - GCD Permutation 知识点:素数筛.容斥定理.gcd 题意:长度为n的一个排列a中,求满足\(gcd(i,j)!=1 且 gcd( ...

  10. K8S Operator的开发与使用

    从应用角度考虑,为什么会出现如此多的Operator场景,为什么很多中间件和厂商都会提供基于Operator的部署方案,他的价值是什么? 随着时代的发展,企业应用部署环境从传统的物理机->虚拟机 ...