Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.python Subject: Re: Keypress Input Date: Tue, 16 Jun 2015 14:22:38 -0700 Organization: A noiseless patient Spider Lines: 14 Message-ID: <87r3pbs601.fsf@jester.gateway.sonic.net> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="6ccd71891c5af5a40d6c4fb25cbad45d"; logging-data="1651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/549hZZ2MmmE45mcLykxQh" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:vtlwTjTojLgn5cuopfN3lw0Qc2Y= sha1:QMCMW/+kZg12ZHasdVaot1h+7+Y= Xref: csiph.com comp.lang.python:92702 John McKenzie writes: > Would like a set-up where something happens when a key is pressed. Not > propose a question, have the user type something, then hit return, then > something happens, but just the R key is pressed, something happens, The quick answer is that you want raw mode tty input. Type "stty raw" to the shell, and sys.stdin.read(1) will give you the bytes of the typed characters immediately. Use "stty -raw" to put it back in cooked mode, or maybe better, "stty sane" to restore normal settings in general. Yes there are messy details like function keys actually sending several characters. In practice you'll also want to control the tty modes from your program instead of the shell, but that should get you started. You may also want to turn off echo, i.e. "stty raw -echo".