Keyboard Controls

To map our keyboard inputs we can use a python module called Tkinter.

Tkinter is Python’s de-facto standard GUI (Graphical User Interface) package. Tkinter is not the only GUI Programming toolkit of Python, however, it is the most commonly used one.

Getting Started

Start by importing the library and setting up the window.


import Tkinter as tk
window = tk.Tk()
#set the size of the window
window.resizable(width=False, height=False)
window.geometry("600x400")
#start the loop
window.mainloop()

Creating a Label

Everything from now on needs to go before the mainloop(). We are going to create a label which will sit in the window and display the keys which are pressed.


lblStroke = tk.Label(window, text="Button Press")
lblStroke.grid(row=5, column=5)

Keypress Handler

Now we have our window setup we need to create a key press event handler.


def keypress(event):
x = event.char
if x == "q":
window.destroy()
else:
#update text
lblStroke.config(text=x)
#bind all major keys and call our keypress handler
window.bind_all('', keypress)

This is a very basic introduction on how you could map your keyboard to control your robot using the W,D,S,A keys.

Keyboard Controls