7.0 and I am wanting to know what I've done wrong with this code. I want it to loop infinitely and say vcmine start but I've done I've tried using for while and tried spamming the code over and over I just want it to print this code into a game so that I don't have to be at my pc but I can only get it to work with it only saying it once please help I've something wrong and I don't know what
from pynput.keyboard import Key, Controller
import time
keyboard = Controller ()
while True:
for char in "vcmine start":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.03)
break
while True:
keyboard.press(key.enter)
keyboard.press(key.enter)
break
I think what you want is :
from pynput.keyboard import Key, Controller
import time
keyboard = Controller ()
while True:
for char in "vcmine start":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.03)
keyboard.press(key.enter)
time.sleep(0.03)
keyboard.press(key.enter)
time.sleep(0.03)
Related
I am a beginner programmer trying to create a Minecraft mining bot using pydirectinput. When I try to use moveRel it does nothing. I don't know what to do to fix this.
import pydirectinput as p
import time
# resume game in top right
p.click(2098, 187)
time.sleep(1)
# Breaks block
def mine():
p.mouseDown()
time.sleep(1)
p.mouseUp()
time.sleep(1)
#program starts
mine()
p.moveRel(0, 300)
mine()
I used this function below to keep the Python program pressing 'enter" over and over again, but when I click with the mouse, a window appears with "Python Not Responding" and then I have to close the program, so my question is; is there any way for me to catch that with Python?
Code:
def stop():
pyautogui.FAILSAFE = False
while True:
pyautogui.press('enter')
Thanks in advance!
You cannot catch your program crashing as if it were an exception. What you can do in add a delay between sending each key press:
import time
def stop():
pyautogui.FAILSAFE = False
while True:
pyautogui.press('enter')
time.sleep(0.1)
Why is the input() after the if statement not working?
I need some help, the input() after the if statement is supposed to stop the script, however it just continues. If i give it another command, for example print("...") or time.sleep(10) it will execute, it is only the input() that does not work. Any ideas?
Edit: Because it might not be clear what the intention is. When asking for an input after the if statement the script should wait before continuing. This is because I want it to pause after I move the mouse so that it does not keep spamming the keys, but I am able to resume it if needed.
import time
from pynput.keyboard import Key, Controller as KeyController
from pynput.mouse import Controller as MouseController
key = KeyController()
mouse = MouseController()
def f1_toggle():
key.press(Key.f1)
key.release(Key.f1)
def enter_toggle():
key.press(Key.enter)
key.release(Key.enter)
input("Press any key to start:")
time.sleep(5)
while True:
start_position = mouse.position
key.press(Key.ctrl_l)
print("Ctrl pressed")
f1_toggle()
print("F1 toggled")
key.release(Key.ctrl_l)
print("Ctrl released")
time.sleep(1)
enter_toggle()
print("Enter toggled")
time.sleep(.5)
end_position = mouse.position
if start_position != end_position:
input("Press any key to continue ")
You're calling enter_toggle() before you ask for input. This is simulating pressing the Enter key. This is being used as the response to the input() call, so it doesn't wait for the user to enter something manually.
Are you sure the two variables are different?
I don't have pyinput, and I am using python 3.8, but in my computer, the input isn't skipped (I commented the pyinput).
Try this:
try:
input("Press any key to continue ")
except Exception as e:
print(e);
Try to see if this helps and what it prints.
I'm building a rudimentary autocomplete for my work, and it was doing fine, until I ran into this problem.
See example
import threading
from pynput import keyboard
from pynput.keyboard import Controller
controll = Controller()
def on_press(key):
if(key == keyboard.Key.alt_l):
controll.type("RIGHT !!!")
if(key == keyboard.Key.delete or key == keyboard.Key.enter):
return False
def handleKey():
x = input('INPUT> ')
print(x)
handle = threading.Thread(target=handleKey)
handle.start()
controll.type("RIGHT !!!")
with keyboard.Listener(
on_press=on_press) as listener:
listener.join()
When you press alt, you will see the output with the wrong string;
It must be [2] RIGHT !, but it was pressed [2]ight
But note that this only happens with the method call inside the event.
Is this a known issue? Am I using it wrong? I'm really confused
I believe that only with python3 and pip3 install pynput, this will make the example work
Tested with Windows 10
I've had a similar issue when trying to press a key while listening to a key. By my experience it is next to impossible. I have even tried to use other libraries' press functions such as pyautogui and keyboard inside of the pynput on_press function.
def on_press(key):
if key = keyboard.Key.esc:
return False
else:
if len(words) > i:
# p = keyboard.KeyCode.from_char(words[i])
pyautogui.press(word[i]) // also used keyboard.press(word[i])
i += 1
else:
keyboard.Controller.press(keyboard.Key.space)
with keyboard.Listener(on_press=on_press, suppress=True) as listener:
listener.join()
I feel like since the press action and the listen action are in different threads the program has to finish one before the other. At least that is what I gathered as I also have not found a solution to this. I also tried putting the press line after the listener.join() like this:
def on_press(key):
if key != keyboard.Key.esc:
return True
with keyboard.Listener(on_press=on_press, suppress=True) as listener:
listener.join()
if len(words) > i:
# p = keyboard.KeyCode.from_char(words[i])
pyautogui.press(words[i])
i += 1
else:
keyboard.Controller.press(keyboard.Key.space)
However, this did not solve the issue either because the program finished after listening to one input and then pressing words[i] once.
Please let me know if you find something. However I may not be able to comment bc of low reputation.
I had a similar issue as well, and something like this worked for me (on Ubuntu 20.04):
from pynput import keyboard
import threading
from time import sleep
event_queue = []
event_to_callback = {
# put your own key-to-function mapping here
# keyboard.KeyCode.from_char('c') : deal_with_c
}
def process_events():
while True:
for c in event_queue:
event_to_callback[c]()
event_queue.clear()
sleep(0.2)
keyboard.Listener(on_press=lambda k: event_queue.append(k)).start()
threading.Thread(target=process_events, args=()).start()
This decouples the listener's thread from the controller's thread; any callbacks that invoke pynput.keyboard.Controller methods will now be executed on a separate thread, different from the one of the listener.
I'm looking to make a 'kill switch' for my python script. Essentially, while my main script is looping and doing it's thing, in the background it's listening for a specific key press and then exits the script entirely when that key is pressed.
I would just throw periodic key checks into the main script, however there are many sleeps and waits, and sitting there holding down the key until the next check comes up isn't very practical.
I know how to code the event listener for the keyboard key, I've just never worked with threads before.
I am using pynput just in case that might cause any incompatibilities with threading libraries.
Any help is greatly appreciated.
keyboard module is capturing events in separate thread, so it might be what you are looking for.
Try something like this:
import keyboard
import time
stop_switch = False
def switch(keyboard_event_info):
global stop_switch
stop_switch = True
keyboard.unhook_all()
print(keyboard_event_info)
def main_function():
global stop_switch
keyboard.on_press_key('enter', switch)
while stop_switch is False:
if stop_switch is False:
print("1")
if stop_switch is False:
time.sleep(0.2)
if stop_switch is False:
print("2")
if stop_switch is False:
time.sleep(0.5)
if stop_switch is False:
print("3")
if stop_switch is False:
time.sleep(1)
stop_switch = False
main_function()
Simple way to exit almost immediately from time.sleep() with for example 10 seconds of sleep:
def main_function():
global stop_switch
keyboard.on_press_key('enter', switch)
while stop_switch is False:
if stop_switch is False:
print("sleeping for 10 seconds")
for i in range(100):
if stop_switch is False:
time.sleep(0.1)
print("program stopped")
stop_switch = False
But better approach is to use threading.Event. Check this.