如何在代码中按下python 2.7中的“enter”按钮,而无需用户按下它

时间:2015-07-06 09:30:47

标签: python python-2.7 user-input

这个问题可能已经再次回答,因为我在问这个问题之前就已经搜索过了。回复的人说应该使用win32api,但我不知道它在哪里,所以我可以导入它(python在导入时无法找到它)或者如何使用它,因为我最近开始学习python 。我需要的是一个代码,它将自动按下“输入”按钮。如果我需要某个图书馆,我想知道我在哪里可以找到它。如果我的问题不明确或者您需要我添加更多内容,请通知我。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

也可以在不安装其他模块的情况下发送按键,请参阅以下内容作为框架:

import ctypes

SendInput = ctypes.windll.user32.SendInput

PUL = ctypes.POINTER(ctypes.c_ulong)

class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]

def PressKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

#############################################################################################################################

lookup = {"a":(0x41),"A":(0x10,0x41),
          "b":(0x42),"B":(0x10,0x42),
          "c":(0x43),"C":(0x10,0x43),
          "d":(0x44),"D":(0x10,0x44),
          "e":(0x45),"E":(0x10,0x45),
          "f":(0x46),"F":(0x10,0x46),
          "g":(0x47),"G":(0x10,0x47),
          "h":(0x48),"H":(0x10,0x48),
          "i":(0x49),"I":(0x10,0x49),
          "j":(0x4a),"J":(0x10,0x4a),
          "k":(0x4b),"K":(0x10,0x4b),
          "l":(0x4c),"L":(0x10,0x4c),
          "m":(0x4d),"M":(0x10,0x4d),
          "n":(0x4e),"N":(0x10,0x4e),
          "o":(0x4f),"O":(0x10,0x4f),
          "p":(0x50),"P":(0x10,0x50),
          "q":(0x51),"Q":(0x10,0x51),
          "r":(0x52),"R":(0x10,0x52),
          "s":(0x53),"S":(0x10,0x53),
          "t":(0x54),"T":(0x10,0x54),
          "u":(0x55),"U":(0x10,0x55),
          "v":(0x56),"V":(0x10,0x56),
          "w":(0x57),"W":(0x10,0x57),
          "x":(0x58),"X":(0x10,0x58),
          "y":(0x59),"Y":(0x10,0x59),
          "z":(0x5a),"Z":(0x10,0x5a),

          "0":(0x30),
          "1":(0x31),
          "2":(0x32),
          "3":(0x33),
          "4":(0x34),
          "5":(0x35),
          "6":(0x36),
          "7":(0x37),
          "8":(0x38),
          "9":(0x39),

          "!":(0x10,0x31),
          "?":(0x10,0xbf),
          "\n":(0x0d),
          " ":(0x20),
          "'":(0x6c),
          "*":(0x10,0x38),
          "+":(0x10,0xbb),
          "/":(0xbf),
          "(":(0x10,0x39),
          ")":(0x10,0x30),
          "-":(0xbd),
          ".":(0xbe),}

要使用此功能,您可以添加:

PressKey(lookup["\n"])
ReleaseKey(lookup["\n"])

到脚本的底部,或者将第一部分代码保存为脚本并导入它。

我为我需要的所有密钥代码构建了查找字典,但是可以在此处找到密钥代码列表: MSDN Virtual Key Codes