jsmubanlogo
  • 首页
  • 网页模板
  • 特效代码
  • 博文源码
  • 插件下载
  •    

Python win32api.keybd_event模拟键盘输入

收藏    

作者第十天    2022-03-12

      

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

示例2复制代码
# 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)
复制代码

 原文:https://www.cnblogs.com/chenxi188/p/11642006.html


免责声明:
      1、 资源售价只是赞助,不代表代码或者素材本身价格。收取费用仅维持本站的日常运营所需。
      2、 本站资源来自用户上传,仅供用户学习使用,不得用于商业或者非法用途,违反国家法律一切后果用户自负。用于商业用途,请购买正版授权合法使用。
      3、 本站资源不保证其完整性和安全性,下载后自行检测安全,在使用过程中出现的任何问题均与本站无关,本站不承担任何技术及版权问题,不对任何资源负法律责任。
      4、 如有损害你的权益,请联系275551777@qq.com及时删除。

关于我们 | 积分获取 | 联系我们 | 用户协议 | 标签搜索 | 网站地图.html | 网站地图.xml | 网站地图.txt

Copyright © 2021-2023 All Right Reserved
陕公网安备 61082202000148号      陕ICP备2025078528号-1
js模板网 -陕西千手码农科技有限责任公司