Python win32api.keybd_event simulates keyboard input

win32api.keybd_event

The function prototype: keybd_event(bVk, bScan, dwFlags, dwExtraInfo)

The first parameter: virtual key code (see the appendix for the keyboard key code comparison table);

The second parameter: hardware scan code, generally set to 0;

The third parameter: a flag bit for function operation. If the value is KEYEVENTF_EXTENDEDKEY, the key is pressed. It can also be set to 0. If the value is KEYEVENTF_KEYUP, the key is released;

The fourth parameter: defines additional 32-bit values related to keystrokes, generally set to 0.

example:

import win32api
import win32con
win32api.keybd_event(13,0,0,0) # enter
win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0) #Release the key
# Press 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)
    # Press Enter
    win32api.keybd_event(0x0D, 0, 0, 0)
    win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(1)
    # Press 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)
# Press 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)
# Press 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)

For more information, please refer to: http://timgolden.me.uk/pywin32-docs/PyWin32.html

Keyboard key code comparison table:

button

key code

button

key code

button

key code

button

key code

A

65

6(numeric keypad)

102

;

59

:

58

B

66

7(numeric keypad)

103

=

61

+

43

C

67

8(numeric keypad)

104

,

44

<

60

D

68

9(numeric keypad)

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 (numeric keypad)

96

Up Arrow

38

Y

89

Clear

12

1(numeric keypad)

97

Right Arrow

39

Z

90

Shift

16

2(numeric keypad)

98

Down Arrow

40

0 (numeric keyboard)

48

Control

17

3(numeric keypad)

99

Insert

45

1(small keyboard)

49

Alt

18

4(numeric keypad)

100

Delete

46

2(small keyboard)

50

Cap Lock

20

5(numeric keypad)

101

Num Lock

144

3(small keyboard)

51

Esc

27

2(numeric keypad)

98

Down Arrow

40

4(small keyboard)

52

Spacebar

32

3(numeric keypad)

99

Insert

45

5(small keyboard)

53

Page Up

33

4(numeric keypad)

100

Delete

46

6(small keyboard)

54

Page Down

34

5(numeric keypad)

101

Num Lock

144

7(small keyboard)

55

End

35

8(small keyboard)

56

Home

36

9(small keyboard)

57

Left Arrow

37

Example 2

# coding=utf-8
from selenium import webdriver
import win32api
import win32con
import win32clipboard
from ctypes import *
import time# The browser opens the Baidu web page
browser = webdriver.Chrome()
browser.maximize_window()
browser.get("https://www.baidu.com/")
time.sleep(2)# Get the page title as the file name
title = browser.title
# Set the path to: absolute path of the current project + file name
path = (os.path.dirname(os.path.realpath(__file__)) + "" + title + ".html")
#Copy path to clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(path)
win32clipboard.CloseClipboard()
# Press 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)
# Position the mouse on the input box and click
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)
# Press 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)
# Press 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)
# Press Enter
win32api.keybd_event(0x0D, 0, 0, 0)
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
browser.close()
There is a small problem...mouse positioning
windll.user32.SetCursorPos(700, 510)