0

I'm looking for a python module that will allow me to detect keyboard events.. Now I know this module msvct, but it only works for key presses that are done in the console. I need to create a passive program that will hook to the keyboard but I cant find how..

Thank you for your help

3
  • You could use modules like tkinter or pyqt Commented Aug 13, 2014 at 18:53
  • Tried Tkinter. but it's the same as msvct, the user needs to be in the program for me to detect the keypresses. Commented Aug 13, 2014 at 19:16
  • I haven't tested it, but you might find this Python keylogger useful. Commented Aug 13, 2014 at 23:26

2 Answers 2

1

Python has a keyboard module with many features. You Can Use It In Both Shell and Console. It Also Detect Key For The Whole Windows.
Install it, perhaps with this command:

pip3 install keyboard

Then use it in code like:

import keyboard #Using module keyboard
while True:  #making a loop
    try:  #used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('a'): #if key 'a' is pressed 
            print('You Pressed A Key!')
            break #finishing the loop
        else:
            pass
    except:
        break  #if user pressed other than the given key the loop will break

You can set it to multiple Key Detection:

if keyboard.is_pressed('a') or keyboard.is_pressed('b') or keyboard.is_pressed('c'):  # and so on
    #then do this
Sign up to request clarification or add additional context in comments.

Comments

1

You can even try this code

sudo apt-get install python-xlib

Log.py

import os
import pyxhook

# This tells the keylogger where the log file will go.
# You can set the file path as an environment variable ('pylogger_file'),
# or use the default ~/Desktop/file.log
log_file = os.environ.get(
    'pylogger_file',
    os.path.expanduser('~/Desktop/file.log')
)
# Allow setting the cancel key from environment args, Default: `
cancel_key = ord(
    os.environ.get(
        'pylogger_cancel',
        '`'
    )[0]
)

# Allow clearing the log file on start, if pylogger_clean is defined.
if os.environ.get('pylogger_clean', None) is not None:
    try:
        os.remove(log_file)
    except EnvironmentError:
       # File does not exist, or no permissions.
        pass

#creating key pressing event and saving it into log file
def OnKeyPress(event):
    with open(log_file, 'a') as f:
        f.write('{}\n'.format(event.Key))

# create a hook manager object
new_hook = pyxhook.HookManager()
new_hook.KeyDown = OnKeyPress
# set the hook
new_hook.HookKeyboard()
try:
    new_hook.start()         # start the hook
except KeyboardInterrupt:
    # User cancelled from command line.
    pass
except Exception as ex:
    # Write exceptions to the log file, for analysis later.
    msg = 'Error while catching events:\n  {}'.format(ex)
    pyxhook.print_err(msg)
    with open(log_file, 'a') as f:
        f.write('\n{}'.format(msg))

This keylogger will append every key you press.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.