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. 一招彻底解决——双系统下windows时间错误显示

    双系统下windows与ubuntu系统时间不同步 我的笔电是windows系统,工作需要安装了ubuntu,于是成为双系统,于是从18年我就遇到了装了ubuntu系统之后windows时间显示错误的 ...

  2. springboot+mybatis+vue

    https://www.cnblogs.com/wlovet/p/10980579.html

  3. 关于cmake找不到库的问题

    1. Error:Could not find a configuration file for package 解决办法1:将/usr/lib/x86_64-linux-gnu/cmake/.... ...

  4. AT212 P-CASカードと高橋君

    题目描述 高桥君为了准备即将到来的7月27日土用丑日,打算邮购一些高级鳗鱼食材,通过网上银行来支付. 高桥君使用的银行卡背面有下图所示的9×9密码表.支付的时候从表中某一位置开始根据指定的方向连续读4 ...

  5. python+基本3D显示

    想要将双目照片合成立体图实现三维重建,完全没有头绪.但是对三维理解是必须的.所以将以前在单片机上运行的 3D画图 程序移植到python上.效果如下: 没有用numpy.openGL等,只用了纯mat ...

  6. ZFBJ - 1 - 42 - NO.1

  7. ssh原理及应用

    SSH原理与运用(一):远程登录 SSH原理与运用(一):远程登录 SSH原理与运用(二):远程操作与端口转发 SSH原理与运用(二):远程操作与端口转发 mitm应用: python开源三方库:ss ...

  8. Spring mvc 使用RestController 和 Controller

    1.RestController相当于Controller和ResponseBody两个注解功能的结合体 2.只有Controller 返回的结果是 视图,一个页面 . 这个要自己写相关的 hello ...

  9. c# 数组 集合 属性访问 设置

    当只修改数组或者集合的某一个特定值时不会经过CLR属性封装器

  10. 关于python print函数format 格式化

    关于python print函数format 格式化  Your Guide to the Python print() Function https://realpython.com/python- ...