Keep getting same error, can't solve it

I am trying to control my Devastator tank, powered by a Raspberry Pi B3, PiHut MotorShield and mini WiFi keyboard. I am receiving the error setupterm cannot find keyboard. Below is my code:

import curses  
import PiMotor 
import time
import curses
import os  # added so we can shutdown OK

#Name of Individual MOTORS
m1 = PiMotor.Motor("MOTOR1",1)
m2 = PiMotor.Motor("MOTOR2",1)

#To drive all motors together
motorAll = PiMotor.LinkedMotors(m1,m2)

#Names for Individual Arrows
ab = PiMotor.Arrow(1)
al = PiMotor.Arrow(2)
af = PiMotor.Arrow(3) 
ar = PiMotor.Arrow(4)

# Get the curses window, turn off echoing of keyboard to screen, turn off     
# instant (no waiting) key response, and use special values for curser keys
screen = curses.initscr()

##This segment drives the motors in the direction listed below:
## forward and reverse takes speed in percentage(0-100)

    while True:
        char = screen.getch()
        if char == ord('q'):
        if char == ord('S'):  # Added for shutdown on capital S
            os.system ('sudo shutdown now')   # shutdown right now!
        elif char == curses.KEY_UP:
        elif char == curses.KEY_DOWN:
        elif char == curses.KEY_RIGHT:
        elif char == curses.KEY_LEFT:
        elif char == 10:

except KeyboardInterupt:
    #Close down curses properly, inc turn echo back on!
    curses.nobreak(); screen.keypad(0); curses.echo(0)

I have used “curses” with mini keyboard before, but this time I am getting the error setupterm cannot find keyboard. Can anyone help me resolve this issue?

The error setupterm cannot find keyboard is usually caused by a missing or incorrect terminal definition. To resolve this issue, you can try the following steps:

  1. Make sure you have the necessary dependencies installed. In your case, you need to install the ncurses library by running the following command:
sudo apt-get install libncurses5-dev
  1. Check if the TERM environment variable is set correctly. You can do this by running the command echo $TERM. It should output xterm or xterm-256color. If it’s not set correctly, you can set it manually by running:
export TERM=xterm
  1. If the above steps don’t work, you can try running your Python script with elevated privileges using sudo. This can sometimes help with access issues related to the keyboard. Run the script with the following command:
sudo python

By following these steps, you should be able to resolve the setupterm cannot find keyboard error and control your Devastator tank using your Raspberry Pi B3, PiHut MotorShield, and mini WiFi keyboard.