2016-07-06 71 views
1

在Windows上,Eiffel中是否存在类似Console.ReadKey的问题?在埃菲尔控制台应用程序中读取密钥

我需要一种方式来从控制台读取输入,而无需等待用户按Enter键。

功能io.read_character无法使用,因为它会阻止,直到用户按Enter键。

回答

1

正如在SO(herehere)以及elsewhere的回答中所解释的那样,没有便携式方式从控制台读取角色而无需等待。但是,您可以通过连接到Eiffel的外部代码轻松使用链接中列出的任何方法。下面的例子演示了如何做到这一点在Windows上:

 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 

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可以从你的代码作为一个经常一个被称为