Click detection + Command: Pynput - python

So, I'm working on something with pynput and I want it so every time I click it prints a different word like:
Click:
Print("HI")
Click:
Print("WHY")
Click:
Print("BYE")
But the code I'm using is:
def on_click(x, y, button, pressed):
print(c.format(
print("HI - Clicked")
if pressed else
print("HI - Unclicked"),
(x, y)))
if not pressed:
# Stop listener
return False
with Listener(
on_click=on_click) as listener:
listener.join()
So after it says ("HI - Unclicked") and you click again I want it to say ("WHY - Clicked") and ("WHY - Unclicked") If anyone knows how PLEASEEE Reply, EDIT: And please show me how to play an audio for the click and unclick with the list like: ['food.wav', 'click.wav'] Thank you.

You just keep a list of your greetings and rotate through them.
greets = ["HI", "WHY", "BYE"]
def on_click(x, y, button, pressed):
greet = greets[0]
if pressed:
print(greet, "- Clicked", x, y )
else:
print(greet, "- Unclicked", x, y )
greets.append(greets.pop(0))
with Listener(on_click=on_click) as listener:
listener.join()
Update
greets = ["gunshot.wav", "you.wav", "cool.wav"]
def on_click(x, y, button, pressed):
greet = greets[0]
if pressed:
pg.mixer.Sound(greet).play()
print(greet, "- Clicked", x, y )
else:
print(greet, "- Unclicked", x, y )
greets.append(greets.pop(0))
with Listener(on_click=on_click) as listener:
listener.join()
Or even:
greets = [pg.mixer.Sound(k) for k in ("gunshot.wav", "you.wav", "cool.wav")]
def on_click(x, y, button, pressed):
greet = greets[0]
if pressed:
greet.play()
print("- Clicked", x, y )
else:
print("- Unclicked", x, y )
greets.append(greets.pop(0))
with Listener(on_click=on_click) as listener:
listener.join()

Related

Keylogger With mouse listening

I want to make a keylogger in Python that listens to the keyboard and at the same time listens to the mouse (simultaneously), the problem is that no matter what I try to do ,separately each of them works well but together it just does not work for me.
This is the code I made for the mouse listener:
from pynput.mouse import Listener
from pynput import keyboard
def writetofile(x,y):
with open('keys.txt', 'a') as file:
file.write('position of mouse: {0}\n'.format((x,y)))
def on_click(x, y, button, pressed):
if pressed:
with open('keys.txt', 'a') as file:
file.write('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
def on_scroll(x, y, dx, dy):
with open('keys.txt', 'a') as file:
file.write('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))
with Listener(on_move=writetofile,on_click=on_click, on_scroll=on_scroll) as file:
file.join()
And this is my keyboard listener:
def write_keys_to_file(keys):
with open('keys.txt', 'a') as file:
key = str(key).replace("'", "")
file.write(key)
with Listener(on_press = write_keys_to_file) as listener:
listener.join()
Would appreciate help .
Thanks in advance .
I found the solution to my problem,had to do it this way:
from pynput.keyboard import Listener as KeyboardListener
from pynput.mouse import Listener as MouseListener
def writetofile(x,y):
with open('keys.txt', 'a') as file:
file.write('position of mouse: {0}\n'.format((x,y)))
def on_click(x, y, button, pressed):
if pressed:
with open('keys.txt', 'a') as file:
file.write('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
def on_scroll(x, y, dx, dy):
with open('keys.txt', 'a') as file:
file.write('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))
def write_keys_to_file(keys):
with open('keys.txt', 'a') as file:
key = str(key).replace("'", "")
file.write(key)
with MouseListener(on_move = writetofile,on_click=on_click, on_scroll=on_scroll) as listener:
with KeyboardListener(on_press=on_press) as listener:
listener.join()

pynput.mouse Listener do not stop

Here is my prank code for my friend but I can not stop this program from keyboard when I make executable.
How can I stop this listener from keyboard shortcut. I know it is thread issue.
import sys
import pyautogui
import keyboard
from pynput.mouse import Listener
from threading import Thread
global x,y
def on_move(x, y):
print ("Mouse moved to ({0}, {1})".format(x, y))
def on_click(x, y, button, pressed):
try: #FOR AUTO FAIL-SAFE
if pressed:
print ('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
pyautogui.move(x+100,y+100)
except:
pass
def on_scroll(x, y, dx, dy):
print ('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))
with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
listener.join()
while True:
if keyboard.is_pressed("q"):
sys.exit(0)
break
So here is my fix. I works because I had the same problem:
Insert:
from pynput.keyboard import Key
then:
In on_move(), on_click(), and on_scroll() add:
if key == Key.f10:
sys.exit()
This will check that when you press f10 (or you can change), the program will exit
You also need to pass key as an argument!
The full updated code is:
import sys
import pyautogui
import keyboard
from pynput.keyboard import Key
from pynput.mouse import Listener
from threading import Thread
global x,y
def on_move(key, x, y):
print ("Mouse moved to ({0}, {1})".format(x, y))
if key == Key.f10:
sys.exit()
def on_click(key, x, y, button, pressed):
try: #FOR AUTO FAIL-SAFE
if pressed:
print ('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
pyautogui.move(x+100,y+100)
except:
pass
if key == Key.f10:
sys.exit()
def on_scroll(key, x, y, dx, dy):
print ('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))
if key == Key.f10:
sys.exit()
with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
listener.join()
while True:
if keyboard.is_pressed("q"):
sys.exit(0)
break
I solved this problem. When mouse position(x,y) on left-top and mid scrolled. Program finishes itself.
def on_scroll(x, y, dx, dy):
print('Scrolled {0} at {1}'.format(
'down' if dy < 0 else 'up',
(x, y,dx,dy)))
if x<100 and y<100:
return False

How does "with mouse.Listener" work in pynput?

I'm trying to understand an example from the pynput documentation.
I understand the general gist, that the code prints a message when the mouse moves/is clicked/etc. I'm just confused about how it does so.
def on_click(x, y, button, pressed):
print('{0} at {1}'.format(
'Pressed' if pressed else 'Released',
(x, y)))
if not pressed:
# Stop listener
return False
with mouse.Listener(
on_move=on_move,
on_click=on_click,
on_scroll=on_scroll) as listener:
listener.join()

How to detect what button was clicked? (Pynput)

I'm trying to detect what mouse button was clicked
so here is my code:
from pynput.mouse import Listener
def on_click(button, pressed):
if button.Left and pressed:
print("You pressed the left mouse button")
if button.Right and pressed:
print("You pressed the right mouse button")
so there was no errors but It's not working any Ideas?
From the Documentation Here
CODE
from pynput import mouse
def on_move(x, y):
print('Pointer moved to {0}'.format(
(x, y)))
def on_click(x, y, button, pressed):
print(button) # Print button to see which button of mouse was pressed
print('{0} at {1}'.format(
'Pressed' if pressed else 'Released',
(x, y)))
# Collect events until released
with mouse.Listener(
on_click=on_click
) as listener:
listener.join()
# ...or, in a non-blocking fashion:
listener = mouse.Listener(on_click=on_click)
listener.start()
As you can see, the button parameter in the function on_click tells you which button was pressed.
EDIT:
Here is how you may handle action based on which button of the mouse was pressed
def on_click(x, y, button, pressed):
btn = button.name
if btn == 'left':
print('Left if Pressed')
# ====== < Handle Pressed or released Event ====== > #
if pressed:
print('Do somethin when Pressed with LEft')
else:
print('LEFT is Released')
elif btn == 'right':
print('Right BTN was pressed ')
# ====== < Handle Pressed or released Event ====== > #
if not pressed:
print('right Button is released')
else:
pass
I post a second answer because of a different nature of the issue found on code here
The issue is how you call the imports.
CORRET CODE
from pynput import mouse
mouse_ = mouse.Controller()
button = mouse.Button
def on_click(x, y, button, pressed):
btn = button.name
if btn == 'left':
if pressed:
mouse_.click(button_.left)
print('works')
with mouse.Listener(
on_click=on_click
) as listener:
listener.join()
Copy and Paste the code without changes.
WARNING
when I use button.left my pc becomes extremely lagy so i suggest you to don't use is unless you know what you are doing

pynput mouse position coordinate to variable and use of variable on a global scale

I'm trying to save the mouse's x and y coordinates when the mouse button is pressed and, separately, when it is released. I am able to print them but unable to save them to variables.
Here's what I got:
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
print('{0} at {1}'.format(
'Pressed' if pressed else 'Released',
(x, y)))
if not pressed:
# Stop listener
return False
with Listener(on_click=on_click) as listener:
listener.join()
And then how would I call these variables on a global scale to use with another module (e.g. pyautogui?)
You've got pretty much everything in place, so I only had to add a few lines. Globals aren't the very best way to do things, but since this program isn't too complex, they will do the job.
The initial values of down_x, etc. don't really matter, as they will be overwritten, but they have to be there, or Python will throw an error.
#if you want to delay between mouse clicking, uncomment the line below
#import time
from pynput.mouse import Listener
import pyautogui
down_x = down_y = up_x = up_y = -1
def on_click(x, y, button, pressed):
global down_x
global down_y
global up_x
global up_y
if pressed:
(down_x, down_y) = (x, y)
else:
(up_x, up_y) = (x, y)
return False
with Listener(on_click=on_click) as listener:
listener.join()
print("Mouse drag from", down_x, ",", down_y, "to", up_x, ",", up_y)
# you may wish to import the time module to make a delay
#time.sleep(1)
pyautogui.mouseDown(down_x, down_y)
#time.sleep(1)
pyautogui.mouseUp(up_x, up_y)
Use global variables:
from pynput.mouse import Listener
xx, yy = 0, 0
def on_click(x, y, button, pressed):
global xx, yy
xx, yy = x, y
print('{0} at {1}'.format(
'Pressed' if pressed else 'Released',
(x, y)))
if not pressed:
# Stop listener
return False
with Listener(on_click=on_click) as listener:
listener.join()
# here you can read xx and yy
If your code gets more complicated you can consider wrapping it up in a class.

Categories

Resources