在 Eiffel 控制台应用程序中读取密钥
Read key in Eiffel console application
Windows 上的 .NET 中有类似 Console.ReadKey
的内容吗?
我需要一种无需等待用户按下 Enter 即可从控制台读取输入的方法。
函数 io.read_character
无法使用,因为它会阻塞,直到用户按下 Enter。
如 SO(here or here) as well as 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
Windows 上的 .NET 中有类似 Console.ReadKey
的内容吗?
我需要一种无需等待用户按下 Enter 即可从控制台读取输入的方法。
函数 io.read_character
无法使用,因为它会阻塞,直到用户按下 Enter。
如 SO(here or here) as well as 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