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

import curses

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

    while True:
        char = screen.getch()
        #if 'q' is pressed 
        if char == ord('q'):
         #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:
     #When 'q' is pressed and program ends 

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…

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…

Python Ethereum Block Chain Interaction with Web3

This tutorial will show how to interact with the Ethereum blockchain using Python. To start, the Python library Web3 will need to be installed. The Web3 library allows you to interact with a local or remote Ethereum node, using a HTTP or IPC connection. Using Web3 you will be able to create smart contracts on…

Leave a Reply

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

You are commenting using your 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