python keyboard.press not working with other applicatons

Question:

I have made a python script that presses space once a second so I can go AFK in games and not get kicked for being idle. I tested it in notepad and it works but when i open Minecraft or Roblox nothing happens.
I did try looking for an answer but couldn’t find anything

from pynput.keyboard import Key, Controller
import time
keyboard = Controller()


while True:
    time.sleep(1)
    keyboard.press(' ')
    keyboard.release(' ')
Asked By: ReactiveSlime

||

Answers:

Sometimes games won’t recognize a super-short key press. You essentially have a space press of less than a microseconds. Try adding a delay between the press and release.

from pynput.keyboard import Key, Controller
import time
keyboard = Controller()


while True:
    time.sleep(1)
    keyboard.press(' ')
    time.sleep(0.15)
    keyboard.release(' ')
Answered By: James
from pynput.keyboard import Key, Controller
import time
import keyboard
keyboard = Controller()


while 1:
    if keyboard.is_pressed('r'):
        while True:
            time.sleep(1)
            keyboard.press('space')
Answered By: user17061283

you should probably run the cmd as administrator and put in some delay for keys to be registered. play around with the values of the delay to find one that suits your need

Answered By: merrickal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.