Using Python to capture keyboard input under Linux

Source: Internet
Author: User

How to monitor keylogger with Python under Linux

Ideas

In the previous article, we said how to use Python to get the name of the device that corresponds to an event in the/dev/input directory. Then we can know the name of the device, you can know by name which event corresponds to the keyboard (possibly more than one keyboard). Then the corresponding event under/dev/input/is the event that is responsible for processing the data entered by the keyboard.

The Evdev input event driver provides a default event handling method for the input subsystem. It receives most of the events from the underlying driver and processes them using the appropriate logic. The Evdev input event driver receives event information from the underlying and reflects it to the Sys file system, and the user program can achieve the ability to handle events by manipulating the Sys file system.

This is done using a library called Evdev, whose principle is to read the buffer array in the/DEV/EVENTX device with the C function Evdev_read (), which contains the Input_event type data, and the following kernel-escalated events will exist here.

Write the following procedure:

#!/usr/bin/env python#coding: Utf-8 fromEvdevImportInputDevice fromSelectImportSelect def detectinputkey():dev = InputDevice ('/dev/input/event4 ') while True: select ([Dev], [], []) forEventinchDev.read ():Print "code:%s value:%s"% (Event.code, Event.value)if__name__ = =' __main__ ': Detectinputkey ()

The role of select is to wait for Dev to change before running the subsequent code, Dev.read () returns the buffer array, which contains input_event type data

Use root permissions to execute the program, by observing. Its code is the input key value, its value is corresponding to its state, press a key, its value is 1, released when value is 0.

Then modify the code a little bit to know the input values and status.

Code
 def detectinputkey():dev = InputDevice ('/dev/input/event4 ') while True: select ([Dev], [], []) forEventinchDev.read ():if(Event.value = =1 orEvent.value = =0) andEvent.code! =0:Print "Key:%s Status:%s"% (Event.code,"pressed" ifEvent.valueElse "Release")

This enables the program to read the keyboard input key and its state.

Effect

To execute the program, press ZXC in turn:

If you want to monitor multiple keyboard inputs at the same time, open multiple threads to monitor it.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use Python to capture keyboard input under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.