【问题标题】:getch() like function in dartgetch() 类似 dart 中的函数
【发布时间】:2020-05-14 21:02:49
【问题描述】:

在 c/c++ 语言中,conio.h 头文件中有一个名为 getch() 的函数,它允许您只输入 1 个字符并且不会在屏幕上回显它,一旦输入该字符,它自动转到下一行代码,无需回车。

我尝试在 dart 中使用 stdin.readByteSync(),但它并没有提供 getch() 在 c/c++ 中提供的功能。我想知道是否有办法在 dart 中创建函数或方法,其行为方式与 getch() 在 c/c++ 中的行为方式相同。谢谢。

【问题讨论】:

  • 首先,没有“C/C++”语言,有两种非常不同的语言C和C++。也没有标准的 C 或 C++ 头文件 conio.h,它最初是一个 DOS 特定的头文件,仅在 Windows 中仍然存在。
  • 你错了。在 C++ 标准中没有名为 <conio.h> 的头文件。这是一个特定于操作系统的头文件,它提供仅特定于您的操作系统的功能。 C++ 标准中没有类似的东西。
  • 为了在不按回车的情况下读取密钥,您需要使用操作系统特定的 API。如果您使用的是 GUI 系统,您可能能够响应有关按键或释放的消息。当键状态改变时,一些操作系统将允许回调函数。一切都取决于您的操作系统,也许还有框架。
  • 不管getch()conio.h 的非标准性如何,您可以查看Flutter 工具(用Dart 编写)如何处理在按下r 时触发热重载。

标签: c++ c dart stdin getch


【解决方案1】:

您只需将以下选项设置为 false: https://api.dart.dev/stable/2.8.2/dart-io/Stdin/lineMode.html

如果您使用的是 Windows,您还需要根据文档首先将以下内容设置为 false: https://api.dart.dev/stable/2.8.2/dart-io/Stdin/echoMode.html

可以这样制作一个简单的工作示例,它只是重复您键入的内容。它在 IntelliJ 中不起作用,但在 CMD、PowerShell 和 Linux bash 中起作用:

import 'dart:convert';
import 'dart:io';

void main() {
  stdin.echoMode = false;
  stdin.lineMode = false;
  stdin.transform(utf8.decoder).forEach((element) => print('Got: $element'));
}

通过这样做,我们也可以根据您自己的建议使用stdin.readByteSync()(请注意,如果您获得 UTF-8 输入,则一个字符可以包含多个字节:

import 'dart:io';

void main() {
  print(getch());
}

int getch() {
  stdin.echoMode = false;
  stdin.lineMode = false;
  return stdin.readByteSync();
}

【讨论】:

    【解决方案2】:

    感谢大家的贡献。但是添加到我得到的答案是这样的

    import 'dart:io';
    
     void main() {
      print(getch());
      }
    
      int getch() {
      stdin.echoMode = false;
      stdin.lineMode = false;
      return stdin.readByteSync();
    }
    

    我决定添加一些东西,使它更像 c 语言 conio.h 头文件中的 getch() 函数。代码是这样的

    import 'dart:io';
    
    void main() {
      print(getch());
    }
    
    String getch() {
      stdin.echoMode = false;
      stdin.lineMode = false;
      int a = stdin.readByteSync();
     return String.fromCharCode(a);
     }
    

    虽然它只适用于 cmd、powershell 和 linux 终端,而不适用于 intelliJ,但总比没有好。最重要的是为flutter和web之类的东西打下dart的基础。有了这些小知识,我就将其付诸实践,制作了一个简单基本的飞镖打字游戏。代码如下:

    import 'dart:io';
    import 'dart:convert';
    import 'dart:core';
    
    
    void main() {
    
        Stopwatch s = Stopwatch();  
    
        String sentence = 'In the famous battle of Thermopylae in 480 BC, one of the most famous battles in history, King Leonidas of Sparta said the phrase'
    ' Molon Labe which means \"come and take them\" in ancient greek to Xerxes I of Persia when the Persians asked the Spartans to lay'
    ' down their arms and surrender.';
    
        List<String> sentenceSplit = sentence.split(' ');
        int wordCount = sentenceSplit.length;
    
        print('Welcome to this typing game. Type the words you see on the                     screen below\n\n$sentence\n\n');
    
        for (int i=0; i<sentence.length; i++) {
        if(i==1) {
            s.start();  // start the timer after first letter is clicked
        }
        if(getch() == sentence[i]) {
            stdout.write(sentence[i]);
        }
        else {
            i--;
            continue;
        }
        }
    
        s.stop();  // stop the timer
        int typingSpeed = wordCount ~/ (s.elapsed.inSeconds/60);
    
        print('\n\nWord Count:\t$wordCount words');
        print('Elapsed time:\t${s.elapsed.inSeconds} seconds');
        print('Typing speed:\t$typingSpeed WPM');
    }
    
    String getch() {
      stdin.echoMode = false;
      stdin.lineMode = false;
      int a = stdin.readByteSync();
      return String.fromCharCode(a);
    }
    

    您可以继续前进,使其成为当用户启动 再次游戏,它应该显示不同的文本,这样他们就不会习惯了。但无论如何,这就是这个问题。它正式关闭。虽然,如果您还有要添加的内容,请随时将其放在这里。谢谢!

    【讨论】:

      猜你喜欢
      • 2016-12-07
      • 2021-08-20
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      相关资源
      最近更新 更多