python keyboard.press not working with other applicatons - python

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

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

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

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

Related

Problem with keyboard library, exactly with stopping .record() in function

import keyboard
import time
import pyautogui
import mouse
def call_record():
event = keyboard.record(until='shift')
print("ended record")
def call_playrecord():
print('playing...')
keyboard.play(event)
keyboard.add_hotkey('l', call_record)
keyboard.add_hotkey('n+m', call_playrecord)
keyboard.wait('esc')
This is a code and a problem about that, is that record wont stop when i press 'shift'
(nevermind that i dont have global set on event i will make this later)
I tried to make a record and play system with hotkeys but i cant stop the record

How to make the script press return with pynput

I just started learning python and I have made this script that types for me, but I couldn't figure out how to make it press the enter key.
import time
keyboard = Controller()
time.sleep(3)
for char in "Hello, this isn't notking but this is a bot, he has been learning python and learned this script that types for him!":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.12)```
This should help you with your problem. Next time please provide the whole code related to the question.
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
time.sleep(3)
for char in "Hello, this isn't notking but this is a bot, he has been learning python and learned this script that types for him!":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.12)
keyboard.press(Key.enter)
keyboard.release(Key.enter)

I am trying to develop a keyboard spammer and auto clicker but i cannot find anywhere a way to stop the spammer with a keybind

for now I'm just trying to get it to work with the keyboard spammer. So far I have done much searching but nothing seems to work.
if you have any tips on how to make the spamming stop without having to close the application it would be appreciated.
This is the code:
from typing import KeysView
import keyboard
import time
keyboard.wait("esc")
x = 4
while x != 5:
for i in range(9):
time.sleep(0.4)
print(keyboard.send("ctrl+v,enter"))
time.sleep(6)
(thanks)
Using the keyboard module there are many ways you could do this, one of them would be to add a hotkey that stops your loop via a global variable (in this example it's when you hit the spacebar):
from typing import KeysView
import keyboard
import time
keyboard.wait("esc")
running = True
def on_space():
global running
running = False
keyboard.add_hotkey('space', on_space)
while running:
for i in range(9):
time.sleep(0.4)
print(keyboard.send("ctrl+v,enter"))
time.sleep(6)
Adding a check for if a key is pressed inside of the for loop should do the trick. You could either make pressing the key break the loop, or since your loop waits for x to equal 5, you could simply make pressing the key change x to 5.

randomize the time from time.sleep

I'm fairly new to python and have been trying to write a script that automates a string of text. The problem is that i don't know how to randomize the time.sleep function. I have also tried adding the time in range function but still can't get it to randomize it.
The code looks like this at the moment. I have to get rid of the time.sleep(40) to randomize it
import time
from pynput.keyboard import Key, Controller
while True:
keyboard = Controller()
time.sleep(40)
for char in "nnnn":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.21)
Use the random module
import random
import time
from pynput.keyboard import Key, Controller
while True:
keyboard = Controller()
sleep_time = random.randrange(0, 100)
time.sleep(sleep_time)
for char in "nnnn":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.21)

Create Keyboard Shortcut?

I am searching for a way to create a cross-platform keyboard shortcut in Python. Such that when I press something like Ctrl+C or Ctrl+Alt+F, the program will run a certain function.
Does there exist such method or library?
I don't know is it possible to send combinations of Ctrl and/or alt. I tried couple of times but I think it doesn't work. (If someone has any other information please correct me). What you can do is something like this:
from msvcrt import getch
while True:
key = ord(getch())
if key == 27: #ESC
PrintSomething()
def PrintSomething():
print('Printing something')
this will run a scrpit every time you press ESC although it will only work when you run it in command prompt.
You can create a listener for keypress event by pynput library.
this is a simple code that run a function when press CTRL+ALT+H keys:
from pynput import keyboard
def on_activate():
print('Global hotkey activated!')
def for_canonical(f):
return lambda k: f(l.canonical(k))
hotkey = keyboard.HotKey(
keyboard.HotKey.parse('<ctrl>+<alt>+h'),
on_activate)
with keyboard.Listener(
on_press=for_canonical(hotkey.press),
on_release=for_canonical(hotkey.release)) as l:
l.join()
Try keyboard. It can be installed with pip install keyboard, and the Ctrl + C example can be accomplished with the syntax:
import keyboard
keyboard.add_hotkey("ctrl + c", print, args=("Hello", "world!"))

Categories