首页 > python 模拟键盘输入英文单词的时候出现空过的现象,是不是跟ord()这里有关系?

python 模拟键盘输入英文单词的时候出现空过的现象,是不是跟ord()这里有关系?

这是一段自动登录QQ的代码

import subprocess
import win32api
import win32con
import time

from ctypes import *

def an(qq):                                                           #自定义 键盘输入
    for i in qq:
        #print(i)
        win32api.keybd_event(ord(i),0,0,0) 
        win32api.keybd_event(ord(i),0,win32con.KEYEVENTF_KEYUP,0)
        time.sleep(0.5)

qq_exe = r"D:\qq\Bin\QQ.exe"                                          #打开QQ
subprocess.Popen([qq_exe])
time.sleep(5)

windll.user32.SetCursorPos(969, 583)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)    
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)         #选定QQ账号输出框
time.sleep(0.5)

an("*****")                                                           #输入账号

win32api.keybd_event(9,0,0,0)
win32api.keybd_event(9,0,win32con.KEYEVENTF_KEYUP,0)                   #Tab
    
an("*******")                                                          #输入密码
win32api.keybd_event(13,0,0,0)
win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)                  #回车
time.sleep(5)    

我发现在密码含有英文字母的时候,部分英文字母并没有输入,会直接跳过。

观察了几次,好像"x","q"等都会不输入。纯数字的密码可以完全输入。(只试了四五组,可能不准确)

请问这是什么情况?


# -*- coding: utf-8 -*- 

import subprocess
import win32api
import win32con
import time

from ctypes import *

def an(qq):
    for i in qq:
        #print(i)
        if i.isdigit():
            win32api.keybd_event(ord(i),0,0,0) 
            win32api.keybd_event(ord(i),0,win32con.KEYEVENTF_KEYUP,0)
            time.sleep(0.5)
        else:
            win32api.keybd_event(ord(i.upper()),0,0,0) 
            win32api.keybd_event(ord(i.upper()),0,win32con.KEYEVENTF_KEYUP,0)
            time.sleep(0.5)

f = file("qqzhanghao.txt")
lines = f.readlines()
f.close()
for line in lines:
    data = line.split()

    a = data[0]
    b = data[1]

    qq_exe = r"D:\qq\Bin\QQ.exe"
    subprocess.Popen([qq_exe])
    time.sleep(5)

    windll.user32.SetCursorPos(969, 583)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    time.sleep(0.5)
    #s = {"0":48,"1":49,"2":50,"3":51,"4":52,"5":53,"6":54}

    #print a
    an(a)

    win32api.keybd_event(9,0,0,0)
    win32api.keybd_event(9,0,win32con.KEYEVENTF_KEYUP,0)
    win32api.keybd_event(8,0,0,0)
    win32api.keybd_event(8,0,win32con.KEYEVENTF_KEYUP,0)
    
    #print b
    an(b)
    win32api.keybd_event(13,0,0,0)
    win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)
    time.sleep(5)
    

终于做完了!

请输入代码

可以参考 Virtual-Key Codes

https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

pywin32是封装这个

你看了一下就会发现 只有 大写的'A' - 'Z'没有小写的'a' - 'z'


你想了一下我们正常的键盘输入就知道了

普通按 A(键盘上的键位) 在大写字母灯没亮的时候输入的是 a

所以对于想要输入"q"

你应该用的是ord("q".upper())


那么怎么输入大写的A呢, 举一反三, 相信大家肯定会

【热门文章】
【热门文章】