Comand from keyboard

This is the place for queries that don't fit in any of the other categories.

Comand from keyboard

Postby Venator » Tue May 07, 2013 11:34 am

Hi all!!
I need some help.. I'd like to know if there's a library, a code that allow me to do something untill a key on the keyboard is pressed??
I mean: i'd like to tur on a led (connected to the gpio ofmy raspberry) untill i keep pressing enter

Is there a way to do that in python or i need pygame?? If so, how can i do it??
Venator
 
Posts: 3
Joined: Tue May 07, 2013 11:24 am

Re: Comand from keyboard

Postby Mekire » Tue May 07, 2013 12:43 pm

Unfortunately the pygame event queue doesn't work without an active pygame display, so you won't find a solution there (unless of course you plan on having an active pygame window).

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Comand from keyboard

Postby setrofim » Tue May 07, 2013 12:52 pm

Unless I'm missing something, the builtin raw_input() (input() in Python 3) will do what you want -- it will pause the script execution until the user presses enter.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Comand from keyboard

Postby Mekire » Tue May 07, 2013 12:58 pm

setrofim wrote:Unless I'm missing something, the builtin raw_input() (input() in Python 3) will do what you want -- it will pause the script execution until the user presses enter.
I was making the assumption that he at least needed to do some sort of processing in the mean time but you may be right; maybe he just wants a light switch. :D

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Comand from keyboard

Postby setrofim » Tue May 07, 2013 1:10 pm

Mekire wrote:I was making the assumption that he at least needed to do some sort of processing in the mean time

If that's the case, the processing can be done in a background thread.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Comand from keyboard

Postby Venator » Tue May 07, 2013 6:33 pm

What i want is to turn on and keep on a led untill a preset key is pressed.. Foe example:
Untill K_l is pressed led is on.. Are there any libreries that allow me to do this??
Venator
 
Posts: 3
Joined: Tue May 07, 2013 11:24 am

Re: Comand from keyboard

Postby metulburr » Tue May 07, 2013 9:22 pm

You mean something like termios?

Linux:
Code: Select all
import termios, sys, os
TERMIOS = termios

def getkey():
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
    new[6][TERMIOS.VMIN] = 1
    new[6][TERMIOS.VTIME] = 0
    termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
    c = None
    try:
        c = os.read(fd, 1)
    finally:
        termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
    return c

while True:
   k = getkey()
   print(k)



windows:
Code: Select all
import msvcrt

def getkey():
    return msvcrt.getch()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1412
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Comand from keyboard

Postby Venator » Wed May 08, 2013 12:36 pm

Well.. Now, how can i set an action to a key??
es: if i press "l" program turn on led1
If i press "k" programm turn on led2


than i tried the program that you posted but i've this error:

pi@raspberrypi ~ $ sudo python key2.py
File "key2.py", line 12
new[6][TERMIOS.VMIN] = 1
^
SyntaxError: invalid syntax
Venator
 
Posts: 3
Joined: Tue May 07, 2013 11:24 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests