Python win32api.keybd_event模拟键盘输入
win32api.keybd_event
该函数原型:keybd_event(bVk, bScan, dwFlags, dwExtraInfo)
第一个参数:虚拟键码(键盘键码对照表见附录);
第二个参数:硬件扫描码,一般设置为0即可;
第三个参数:函数操作的一个标志位,如果值为KEYEVENTF_EXTENDEDKEY则该键被按下,也可设置为0即可,如果值为KEYEVENTF_KEYUP则该按键被释放;
第四个参数:定义与击键相关的附加的32位值,一般设置为0即可。
例子:
import win32api
import win32con
win32api.keybd_event(13,0,0,0) # enter
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)
更多可参考:http://timgolden.me.uk/pywin32-docs/PyWin32.html
键盘键码对照表:
|
按键 |
键码 |
按键 |
键码 |
按键 |
键码 |
按键 |
键码 |
|
A |
65 |
6(数字键盘) |
102 |
; |
59 |
: |
58 |
|
B |
66 |
7(数字键盘) |
103 |
= |
61 |
+ |
43 |
|
C |
67 |
8(数字键盘) |
104 |
, |
44 |
< |
60 |
|
D |
68 |
9(数字键盘) |
105 |
- |
45 |
_ |
95 |
|
E |
69 |
* |
106 |
. |
46 |
> |
62 |
|
F |
70 |
! |
33 |
/ |
47 |
? |
63 |
|
G |
71 |
Enter |
13 |
` |
96 |
~ |
126 |
|
H |
72 |
@ |
64 |
[ |
91 |
{ |
123 |
|
I |
73 |
# |
35 |
\ |
92 |
| |
124 |
|
J |
74 |
$ |
36 |
} |
125 |
] |
93 |
|
K |
75 |
F1 |
112 |
a |
97 |
b |
98 |
|
L |
76 |
F2 |
113 |
c |
99 |
d |
100 |
|
M |
77 |
F3 |
114 |
e |
101 |
f |
102 |
|
N |
78 |
F4 |
115 |
g |
103 |
h |
104 |
|
O |
79 |
F5 |
116 |
i |
105 |
j |
106 |
|
P |
80 |
F6 |
117 |
k |
107 |
l |
108 |
|
Q |
81 |
F7 |
118 |
m |
109 |
n |
110 |
|
R |
82 |
F8 |
119 |
o |
111 |
p |
112 |
|
S |
83 |
F9 |
120 |
q |
113 |
r |
114 |
|
T |
84 |
F10 |
121 |
s |
115 |
t |
116 |
|
U |
85 |
F11 |
122 |
u |
117 |
v |
118 |
|
V |
86 |
F12 |
123 |
w |
119 |
x |
120 |
|
W |
87 |
Backspace |
8 |
y |
121 |
z |
122 |
|
X |
88 |
Tab |
9 |
0(数字键盘) |
96 |
Up Arrow |
38 |
|
Y |
89 |
Clear |
12 |
1(数字键盘) |
97 |
Right Arrow |
39 |
|
Z |
90 |
Shift |
16 |
2(数字键盘) |
98 |
Down Arrow |
40 |
|
0(小键盘) |
48 |
Control |
17 |
3(数字键盘) |
99 |
Insert |
45 |
|
1(小键盘) |
49 |
Alt |
18 |
4(数字键盘) |
100 |
Delete |
46 |
|
2(小键盘) |
50 |
Cap Lock |
20 |
5(数字键盘) |
101 |
Num Lock |
144 |
|
3(小键盘) |
51 |
Esc |
27 |
2(数字键盘) |
98 |
Down Arrow |
40 |
|
4(小键盘) |
52 |
Spacebar |
32 |
3(数字键盘) |
99 |
Insert |
45 |
|
5(小键盘) |
53 |
Page Up |
33 |
4(数字键盘) |
100 |
Delete |
46 |
|
6(小键盘) |
54 |
Page Down |
34 |
5(数字键盘) |
101 |
Num Lock |
144 |
|
7(小键盘) |
55 |
End |
35 |
||||
|
8(小键盘) |
56 |
Home |
36 |
||||
|
9(小键盘) |
57 |
Left Arrow |
37 |
||||
# 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()
有个小问题...鼠标定位
windll.user32.SetCursorPos(700, 510)
Python win32api.keybd_event模拟键盘输入的更多相关文章
- Python模拟键盘输入和鼠标操作
Python模拟键盘输入和鼠标操作 一.Python键盘输入模拟: import win32api import win32con win32api.keybd_event(17,0,0,0) #c ...
- 模拟键盘输入 : SendMessage, keybd_event, PostKeybdMessage
转自模拟键盘输入 : SendMessage, keybd_event, PostKeybdMessage 目的 最近项目要求在Windows CE下模拟键盘输入,上网搜索了一下,发现有3个API可以 ...
- 模拟键盘输入首先要用到一个API函数:keybd_event
转自:http://www.cnblogs.com/cpcpc/archive/2011/02/22/2123055.html 模拟键盘输入首先要用到一个API函数:keybd_event. 模拟按键 ...
- C# keybd_event用法 模拟键盘输入
最近有业务需求,需要模拟键盘输入,所以了解了一下C#中keybd_event函数的用法.该函数能够产生WM_KEYUP或WM_KEYDOWN消息,即可以触发键盘事件. 函数引用如下: [DllImpo ...
- VC 模拟键盘输入
转载请注明来源:https://www.cnblogs.com/hookjc/ vc模拟键盘输入keybd_event(VK_LWIN, 0, 0 ,0);keybd_event('M', 0, 0 ...
- 使用C#模拟键盘输入、鼠标移动和点击、设置光标位置及控制应用程序的显示
1.模拟键盘输入(SendKeys) 功能:将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样. 语法:SendKeys.Send(string keys);SendKeys.SendWa ...
- VB模拟键盘输入的N种方法
VB模拟键盘输入的N种方法http://bbs.csdn.net/topics/90509805hd378发表于: 2006-12-24 14:35:39用VB模拟键盘事件的N种方法 键盘是我们使用计 ...
- 用Delphi模拟键盘输入
在Windows大行其道的今天,windows界面程序受到广大用户的欢迎.对这些程序的操作不外乎两种,键盘输入控制和鼠标输入控制.有时,对于繁杂的,或重复性的操作,我们能否通过编制程序来代替手工输入, ...
- Python+Selenium自动化-模拟键盘操作
Python+Selenium自动化-模拟键盘操作 0.导入键盘类Keys() selenium中的Keys()类提供了大部分的键盘操作方法:通过send_keys()方法来模拟键盘上的按键. # ...
随机推荐
- C++Review15_内存管理
一.野指针 定义指针变量时最好初始化为NULL: 内存回收后,指针也用完了,这时候也需要及时将指针置为NULL: 指针就像野狗一样,为了防止它乱指,除了在使用期间,别的时候都需要置为NULL.这样它就 ...
- selenium 启动、窗口、获取标题
1. from selenium import webdriver #启动chrom浏览器,没写executable_path,这是因为配置环境时,已经将chromdriver放到python安装文件 ...
- 用 Weave Scope 监控集群【转】
创建 Kubernetes 集群并部署容器化应用只是第一步.一旦集群运行起来,我们需要确保一起正常,所有必要组件就位并各司其职,有足够的资源满足应用的需求.Kubernetes 是一个复杂系统,运维团 ...
- vue 中使用 echarts 自适应问题
echarts 自带的自适应方法 resize() 具体用法: let xxEcharts = this.$echarts.init(document.getElementById('xxx')) ...
- NIO 与 零拷贝
零拷贝介绍 零拷贝是网络编程的关键, 很多性能优化都需要零拷贝. 在 Java程序中, 常用的零拷贝方式有m(memory)map[内存映射] 和 sendFile.它们在OS中又是怎样的设计? NI ...
- 《Airbnb架构要点分享》阅读笔记
Airbnb成立于2008年8月,总部位于加利福尼亚州旧金山市.Airbnb是一个值得信赖的社区型市场,在这里人们可以通过网站.手机或平板电脑发布.发掘和预订世界各地的独特房源,其业务已经覆盖190个 ...
- 微信小程序学习开发笔记
首先注册小程序开账号,下载开发工具之后,先啃官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/ ,把小程序的基本的代码框架 ...
- 使用Hibarnate: 出现 java.sql.SQLException: ORA-00911: 无效字符, 解决思路
1. 查看到: Hibernat自动生成的sql查询语句 Hibernate: select * from ( select module0_.MODULE_ID as MODULE_ID1_1_, ...
- poj 1027 Ignatius and the Princess II全排列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- java正则表达式校验密码必须是包含大小写字母、数字、特殊符号的8位以上组合
一.需求:密码必须是包含大写字母.小写字母.数字.特殊符号(不是字母,数字,下划线,汉字的字符)的8位以上组合 二.方案:利用正则表达式来校验 三.思路:排除法 1.排除大写字母.小写字母.数字.特殊 ...