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
Related
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()
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()
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()
i cant find a solution for the following problem:
I would like to record keystrokes and mousemovement at the same time. Right now i tried to combine the scripts from the pynput Package Documentation.
Monitoring the mouse: https://pynput.readthedocs.io/en/latest/mouse.html#monitoring-the-mouse
Monitoring the keyboard:
https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard
from pynput import mouse
from pynput import keyboard
def on_move(x, y):
print('Pointer moved to {0}'.format(
(x, y)))
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
def on_scroll(x, y, dx, dy):
print('Scrolled {0}'.format(
(x, y)))
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with mouse.Listener(
on_move=on_move,
on_click=on_click,
on_scroll=on_scroll) as listener:
listener.join()
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
Right now the keyboard listener only starts after the mouse listener gets closed. Is there any way to record mouse and keyboard at the same time? Are there better site-packages for this? Thanks a lot in advance!
All you need is this, my friend
# Collect events until released
with keyboard.Listener(on_release=on_release) as k_listener, mouse.Listener(on_click=on_click) as m_listener:
k_listener.join()
m_listener.join()
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.