【发布时间】:2016-07-06 22:08:32
【问题描述】:
在 Windows 上的 Eiffel 中是否有来自 .NET 的 Console.ReadKey 之类的东西?
我需要一种无需等待用户按 Enter 即可从控制台读取输入的方法。
函数io.read_character 无法使用,因为它会阻塞,直到用户按 Enter 键。
【问题讨论】:
标签: console console-application eiffel
在 Windows 上的 Eiffel 中是否有来自 .NET 的 Console.ReadKey 之类的东西?
我需要一种无需等待用户按 Enter 即可从控制台读取输入的方法。
函数io.read_character 无法使用,因为它会阻塞,直到用户按 Enter 键。
【问题讨论】:
标签: console console-application eiffel
正如关于 SO(here 或 here)以及 elsewhere 的答案中所解释的,没有无需等待即可从控制台读取字符的便携式方法。但是,您可以通过连接 Eiffel 的外部代码轻松使用链接中列出的任何方法。以下示例演示了如何在 Windows 上执行此操作:
read_char: CHARACTER
-- Read a character from a console without waiting for Enter.
external "C inline use <conio.h>"
alias "return getch ();"
end
然后可以从您的代码中调用功能read_char 作为常规功能:
from
io.put_string ("Press q or Esc to exit.")
io.put_new_line
until
c = 'q' or c = '%/27/'
loop
c := read_char
io.put_string ("You pressed: ")
if c = '%U' or c = '%/224/' then
-- Extended key is pressed, read next character.
c := read_char
io.put_string ("extended key ")
io.put_natural_32 (c.natural_32_code)
else
io.put_character (c)
end
io.put_new_line
end
【讨论】: