简体   繁体   中英

How can I press the button "enter" in python 2.7, without having the user pressing it, from code

This question has probably been answered again, since I searched for it before I asked this question. The people who answered said that win32api should be used, but I don't know where it is, so I can import it(python can't find it when I import it) or how to use it, since I started learning python recently. What I need, is a code, that will automatically press the "enter" button. If I need a certain library, I would like to know where I can find it. Please, inform me if my question isn't clear or you need me to add more things. Thanks in advance :)

it is also possible to send keypresses without installing additional modules, see the following as a framework:

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),}

to use this, you can either add:

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

to the bottom of the script, or save the first section of code as a script and import it.

i built the lookup dictionary for all the key codes i required, but a list of key codes can be found here: MSDN Virtual Key Codes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM