I was wondering if anyone knew of a way to scan for key presses in Python. It doesn't need to detect WHAT key is being pressed, just that one is being pressed. Also it needs to work with the console.

You could use pygame.
http://pygame.org/docs/ref/event.html
Tips:
Remember to "pump" the event queue on each update.
Don't initalize a window, just use the console.

Python does not have a keypress capture so you have to use one provided by the system like termios.

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                                                                

if __name__ == '__main__':                                                      
    print "type something...'q' to quit"                                        
    s = ''                                                                      
    while 1:                                                                    
        c = getkey()                                                            
        if c == 'q':                                                            
            break                                                           
        print "captured key", c, ord(c)                                         
        s = s + c                                                               
                                                                                
    print s
commented: nice snippet +4

Nice one woooee

i think using new as a variable is not ok. I know you know that anyway that new is a reserved object.

In all, i like your sleakness . Bravo ;)

richieking:
No it is not reserved:

IDLE 2.6.6      
>>> dir(new)

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    dir(new)
NameError: name 'new' is not defined
>>>

C programmers don't like using "new" as there is a new keyword and an operator new in C++. They also insist on the redundant use of main(). Perhaps someone will write a "Python is not C" article. Until then, I will try to restrain from using variable names that make others uncomfortable. Hopefully, they will restrain from interrupting the program flow by making the reader jump back up into the program to find main() in order to start at the beginning, which is in the middle.

mmmmmm python is strange. its only on python that new has broken loose i think.

Thanks guys ;)

mmmmmm python is strange. its only on python that new has broken loose i think.

Thanks guys ;)

new is a module in the standard library.

So the question is Girbouillis

Do you recommend new to be used as a basic variable or reserved?
From python point of view.?

I strongly dont recommend that expecially as python seems still evolving. Changes here and there. Who knows.?

But i need your view Grib.!
:)

You can import new as new_module for example, if there ìs conflict.

So the question is Girbouillis

Do you recommend new to be used as a basic variable or reserved?
From python point of view.?

I strongly dont recommend that expecially as python seems still evolving. Changes here and there. Who knows.?

But i need your view Grib.!
:)

I would not recommend new as a variable name because when you think of using new as a variable, there are usually better and more explicit alternatives. The second argument is the same as yours: python changes and new is a potential future keyword (I don't think the risk is very high because if new is not a keyword in python, it's probably a deliberate choice of the python developpers).

The use of new in woooee's snippet above is reasonable: in a local scope when there are an 'old' value and a 'new' value. Why not ? 'old_state' and 'state' were other candidates.

In the end. I think its more prudent to reserve new as a keyword and using new must be avoided due to various reasons. I name a few.

1.Print has been a function in numerous prog. languages(c,c++,php,perl) but python introduced this idea in py3.
2. Python is all object variable types(python's way). Thus py variables are more than C,C++, variables. They are objects. Objects are initialized with new keyword. PYc. Foundermental python written language.
OK. This is my concern.

New stuffs that python started including in python 3 are already up and actived stuff in other languages.. or the above listed, haskell and partly lua.

Print function is something they could have included from the start but they did not see why.

New is on the way... Trust me. I have been carefuly analyzing python and its future and i think. This change is bound to happend.

Besides python is gradually moving to te top. Python could have been in place of java if all these litle flavours of python came under python with single install. eg(ironpy,numpy,pyc,matpy,pyaudio,pymol) you name it.

Java is about 140mb on my system. Due to that, java runs everywhere. what is the point of 20mb and strugle all over.?

Python is not only a prog. language . IT IS THE FUTURE !!!

That is my view. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.