![](https://secure.gravatar.com/avatar/8fd030b703fa8602401ff6525fa2f107.jpg?s=120&d=mm&r=g)
gquell@skequell.de wrote:
Hallo Leut,
~ versuchs mal so:
#!/usr/bin/env python # testprogramm getchar
import os, sys import termios
def getch():
~ fd = sys.stdin.fileno() ~ if os.isatty(fd):
~ 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 ~ try: ~ termios.tcsetattr(fd, termios.TCSANOW, new) ~ termios.tcsendbreak(fd,0) ~ ch = os.read(fd,7) # vorher: 7 ~ finally: ~ termios.tcsetattr(fd, termios.TCSAFLUSH, old) ~ else: ~ ch = os.read(fd,7)
~ return(ch)
if __name__ == "__main__": ~ print "Zeichen eingeben" , ~ cc=getch() ~ for j in cc: print "%x" % ord(j), # End getchar.py
Gruß Gerhard
Sorry, da war ich wohl missverständlich. Das Problem liegt nicht in dem termios-Zweig, sondern bei dem 2. read, wenn stdin nicht als tty erkannt wird ! _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
![](https://secure.gravatar.com/avatar/eeabb6287768e75cb364b51936ebba1a.jpg?s=120&d=mm&r=g)
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hallo Hartmut, ~ hier mein Test und dessen Ausgabe, danach ist alles ok: ~ echo "1234567890" | ./getchar.py ~ Zeichen eingeben 31 32 33 34 35 36 37 Gruß Gerhard - ------------- Hartmut Fröls wrote: | gquell@skequell.de wrote: | |> Hallo Leut, |> |> ~ versuchs mal so: |> |> |> #!/usr/bin/env python |> # testprogramm getchar |> |> import os, sys |> import termios |> |> def getch(): |> |> ~ fd = sys.stdin.fileno() |> ~ if os.isatty(fd): |> |> ~ 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 |> ~ try: |> ~ termios.tcsetattr(fd, termios.TCSANOW, new) |> ~ termios.tcsendbreak(fd,0) |> ~ ch = os.read(fd,7) # vorher: 7 |> ~ finally: |> ~ termios.tcsetattr(fd, termios.TCSAFLUSH, old) |> ~ else: |> ~ ch = os.read(fd,7) |> |> ~ return(ch) |> |> if __name__ == "__main__": |> ~ print "Zeichen eingeben" , |> ~ cc=getch() |> ~ for j in cc: print "%x" % ord(j), |> # End getchar.py |> |> |> Gruß Gerhard |> | Sorry, da war ich wohl missverständlich. | | Das Problem liegt nicht in dem termios-Zweig, sondern bei dem | 2. read, wenn stdin nicht als tty erkannt wird ! | | | | | | _______________________________________________ | python-de maillist - python-de@python.net | http://python.net/mailman/listinfo/python-de - -- - ------------------------------------------------------ skequell ------ ~ Gerhard Quell Software & Knowledge Engineering ~ Schützenweg 3 eMail: gquell@skequell.de Fon: 0731-26400651 ~ 89275 Elchingen web : http://www.skequell.de Fax: 0731-26400652 - --------------- pgp: 473EC53C - http://www.keyserver.net/en--------- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFBIjLnvHbZD0c+xTwRAsADAJ0VluBgakdmgigSHvIPVIVy8SPzfQCffdqf Nt2XyCtHanNY8ofp+Wscny0= =O/GZ -----END PGP SIGNATURE----- _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
![](https://secure.gravatar.com/avatar/8fd030b703fa8602401ff6525fa2f107.jpg?s=120&d=mm&r=g)
Hallo Gerhard, vielen Dank für deine Bemühungen. Anscheinend manipuliert die vorgeschaltete Warenwirtschaft die stdin so, daß ich immer nur max. 3 Byte vom read zurückbekomme, dummerweise ohne erkennbares System in 1-, 2- oder 3-Byte-Paketen. Ich habe lange gesucht aber keine Möglichkeit gefunden, eventuell Eigenschaften der stdin zu ändern (analog der flags bei os.open). Ich habe mir deshalb mit folgenden Zeilen geholfen: ... ch1 = os.read(fd,2) ch = ch1 if len(ch1) == 2 or ch1 == "\033": while 1: if ch not in key_func + key_curs + key_curs1 and len(ch) < 5: ch1 = os.read(fd,2) ch = ch + ch1 else: break ... Ist vielleicht nicht elegant, funktioniert aber solange keiner ESC drückt, dafür ist noch der "Sicherheits-Abbruch" len(ch) <= 5. Für elegantere Lösungen direkt an der stdin wäre ich aber immer noch dankbar. Gruß Hartmut Gerhard Quell schrieb:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Hallo Hartmut,
~ hier mein Test und dessen Ausgabe, danach ist alles ok: ~ echo "1234567890" | ./getchar.py ~ Zeichen eingeben 31 32 33 34 35 36 37
Gruß
Gerhard - ------------- Hartmut Fröls wrote: | gquell@skequell.de wrote: | |> Hallo Leut, |> |> ~ versuchs mal so: |> |> |> #!/usr/bin/env python |> # testprogramm getchar |> |> import os, sys |> import termios |> |> def getch(): |> |> ~ fd = sys.stdin.fileno() |> ~ if os.isatty(fd): |> |> ~ 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 |> ~ try: |> ~ termios.tcsetattr(fd, termios.TCSANOW, new) |> ~ termios.tcsendbreak(fd,0) |> ~ ch = os.read(fd,7) # vorher: 7 |> ~ finally: |> ~ termios.tcsetattr(fd, termios.TCSAFLUSH, old) |> ~ else: |> ~ ch = os.read(fd,7) |> |> ~ return(ch) |> |> if __name__ == "__main__": |> ~ print "Zeichen eingeben" , |> ~ cc=getch() |> ~ for j in cc: print "%x" % ord(j), |> # End getchar.py |> |> |> Gruß Gerhard |> | Sorry, da war ich wohl missverständlich. | | Das Problem liegt nicht in dem termios-Zweig, sondern bei dem | 2. read, wenn stdin nicht als tty erkannt wird ! | | | | | | _______________________________________________ | python-de maillist - python-de@python.net | http://python.net/mailman/listinfo/python-de
- -- - ------------------------------------------------------ skequell ------ ~ Gerhard Quell Software & Knowledge Engineering ~ Schützenweg 3 eMail: gquell@skequell.de Fon: 0731-26400651 ~ 89275 Elchingen web : http://www.skequell.de Fax: 0731-26400652 - --------------- pgp: 473EC53C - http://www.keyserver.net/en--------- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFBIjLnvHbZD0c+xTwRAsADAJ0VluBgakdmgigSHvIPVIVy8SPzfQCffdqf Nt2XyCtHanNY8ofp+Wscny0= =O/GZ -----END PGP SIGNATURE-----
_______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
-- Mit freundlichen Grüßen H. Fröls Peter Panno GmbH Abteilung IT Simon-Arzt-Str. 4 53332 Bornheim Telefon 02222/9633-53 Telefax 02222/9633-51 _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
![](https://secure.gravatar.com/avatar/7c27e514504f90a63edaefd74248a461.jpg?s=120&d=mm&r=g)
Für elegantere Lösungen direkt an der stdin wäre ich aber immer noch dankbar.
Wurde dir schon pexpect vorgeschlagen? Hilft gut bei Programmen, die eigentlich nur fuer Konsole gedacht sind und pipes nicht so recht unterstuetzen. MfG Diez _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
participants (3)
-
Diez B. Roggisch
-
Gerhard Quell
-
Hartmut Fröls