⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15_07.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 15.6. Reading from the Keyboard (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:19Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"><LINKREL="prev"HREF="ch15_06.htm"TITLE="15.5. Changing Text Color"><LINKREL="next"HREF="ch15_08.htm"TITLE="15.7. Ringing the Terminal Bell"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_06.htm"TITLE="15.5. Changing Text Color"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.5. Changing Text Color"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_08.htm"TITLE="15.7. Ringing the Terminal Bell"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.7. Ringing the Terminal Bell"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-24365">15.6. Reading from the Keyboard</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-561">Problem <ACLASS="indexterm"NAME="ch15-idx-1000006231-0"></A></A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch15-idx-1000005087-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005087-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005087-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005087-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005087-4"></A><ACLASS="indexterm"NAME="ch15-idx-1000005087-5"></A>You want to read a single character from the keyboard. For instance, you've displayed a menu of one-character options, and you don't want to require the user to press Enter to make their selection.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-567">Solution</A></H3><PCLASS="para">Use the CPAN module Term::ReadKey to put the terminal into <CODECLASS="literal">cbreak</CODE> mode, read characters from STDIN, and then put the terminal back into its normal mode:</P><PRECLASS="programlisting">use Term::ReadKey;ReadMode 'cbreak';$key = ReadKey(0);ReadMode 'normal';</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-583">Discussion</A></H3><PCLASS="para">Term::ReadKey can put the terminal into many modes &nbsp;-  <CODECLASS="literal">cbreak</CODE> is just one of them. <CODECLASS="literal">cbreak</CODE> mode makes each character available to your program as it is typed (see <ACLASS="xref"HREF="ch15_07.htm#ch15-31922"TITLE="sascii">Example 15.1</A>). It also echoes the characters to the screen; see <ACLASS="xref"HREF="ch15_11.htm"TITLE="Reading Passwords">Recipe 15.10</A> for an example of a mode that does not echo.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch15-31922">Example 15.1: sascii</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch15-idx-1000004998-0"></A>sascii - Show ASCII values for keypressesuse Term::ReadKey;ReadMode('cbreak');print &quot;Press keys to see their ASCII values.  Use Ctrl-C to quit.\n&quot;;while (1) {    $char = ReadKey(0);    last unless defined $char;    printf(&quot; Decimal: %d\tHex: %x\n&quot;, ord($char), ord($char));}ReadMode('normal');</PRE></DIV><PCLASS="para">Using <CODECLASS="literal">cbreak</CODE> mode doesn't prevent the terminal's device driver from interpreting end-of-file and flow-control characters. If you want to be able to read a real Ctrl-C (which normally sends a <CODECLASS="literal">SIGINT</CODE> to your process) or a Ctrl-D (which indicates end-of-file under Unix), you want to use <CODECLASS="literal">raw</CODE> mode.</P><PCLASS="para">An argument of <CODECLASS="literal">0</CODE> to <CODECLASS="literal">ReadKey</CODE> indicates that we want a normal read using <CODECLASS="literal">getc</CODE>. If no input is available, the program will pause until there is some. We can also pass <CODECLASS="literal">-1</CODE> to indicate a non-blocking read, or a number greater than <CODECLASS="literal">0</CODE> to indicate the number of seconds to wait for input to become available; fractional seconds are allowed. Non-blocking reads and timed-out reads return either <CODECLASS="literal">undef</CODE> when no input is available or a zero-length string on end of file.</P><PCLASS="para">Recent versions of Term::ReadKey also include limited support for non-Unix systems.<ACLASS="indexterm"NAME="ch15-idx-1000005089-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005089-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005089-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005089-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005089-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-623">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch03_043.htm"><CODECLASS="literal">getc</CODE></A> and <ACLASS="olink"HREF="../prog/ch03_166.htm"><CODECLASS="literal">sysread</CODE></A> functions in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>, and in <ICLASS="filename">perlfunc </I>(1); the documentation for the Term::ReadKey module from CPAN; <ACLASS="xref"HREF="ch15_09.htm"TITLE="Using POSIX termios">Recipe 15.8</A>; <ACLASS="xref"HREF="ch15_10.htm"TITLE="Checking for Waiting Input">Recipe 15.9</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_06.htm"TITLE="15.5. Changing Text Color"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.5. Changing Text Color"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_08.htm"TITLE="15.7. Ringing the Terminal Bell"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.7. Ringing the Terminal Bell"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.5. Changing Text Color</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">15.7. Ringing the Terminal Bell</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -