Python win32api.keybd_event模擬鍵盤輸入

lhrbest發表於2019-05-19

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)





About Me

........................................................................................................................

● 本文作者:小麥苗,部分內容整理自網路,若有侵權請聯絡小麥苗刪除

● 本文在itpub( http://blog.itpub.net/26736162 )、部落格園( http://www.cnblogs.com/lhrbest )和個人weixin公眾號( xiaomaimiaolhr )上有同步更新

● 本文itpub地址: http://blog.itpub.net/26736162

● 本文部落格園地址: http://www.cnblogs.com/lhrbest

● 本文pdf版、個人簡介及小麥苗雲盤地址: http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答: http://blog.itpub.net/26736162/viewspace-2134706/

● DBA寶典今日頭條號地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

........................................................................................................................

● QQ群號: 230161599 (滿) 、618766405

● weixin群:可加我weixin,我拉大家進群,非誠勿擾

● 聯絡我請加QQ好友 646634621 ,註明新增緣由

● 於 2019-05-01 06:00 ~ 2019-05-30 24:00 在魔都完成

● 最新修改時間:2019-05-01 06:00 ~ 2019-05-30 24:00

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

........................................................................................................................

小麥苗的微店 https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

小麥苗出版的資料庫類叢書 http://blog.itpub.net/26736162/viewspace-2142121/

小麥苗OCP、OCM、高可用網路班 http://blog.itpub.net/26736162/viewspace-2148098/

小麥苗騰訊課堂主頁 https://lhr.ke.qq.com/

........................................................................................................................

使用 weixin客戶端 掃描下面的二維碼來關注小麥苗的weixin公眾號( xiaomaimiaolhr )及QQ群(DBA寶典)、新增小麥苗weixin, 學習最實用的資料庫技術。

........................................................................................................................

歡迎與我聯絡

 

 



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2644877/,如需轉載,請註明出處,否則將追究法律責任。

相關文章