Coki
Coki

Reputation: 11

How can I get a character in Python similar to Pascal readkey

In Pascal I can execute this code to get a character from keyboard input:

uses crt;
var ch: char;
begin
    ch := '.';
    while ch <> '\' do
    begin
        ch := readkey;
        writeln( ch );
    end;
end;

Is there a similar one in Python? :)

Upvotes: 1

Views: 4440

Answers (4)

retornam
retornam

Reputation: 339

import sys    

def prog():    
    char = ""     
    while char != "/":    
        char = sys.stdin.read(1)    
        print char
prog()

Upvotes: 3

erbridge
erbridge

Reputation: 1386

You could do it by running Tkinter in the background:

import Tkinter

def keyPress(event, tk):
    ch = event.char
    if ch == '\\':
        tk.destroy()
    else:
        print ch

if __name__ == '__main__':
    tk = Tkinter.Tk()
    tk.bind_all('<Key>', lambda event: keyPress(event, tk))
    tk.withdraw()
    tk.mainloop()

(Hacked from: http://www.daniweb.com/forums/post567365.html#post567365)

Upvotes: 2

9000
9000

Reputation: 40894

You can't use CRT; I recommend you to import pygame instead.

Upvotes: 0

PrettyPrincessKitty FS
PrettyPrincessKitty FS

Reputation: 6400

raw_input.

Then slice the first character.

Upvotes: 0

Related Questions