Advertisement
Python253

open_optional_features

Apr 30th, 2024
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  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. """
  8. This script facilitates the opening of the "Turn Windows features on or off" dialog on Windows systems.
  9. It does so by emulating key presses to navigate through the Windows Run dialog and enter the command to open the features dialog.
  10.  
  11. Requirements:
  12.    - Python 3.x
  13.    - Windows operating system
  14.  
  15. Usage:
  16.    - Run the script in a Python environment compatible with the specified requirements.
  17.  
  18. Functions:
  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.  
  23. Additional 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.  
  28. import subprocess
  29. import os
  30. import sys
  31. import time
  32. import ctypes
  33.  
  34. def 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.  
  45. def 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.  
  56. def 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.  
  76. if __name__ == "__main__":
  77.     simulate_key_strokes()
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement