Archive keyboard entries in Python

Asked

Viewed 76 times

0

I am building a code to retain any command made by the keyboard, they will be published in a topic of the ROS structure to control a mobile robot. I’m not very familiar with Python, the most I could about data storage was the code below... For example, let’s assume that the directional to the left is typed, I need to store that it was pressed to, just then, inform the robot that it must move in such a way.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
arq = open('/home/lucas/Área de Trabalho/robosmoveis2018/dados.txt', 'w')
text = """
Lista 
---
1
2
3
"""
arq.write(text)
arq.close()
  • Your question will probably be closed, but in the meantime I’ll leave this link, goes from the basic concepts to what you want to do, good luck ;) And I recommend that you do the tour

  • But what is the content of this dados.txt ? If it is text commands I don’t see how your question is different from a "how to read content from a python file ?" , which already has several. And your question is to build the archive or read ? Still very confused and unclear the question. Be as objective as possible in what you’ve already done, in what you need to do, and how you need what you’ve done.

1 answer

2


One of the easiest things to use in Python to read the keyboard directly is the library "pygame".

With it it is possible to get information about the keystrokes pressed in real time (without having to wait for the user to type "enter") and get information about the arrow keys and others, which using only the terminal I/O is very complicated to do.

Pygame would be nice because besides giving you control over the keyboard reading and a simple interface to draw on a window, it leaves you in full control of the program - so the same program that reads the keyboard can exchange data with the external device (the robot, in this case), and even draw some feedback in the window, to the program user.

But you’ll need to learn a minimum of Python to be able to do things - it’s an easy complexity project for the average, and you won’t be able to solve it by asking for every step, or by trial and error. If you read English I suggest following the official tutorial in python.org -otherwise, see the documentation in English/translated here: https://wiki.python.org.br/DocumentacaoPython

Browser other questions tagged

You are not signed in. Login or sign up in order to post.