'How to attach a Python script to a process and let it run in the background

I have a Python script that sends keystrokes, what I'm trying to do is, run the script in a process that's in the background. Meaning that while I'm focused on a different process for example chrome.exe, I want the script to send keystrokes to a process in the background for example test.exe.



Solution 1:[1]

Try this.

import keyboard
import time

while True:
    time.sleep(.1)
    k = keyboard.get_hotkey_name()
    if k=='p': # change s with another key
        print('p is pressed')
        # OR #
    if keyboard.is_pressed(hotkey='p'):
        print('p is pressed')

Note: If you want to add a event on ctrl+s or ctrl+p You just need to change p to ctrl+p or anything you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sharim Iqbal