Python 3 Pynput Library Script to Detect Which Keyboard Key is Pressed and Display Info in Terminal
126 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-pynput-library-script-to-detect-which-keyboard-key-is-pressed-and-display-info-in-terminal/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
script which will actually detect which
0:06
keyboard key that you have pressed and
0:09
it will actually print it out as well so
0:11
there is either two packages you can use
0:14
inside Python the first package is very
0:17
uh popular here so just go to this
0:19
website and just search for this package
0:22
which is pip install py n pu so this
0:27
will actually monitor and control your
0:29
user input devices such as your keyboard
0:31
such as your mouse so it will detect
0:34
which keyboard key that you are pressing
0:35
is inside this uh module it becomes easy
0:39
after installing it just create a simple
0:41
Python script and just first of all
0:45
require this module using
0:48
this keyboard and from this you need to
0:51
import the key and also the
0:55
listener so after importing it we will
0:59
uh
1:01
actually call a function right here
1:03
using this listener and inside this you
1:07
it takes an argument here which is on
1:09
press so on press whenever you press any
1:12
sort of key inside your keyboard this
1:14
function will
1:15
execute as
1:18
listener and listener dot join so now we
1:22
just need to define this custom function
1:25
which is which will execute as soon as
1:27
you press the keyboard key on your
1:30
keyboard here so right here we just need
1:33
to define this function which is show
1:37
and this key will automatically be
1:40
passed right here as soon as you press
1:42
the key on your keyboard and right here
1:45
we can show the information that which
1:47
key that you have pressed inside this
1:50
very
1:53
simply so you can simply print out the
1:56
message that you have entered this key
2:00
and
2:02
just simply print out which key has been
2:06
pressed by the users like
2:09
this so if I just execute the script
2:12
right here in the terminal just write
2:13
here python app py and after that I just
2:17
press any sort of key here you will see
2:20
it is listening for all those events and
2:22
it is just printing out that you have
2:24
entered this much of
2:27
key so it's acting as a
2:30
listener for detecting any sort of
2:32
keyboard events so whatever you are
2:35
typing the keyboard key it is detecting
2:37
it and printing it out so this is really
2:40
helpful in those scenarios if you want
2:42
to detect any keyboard code input from
2:45
the user so if a specific key has been
2:47
pressed by the user let's suppose if you
2:50
want to uh close out the application
2:54
if the user uh
2:57
presses the delete key
3:00
or all these keys are there so there is
3:04
also this enter key so if if the enter
3:08
key has been pressed then we simply need
3:10
to
3:12
return
3:14
false so we need to stop the listener so
3:18
now what happens again if
3:22
you just need to close this application
3:25
once again run
3:28
this so now as soon as
3:33
I So you just need to specify a correct
3:37
key here so let me say delete
3:50
key so here K is capital here so just
3:54
make sure that the K is capital and
3:57
again run this application
4:01
so as soon as you press the delete key
4:03
you will see the listener will close the
4:06
application will close so it will
4:08
actually detect the delete key so here
4:10
you can specify the event for a certain
4:13
key all the keys which are there on the
4:16
keyboard so here you can do this so this
4:19
is the first approach so there is also
4:21
the second module that you can as well
4:23
use as well so the second module is the
4:27
little bit easy so this is the keyboard
4:31
module so here you can actually run a
4:34
simple while
4:36
loop so this will also do the same thing
4:39
so here we can simply print out
4:41
whichever key has been pressed by the
4:44
keyboard by using this function read
4:46
underscore key so this will actually
4:47
return which key has been pressed by the
4:50
user so here we can listen for the
4:52
certain key event so here we can simply
4:55
say if the
4:57
keyboard read is equal to trip double
5:01
equal to a so if the keyboard key has
5:05
equal to a then we just need to break so
5:08
exit out of the application if the
5:10
keyboard key a has been pressed so now
5:14
if you
5:17
see as soon as you start the application
5:20
nothing will happen as soon as you press
5:22
the A keyword the application will
5:32
break so as you can see we are right uh
5:35
pressing all those key so as soon as I
5:38
press the A keyboard here a key here the
5:41
application closes so this is a second
5:44
way by which you can do the same thing
5:46
you can see the code is less here in
5:48
this approach so both the approaches
5:51
have popular you can just monitor your
5:55
keyboard events using these two
5:57
libraries uh thank you very much for
6:00
watching this video and also check out
6:01
my website freemediatetools.com
6:05
uh which contains uh thousands of tools
#Computers & Electronics
#Programming
#Scripting Languages