为什么模拟按键不能用于alt + tab和ctrl + alt + del?

时间:2016-05-27 01:47:24

标签: python keyboard

所以我想尝试模拟一些按键,我使用了这里的脚本:

Python simulate keydown

Generate keyboard events

当我运行这些脚本时,虽然按下了按钮,但它没有显示Windows 8上的更改窗口或ctrl alt del菜单。但是,在Windows 7计算机上,alt选项卡确实有效。为什么它不适用于Windows 8,有没有办法模拟这些笔画?

1 个答案:

答案 0 :(得分:1)

import ctypes,time

KEYEVENTF_UNICODE = 0x0004
KEYEVENTF_KEYUP = 0x0002

def PressKey(KeyUnicode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( 0, KeyUnicode, KEYEVENTF_UNICODE, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(KeyUnicode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( 0, KeyUnicode, KEYEVENTF_UNICODE|KEYEVENTF_KEYUP, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))


def PressAltTab():

    ctypes.windll.user32.keybd_event(0x12, 0, 0, 0) #Alt
    ctypes.windll.user32.keybd_event(0x09, 0, 0, 0) #Tab

    time.sleep(2) #optional : if you want to see the atl-tab overlay

    ctypes.windll.user32.keybd_event(0x09, 0, 0x0002, 0) #~Tab
    ctypes.windll.user32.keybd_event(0x12, 0, 0x0002, 0) #~Alt

PressAltTab()

这适用于ALT + TABPressAltTab()您看到ctypes.windll.user32.keybd_event(0x12, 0, 0, 0),只需找到ctrldelete的正确数字。如您所见alt0x12

但是,ctrl + alt + del是一个特殊的快捷方式,可能有安全理由阻止它进行虚假按压。至少那是我在论坛上看到的。