Read KeyBoard Strokes with Python

This will show how to read key strokes from a keyboard using Python. First, the python library Curses must be imported, here is a link to the curses documentation if you would like more in-depth detail. Curses is is used to read key strokes from keyboard and set different actions for each key.

In this example, the Up, Down, Left and Right keys on the keyboard will be used. When a the program is ran, what ever directional key is being pressed will be printed in the terminal. It is important to know that the python file must be executed in the computer terminal. The curses library will not run in a IDE. Use the command sudo python File_Name.py

import curses

#Get Window 
screen = curses.initscr()
# Turn off Echo 
curses.noecho()
#Instant Response 
curses.cbreak()
#Use Special Keys 
screen.keypad(True)

try:
    while True:
        char = screen.getch()
        #if 'q' is pressed 
        if char == ord('q'):
            break
         #If  UP Key is pressed 
        elif char == curses.KEY_UP:
            print("UP Key")
        #If  Down Key is pressed
        elif char == curses.KEY_DOWN:
            print("DOWN Key")
        #If  LeftKey is pressed
        elif char == curses.KEY_LEFT:
            print("LEFT Key")
        #If  Right Key is pressed
        elif char == curses.KEY_RIGHT:
            print("RIGHT Key")
         # If ENTER is pressed 
        elif char == 10:
            print("STOP")
finally:
     #When 'q' is pressed and program ends 
    curses.nocbreak();screen.keypad(0);curses.echo()
    curses.endwin()

The code above will above will print out the the direction of each directional key when that key is pressed. Curses comes with key Constance that allow you to access each key on the keyboard and give it a unique action. Note the command curses.KEY_UP . This references the curses constant for the Up directional key and once the key was pressed the action print(‘UP Key’) was given. Here is a list of the different Constants that come with the library.

Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the … Read more Android Studio Loading Animation Between Activities

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value … Read more Android Studio Tutorial SeekBar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s