def press_shift_and_number(file, char):
file.write(f'KeyDown "Shift", 1\nDelay 50\nKeyDown "{char}", 1\nDelay 50\nKeyUp "{char}", 1\nDelay 50\nKeyUp "Shift", 1\nDelay 50\n')
def write_key_presses(key_sequences):
special_chars = {
'!': '1',
'@': '2',
'#': '3',
'$': '4',
'%': '5',
'^': '6',
'&': '7',
'*': '8',
'(': '9',
')': '0',
'{': "[",
'}': "]",
'+': "=",
'"': "'",
'<': ",",
'>': ".",
'~': "`",
'?': "/",
'_': '-',
'|': '\\'
}
with open('script.txt', 'w') as file:
file.write('Delay 10000\n')
for sequence in key_sequences:
sequence = sequence.strip().rstrip('\n')
if sequence != "":
for char in sequence:
if char in special_chars:
press_shift_and_number(file, special_chars[char])
elif ord(char) >= ord("A") and ord(char) <= ord("Z"):
file.write(f'KeyDown "Shift", 1\nDelay 50\nKeyDown "{char.lower()}", 1\nDelay 50\nKeyUp "{char.lower()}", 1\nDelay 50\nKeyUp "Shift", 1\nDelay 50\n')
else:
if char == ' ':
char = 'Space'
file.write(f'KeyDown "{char}", 1\nDelay 50\nKeyUp "{char}", 1\nDelay 50\n')
file.write('KeyDown "Enter", 1\nDelay 50\nKeyUp "Enter", 1\nDelay 50\n')
file_name = 'install_libs.txt'
with open(file_name, 'r', encoding='utf-8') as file:
lines = file.readlines()
file = file_name.split('.')[0]
input_strings = [f"cat << EOF >{file + '.sh'}"]
input_strings.extend(lines)
check_script = f"sh {file + '.sh'}"
input_strings.extend(["EOF", check_script])
write_key_presses(input_strings)
Windows上用虚拟机、Python和按键精灵实现自动测试Shell代码
最新推荐文章于 2024-10-28 11:43:39 发布