0

so i wanted to try and simulate some key presses and i used the scripts found here:

Python simulate keydown

and

Generate keyboard events

when I run these scripts, although the buttons are being pressed, it does not bring up the change windows thing or ctrl alt del menu on windows 8. however, on a windows 7 computer the alt tab did work. Why does it not work on windows 8 and is there a way I can simulate these strokes?

Community
  • 1
  • 1
J leong
  • 285
  • 5
  • 13
  • use `pyautogui` module. It's the best for keyboard-mouse actions. And you could find tutorials easily. – GLHF May 27 '16 at 02:09
  • @GLHF pyautogui still does not simulate alt tab or ctrl alt delete. the buttons are pressed but its not invoking the special functions for windows. – J leong May 27 '16 at 02:30
  • It does. It does everything with keyboard+mouse. There are some functions in pyautogui for holding buttons. And it does work. – GLHF May 27 '16 at 02:45
  • @GLHF are you sure? can you post the code for it then? because when I use keydown in pyautogui to try and simulate either alt tab or ctrl alt del it fails. the buttons are pressed and held however it does not do window's special functions for those bindings. – J leong May 27 '16 at 02:56

1 Answers1

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()

This works for ALT+TAB, at PressAltTab() you see ctypes.windll.user32.keybd_event(0x12, 0, 0, 0), just find the right numbers for ctrl and delete. As you see alt is 0x12

However, ctrl+alt+del is a special shortcut, there might be security reasons to block it from fake-pressing. At least that's what I saw in the forums.

GLHF
  • 3,835
  • 10
  • 38
  • 83