Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect in python which keys are pressed

I need to know which key is being pressed right now. I'm not looking to capture some specific keys to trigger an event or anything like that,

I want to know which keys are pressed now and display a list of them.

I also need to capture special keys like F1 ... F12, shift, alt, home, windows, etc. Basically all keys on the keyboard.

How do I do this in python? How do I capture keyboard events?

Related

  • Cross platform keylogger
  • Is there a cross-platform python low-level API to capture or generate keyboard events?

EDIT

Just so you know I'm not trying to make a keylogger. I'm trying to make a diagnoses tool (I split water on my laptop and the keyboard is starting to get crazy sometime!!)

like image 368
hasen Avatar asked Mar 29 '09 08:03

hasen


People also ask

How do I know which key is pressed?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.

How do I capture a keyboard input in Python?

Use the input() function to get Python user input from keyboard. Press the enter key after entering the value. The program waits for user input indefinetly, there is no timeout. The input function returns a string, that you can store in a variable.

How do I know if a pygame key was pressed?

To know which key was pressed, we have to check the event. key variable corresponds to which pygame keys. For example, the pygame key for the letter “A” is “K_a” then we will compare event. Key with K a and if it comes to be same that means the key “A” was pressed.


2 Answers

PyKeylogger mentioned in the related question might do the job.

like image 197
jfs Avatar answered Sep 20 '22 16:09

jfs


The easiest way to do something like this, if you're not too fussy, is to bring in a GUI toolkit such as pygame or wxPython. For example, run the wxPython Demo, then go to the demo for KeyEvents.

like image 21
John Fouhy Avatar answered Sep 19 '22 16:09

John Fouhy