Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

getchar() in python

Name: Anonymous 2009-03-13 20:23

is it possible?

Name: Anonymous 2009-03-13 20:25

unpossible

Name: Anonymous 2009-03-13 20:27

sys.stdin.read(1)

Name: Anonymous 2009-03-13 20:50

>>3
still have to press enter

Name: Anonymous 2009-03-13 21:02

>>1
forget it, it's NP-HARD

Name: Anonymous 2009-03-13 21:31

Write a DLL in C with getchar() exportable and import it in Python or something.

Name: Anonymous 2009-03-13 21:32

>>4
Just like getchar.

Name: Anonymous 2009-03-13 23:30

>>7
I thought getchar was the single character without enter one?

Name: Anonymous 2009-03-13 23:33

The Doryen Library has a getchar and putchar function, you could have a look at that. http://thedoryenlibrary.appspot.com/

But I think it's pretty much a C library wrapped to Python.

Name: Anonymous 2009-03-13 23:40

>>8
That's getch, and it's not standard.

Name: Anonymous 2009-03-14 0:17

I prefer getchu, if you catch my drift.

Name: Anonymous 2009-03-14 4:39

>>11
Gesundheit!

Name: Anonymous 2009-03-14 6:18

ncurses has a getch

Name: Anonymous 2009-03-14 6:18

maybe you can do a fcntl or whatever on STDIN to make it unbuffered

Name: Anonymous 2009-03-14 7:53

>>13
You can't call ncurses' getch function unless you do all the curses initialization stuff and set up a window.
>>14
Still wouldn't help -- after all, getchar only receives the input after you actually press Enter.

Name: Anonymous 2009-03-14 9:02

>>14
I know. I didnt say you didn't have to.
>>15
NOT IF ITS UNBUFFERED

Name: Anonymous 2009-03-14 9:11

>>16
HELLO! THIS IS THE SHIFT KEY -> [ SHIFT ]!
HOLD IT DOWN WHEN YOU WANT TO SOUND SMARTER!

USE IT OFTEN!

Name: Anonymous 2009-03-14 9:13

>>16
Alright, you were right. I must have confused this with nonblocking mode.

In any case, enjoy my ENTERPRISE CROSS-PLATFORM implementation for getch.

def getch():
    """ Read a character without actually pressing Enter. """
    import sys
    if sys.platform == 'win32':
        # Enjoy your AIDS
        import ctypes
        # Set up functions, constants, etc
        kernel32 = ctypes.windll.kernel32
        HANDLE = ctypes.c_void_p
        DWORD = ctypes.c_ulong
        LPDWORD = ctypes.POINTER(ctypes.c_ulong)
        LPVOID = ctypes.c_void_p
        # GetStdHandle
        STD_INPUT_HANDLE = ctypes.c_ulong(-10)
        GetStdHandle = kernel32.GetStdHandle
        GetStdHandle.argtypes = [DWORD]
        GetStdHandle.restype = HANDLE
        # GetConsoleMode
        ENABLE_ECHO_INPUT = 0x4L
        ENABLE_LINE_INPUT = 0x2L
        GetConsoleMode = kernel32.GetConsoleMode
        GetConsoleMode.argtypes = [HANDLE, LPDWORD]
        GetConsoleMode.restype = bool
        # SetConsoleMode
        SetConsoleMode = kernel32.SetConsoleMode
        SetConsoleMode.argtypes = [HANDLE, DWORD]
        SetConsoleMode.restype = bool
        # ReadConsole
        ReadConsole = kernel32.ReadConsoleW
        ReadConsole.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPVOID]
        ReadConsole.restype = bool
        # Get console handle
        handle = GetStdHandle(STD_INPUT_HANDLE)
        if long(handle) == -1L:
            raise WindowsError, 'Unable to obtain STDIN handle'

        # Store old mode
        oldmode = DWORD()
        success = GetConsoleMode(handle, ctypes.byref(oldmode))
        if not success:
            raise WindowsError, 'Unable to get console mode'

        try:
            # Change mode
            newmode = oldmode.value & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)
            success = SetConsoleMode(handle, newmode)
            if not success:
                raise WindowsError, 'Unable to set console mode'

            # Read input
            buffer = ctypes.create_unicode_buffer('', 1)
            chars_read = DWORD()
            success = ReadConsole(handle, ctypes.byref(buffer), 1, ctypes.byref(chars_read), None)
            if not success:
                raise WindowsError, 'Console read failed'
        finally:
            SetConsoleMode(handle, oldmode)

        return buffer.value

    else:
        import termios
       
        oldflags = termios.tcgetattr(sys.stdin.fileno())
        newflags = list(oldflags)
        # Set non-canonical mode
        newflags[3] &= ~(termios.ICANON | termios.ECHO)
        try:
            termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, newflags)
            chars = sys.stdin.read(1)
        finally:
            termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, oldflags)
       
        return chars.decode()

Name: Anonymous 2009-03-14 19:48

You know, there's already a working getch implementation in the pywin32 package.

Name: Anonymous 2009-03-15 11:17

>>19
where is it then?

Name: Anonymous 2009-03-15 11:45

>>18
Not enough NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL.

Name: CMushroom 2010-10-09 4:50

>>18
I was searching for a way to get the same effect of getch but when I saw what heresy you had to do to get it done in Python, I think I'm gonna stick to a C approach.

Name: Anonymous 2010-10-09 6:48

>>22

def getch():
    import os
    if os.name == 'nt':
        import msvcrt
        return msvcrt.getch()
    else:
        # What >>18 said.


Also, way to bump a five year old thread.

Name: Anonymous 2010-10-09 9:34

>>23
Python had a msvcrt module? Neat.

Name: Anonymous 2010-12-06 9:20

Back to /b/, ``GNAA Faggot''

Name: Anonymous 2011-02-03 4:11

Name: fdgf 2011-03-22 9:42

54

Name: Anonymous 2011-03-22 10:51

>>30
Way to bump a five years old thread.

Don't change these.
Name: Email:
Entire Thread Thread List