· last year · Apr 30, 2024, 11:40 PM
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Filename: open_optional_features.py
4# Version: 1.0.0
5# Author: Jeoi Reqi
6
7"""
8This script facilitates the opening of the "Turn Windows features on or off" dialog on Windows systems.
9It does so by emulating key presses to navigate through the Windows Run dialog and enter the command to open the features dialog.
10
11Requirements:
12 - Python 3.x
13 - Windows operating system
14
15Usage:
16 - Run the script in a Python environment compatible with the specified requirements.
17
18Functions:
19 - press_key(key): Simulates pressing a key using ctypes.
20 - type_string(string): Simulates typing a string by pressing individual keys.
21 - simulate_key_strokes(): Simulates key strokes to open the Run dialog and type 'optionalfeatures' to open the 'Turn Windows features on or off' dialog.
22
23Additional Notes:
24 - The script utilizes ctypes to interact with the Windows API and simulate key presses.
25 - Ensure that the script is executed with the necessary permissions to emulate key presses and open system dialogs.
26"""
27
28import subprocess
29import os
30import sys
31import time
32import ctypes
33
34def press_key(key):
35 """
36 Simulate pressing a key using ctypes.
37
38 Parameters:
39 key (int): The virtual key code of the key to be pressed.
40 """
41 ctypes.windll.user32.keybd_event(key, 0, 0, 0)
42 time.sleep(0.05)
43 ctypes.windll.user32.keybd_event(key, 0, 2, 0)
44
45def type_string(string):
46 """
47 Simulate typing a string by pressing individual keys.
48
49 Parameters:
50 string (str): The string to be typed.
51 """
52 for char in string:
53 key_code = ord(char.upper())
54 press_key(key_code)
55
56def simulate_key_strokes():
57 """
58 Simulate key strokes to open the Run dialog and type 'optionalfeatures' to open the 'Turn Windows features on or off' dialog.
59 """
60 # Simulate pressing Win + R to open Run dialog
61 press_key(0x5B) # VK_LWIN
62 press_key(0x52) # 'R'
63 type_string("un") # Complete the simulated typing of "run"
64
65 # Hit Enter
66 press_key(0x0D) # 'Enter'
67
68 time.sleep(0.5) # Give time for Run dialog to appear
69
70 # Simulate typing "optionalfeatures"
71 type_string("optionalfeatures")
72
73 # Hit Enter
74 press_key(0x0D) # 'Enter'
75
76if __name__ == "__main__":
77 simulate_key_strokes()
78
79