在python中模拟物理按键,而无需引发LowLevelKeyHookInjected(0x10)标志

时间:2019-02-11 05:02:29

标签: python-3.x emulation simulation keypress keyboard-events

我试图在python中模拟物理按键(即F12),而不在Windows中引发LowLevelKeyHookInjected(0x10)标志。这样做的目的是在程序中触发一项操作,以过滤掉模拟的键盘按键。该程序是一种辅助技术,它提供了用于操作系统控制的替代访问方法。

我尝试了许多选项,包括SendKeys,pyauotogui,pynput,ctype等...

如果我物理地按F12,则可以触发适当的操作,但是,使用上述方法通过python模拟F12的按动和释放不起作用。这是我尝试过的一个示例:

import pynput.keyboard import Key, Controller
import time
Keyboard = Controller()
keyboard.press(Key.f12)
time.sleep(.05)
keyboard.release(Key.f12)

因此,它仅模拟了f12键的按下,等待0.05秒,然后释放键。

我正在Windows上运行Python 3.6.6

谢谢您的所有帮助!

1 个答案:

答案 0 :(得分:1)

这是几天前遇到相同问题时使用的方法。有点混乱,但是可以完成工作。 :)

import ctypes
from ctypes import wintypes
import time
user32 = ctypes.WinDLL('user32', use_last_error=True)
INPUT_KEYBOARD = 1
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP       = 0x0002
KEYEVENTF_UNICODE     = 0x0004
MAPVK_VK_TO_VSC = 0
# msdn.microsoft.com/en-us/library/dd375731
wintypes.ULONG_PTR = wintypes.WPARAM
class MOUSEINPUT(ctypes.Structure):
    _fields_ = (("dx",          wintypes.LONG),
                ("dy",          wintypes.LONG),
                ("mouseData",   wintypes.DWORD),
                ("dwFlags",     wintypes.DWORD),
                ("time",        wintypes.DWORD),
                ("dwExtraInfo", wintypes.ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
    _fields_ = (("wVk",         wintypes.WORD),
                ("wScan",       wintypes.WORD),
                ("dwFlags",     wintypes.DWORD),
                ("time",        wintypes.DWORD),
                ("dwExtraInfo", wintypes.ULONG_PTR))
    def __init__(self, *args, **kwds):
        super(KEYBDINPUT, self).__init__(*args, **kwds)
        if not self.dwFlags & KEYEVENTF_UNICODE:
            self.wScan = user32.MapVirtualKeyExW(self.wVk,
                                                 MAPVK_VK_TO_VSC, 0)
class HARDWAREINPUT(ctypes.Structure):
    _fields_ = (("uMsg",    wintypes.DWORD),
                ("wParamL", wintypes.WORD),
                ("wParamH", wintypes.WORD))
class INPUT(ctypes.Structure):
    class _INPUT(ctypes.Union):
        _fields_ = (("ki", KEYBDINPUT),
                    ("mi", MOUSEINPUT),
                    ("hi", HARDWAREINPUT))
    _anonymous_ = ("_input",)
    _fields_ = (("type",   wintypes.DWORD),
                ("_input", _INPUT))
LPINPUT = ctypes.POINTER(INPUT)
def PressKey(hexKeyCode):
    x = INPUT(type=INPUT_KEYBOARD,
              ki=KEYBDINPUT(wVk=hexKeyCode))
    user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
    x = INPUT(type=INPUT_KEYBOARD,
              ki=KEYBDINPUT(wVk=hexKeyCode,
                            dwFlags=KEYEVENTF_KEYUP))
   user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def F12():
    PressKey(0x7B)
    time.sleep(0.5)
    ReleaseKey(0x7B)
    # you can change 0x30 to any key you want. For more info look at :
    # msdn.microsoft.com/en-us/library/dd375731


F12()

只需使用F12()模拟F12键。

如果还要模拟其他键,则可以将F12函数中0x7B的位置更改为其他内容(请查看msdn.microsoft.com/zh-cn/library/dd375731,以查找具有什么值的键)。 还要模拟字母,这可能有助于快速转换:

def toKeyCode(c):
    keyCode = keyCodeMap[c[0]]
    return keyCode

keyCodeMap = {
    'shift'             : "0x10",
    '0'                 : "0x30",
    '1'                 : "0x31",
    '2'                 : "0x32",
    '3'                 : "0x33",
    '4'                 : "0x34",
    '5'                 : "0x35",
    '6'                 : "0x36",
    '7'                 : "0x37",
    '8'                 : "0x38",
    '9'                 : "0x39",
    'a'                 : "0x41",
    'b'                 : "0x42",
    'c'                 : "0x43",
    'd'                 : "0x44",
    'e'                 : "0x45",
    'f'                 : "0x46",
    'g'                 : "0x47",
    'h'                 : "0x48",
    'i'                 : "0x49",
    'j'                 : "0x4A",
    'k'                 : "0x4B",
    'l'                 : "0x4C",
    'm'                 : "0x4D",
    'n'                 : "0x4E",
    'o'                 : "0x4F",
    'p'                 : "0x50",
    'q'                 : "0x51",
    'r'                 : "0x52",
    's'                 : "0x53",
    't'                 : "0x54",
    'u'                 : "0x55",
    'v'                 : "0x56",
    'w'                 : "0x57",
    'x'                 : "0x58",
    'y'                 : "0x59",
    'z'                 : "0x5A",
}

(这也是我的第一篇文章,所以如果我搞砸了,对不起:D)