Im trying to make a simple code to crouch when I run it. this is my code:
from pynput.keyboard import Key, Controller
import time
import keyboard as key
keyboard = Controller()
time.sleep(2)
for i in range(30):
keyboard.press("c")
keyboard.release('c')
time.sleep(0.1)
Related
This code is suppused to take a screenshot of a specific window using ImageGrab(PIL), however the screenshot is not acurrate and I have no Idea why.
here is a screenshot the code took(both not the right size and not in the right position)
and this is the code
import pygetwindow as gw
import PIL.ImageGrab
from pynput.mouse import Button, Controller
mouse = Controller()
from pynput.keyboard import Key, Controller
keyboard = Controller()
chosenWindow = gw.getWindowsWithTitle('Discord')[0]
print(chosenWindow.topleft)
print(chosenWindow.bottomright)
Im = PIL.ImageGrab.grab(bbox=(chosenWindow.topleft[0],chosenWindow.topleft[1],chosenWindow.bottomright[0],chosenWindow.bottomright[1]))
Im.show()
so I was trying to make an auto clicker, the other day and for some reason, when I try to activate it with the toggle_key ("t") the program doesn't start. I was following this tutorial https://www.youtube.com/watch?v=4hdK9Gey76E and it seems to work in the video. Can anyone explain why the program doesn't start when I turn it on?
import time
import threading
from pynput.mouse import Controller, Button
from pynput.keyboard import Listener, KeyCode
TOGGLE_KEY = KeyCode(char="t")
clicking = False
mouse = Controller()
def clicker():
while True:
if clicking:
mouse.click(Button.left, 1)
time.sleep(0.001)
def toggle_event(key):
if key == TOGGLE_KEY:
global clicking
clicking = not clicking
click_thread = threading.Thread(target=clicker())
click_thread.start()
with Listener(on_press=toggle_event) as Listener:
Listener.join()
I am creating a pyautogui automation that connect to a W3270 terminal (really old :))
this terminal is expecting pause key to be pressed,
Apart pyautogui, I also tried Keyboard library, but i am unable to send pause
import pyautogui
import keyboard
import constants as const
locateOnScreen(const.IPAIRE_TERMINAL_CONNECTED)
command = '/FOR SIGNON'
pause = '\u0019'
pyautogui.write(command)
time.sleep(1)
keyboard.send('pause')
Am I suppose to use keyboard to simulate 'pause' button?
I found a solution using pynput
import pyautogui
import constants as const
from pynput.keyboard import Key, Controller
locateOnScreen(const.IPAIRE_TERMINAL_CONNECTED)
command = '/FOR SIGNON'
pyautogui.write(command)
time.sleep(1)
keyboard = Controller()
keyboard.press(Key.pause)
keyboard.release(Key.pause)
The default trigger of failsafe pyautogui is drag mouse to up-left corner. if i do that then the program will stop. How to change trigger, instead drag mouse i want set the trigger with keyboard input. for example if i press P key then the program will stop (the failsafe executed)
There isnt a way using pyautogui but it has been done using pynput. here is an example (pay attention to def keypress specifiaclly)
from random import random
from threading import Thread
from time import sleep
from pynput.mouse import Controller, Button
from pynput.keyboard import KeyCode, Listener
import pyautogui
delay = float(pyautogui.confirm(text='Choose Clicking Speed Per Second', title='Spook AutoClicker', buttons=['0.5','0.001']))
mouse = Controller()
class AutoClicker(Thread):
clicking = False
def run(self):
while True:
if AutoClicker.clicking:
mouse.click(Button.left)
sleep(delay)
def keypress(key):
if key == KeyCode(char="p"):
AutoClicker.clicking = not AutoClicker.clicking
AutoClicker().start()
with Listener(on_press=keypress) as listener:
listener.join()
I was testing this functions for another python script, but when I executed them I got an error
from pynput.keyboard import Key, Controller
from pynput.mouse import Button, Controller
import webbrowser
import time
def moveMouseAndClick():
url = "Secret"
webbrowser.open(url)
mouse = Controller()
time.sleep(4.5)
mouse.position = (1094.59, 153.52)
time.sleep(1)
mouse.press(Button.left)
mouse.release(Button.left)
def login():
keyboard = Controller()
keyboard.type("Secret")
keyboard.press(Key.enter)
keyboard.release(Key.enter)
moveMouseAndClick()
time.sleep(2)
login()
output:
TypeError: AttributeError: 'Controller' object has no attribute 'type'
So I made a few changes, but I got another error
from pynput.keyboard import Controller as Ctrll_1
from pynput.mouse import Controller as Ctrll_2
from pynput.keyboard import Key
from pynput.mouse import Button
import webbrowser
import time
def moveMouseAndClick():
url = "Secret"
webbrowser.open(url)
mouse = Ctrll_2
time.sleep(4.5)
mouse.position = (1094.59, 153.52)
time.sleep(1)
mouse.press(Button.left)
mouse.release(Button.left)
def login():
keyboard = Ctrll_1
keyboard.type("Secret")
keyboard.press(Key.enter)
keyboard.release(Key.enter)
moveMouseAndClick()
time.sleep(2)
login()
output:
TypeError: press() missing 1 required positional argument: 'button'
I'm a beginner in python so I'm completely lost, any help or advice will be great thanks!
The reason for that error is it's because mouse.Controller which in this case is Ctrll_2
is a class variable not a function so you should've defined initiated it before using it.
the problem will happen at keyboard.Controller which is Ctrll_1
here is the fixed code that should be working fine in your case
from pynput.keyboard import Controller as Ctrll_1
from pynput.mouse import Controller as Ctrll_2
from pynput.keyboard import Key
from pynput.mouse import Button
import webbrowser
import time
def moveMouseAndClick():
url = "http://google.com"
webbrowser.open(url)
mouse = Ctrll_2()
time.sleep(4.5)
mouse.position = (1094.59, 153.52)
time.sleep(1)
mouse.press(Button.left)
mouse.release(Button.left)
def login():
keyboard = Ctrll_1()
keyboard.type("password")
keyboard.press(Key.enter)
keyboard.release(Key.enter)
moveMouseAndClick()
time.sleep(2)
login()
in mouse.press(Button.left), you should insert all button arguments. https://pypi.org/project/mouse/