简体   繁体   English

简单的“按键”程序

[英]Simple 'keypress' program

I'm just starting out in C++, and am trying to work out how to detec what key has been pressed. 我只是从C ++开始,正在尝试找出如何确定按下了什么键。 I understand the ASCII code for 'a' is 97. So... 我知道'a'的ASCII码是97。所以...

1) What is wrong with the following program? 1)以下程序有什么问题?

#include <iostream>
using namespace std;

int main()
{
char thing[1];

cin >> thing;
if (thing == 97)
    cout << "You pressed 'a'!\n";
return 0;
}

2) How can I make it so that rather than having to type the letter as an input and press enter, the program just accepts the keypress immediately? 2)我如何才能使程序不必立即输入字母并按回车键,而只是立即接受按键?

I'm just starting out in C++ 我只是从C ++开始

Welcome! 欢迎! I know you will find learning to program C++ both confusing and enjoyable. 我知道您会发现学习C ++编程既令人困惑又令人愉悦。 May I suggest that you first acquire a good C++ book? 我可以建议您首先学习一本好书吗? There is an excellent list of such books here: The Definitive C++ Book Guide and List 这里有很多此类书籍的清单: 《权威C ++书籍指南和清单》

1) What is wrong with the following program? 1)以下程序有什么问题?

#include <iostream>
using namespace std;

You shouldn't import the entire std namespace. 您不应该导入整个std名称空间。 There are a lot of symbols in that namespace, and you will almost certainly collide with one of them at some point. 该命名空间中有很多符号,几乎可以肯定,您有时会与其中一个发生冲突。 I know that many beginner textbooks instruct you to do this. 我知道许多初学者教科书都会指导您执行此操作。 Don't. 别。

int main()
{
  char thing[1];

There is no reason to declare thing an array. 没有理由将thing声明为数组。 It should be char thing; 应该是char thing; .

  cin >> thing;

Because, when you do this, you create a buffer-overflow bug. 因为,当您这样做时,会创建一个缓冲区溢出错误。 Since thing is an array, cin will treat thing as a C-style string, and happily write the entire input line to it, even if it doesn't fit. 由于thing是一个数组,因此cinthing视为C样式的字符串,并愉快地将整个输入行写入其中,即使它不合适。

if (thing == 97)
  cout << "You pressed 'a'!\n";
  • (Assuming your didn't fix the definition of thing ), thing==97 compares the array to the constant 97. More specifically, it compares the address of the first element of the array to the constant 97. This is a bad thing, and probably won't even compile. (假设您没有固定thing的定义), thing==97会将数组与常量thing==97进行比较。更具体地说,它将数组的第一个元素的地址与常量97进行比较。这很不好,甚至可能不会编译。
  • (Assuming you fixed the definition of thing ), the naked constant 97 is confusing to the readers of your code, including to yourself. (假设您固定了thing的定义),裸常量97会使您的代码读者(包括您自己)感到困惑。 Instead compare thing to the equally valid integral constant 'a' . 相反,将thing与同样有效的积分常数'a'

Putting it all together: 放在一起:

#include <iostream>
using std::cout;
using std::cin;

int main()
{
  char thing;

  cin >> thing;
  if (thing == 'a')
    cout << "You pressed 'a'!\n";
  else
    cout << "You pressed not-'a': " << thing << "\n";
  return 0;
}

2) How can I make it so that rather than having to type the letter as an input and press enter, the program just accepts the keypress immediately? 2)我如何才能使程序不必立即输入字母并按回车键,而只是立即接受按键?

As others have pointed out, you must use a platform-specific API for this. 正如其他人指出的那样,您必须为此使用特定于平台的API。 On Microsoft Windows, try getch() . 在Microsoft Windows上,尝试getch()

1) 1)

cin >> thing

is unsafe if they press more than one character before pushing enter, as you've only allocated space for 1 character. 如果他们在按下Enter键之前按了多个字符,则不安全,因为您只为1个字符分配了空间。

Also, you want 另外,你要

if(thing[0] == 97) // ...

2) There is no standard way. 2)没有标准方法。 The function getch() works on some platforms. 函数getch()在某些平台上有效。

Use the function _getch() to give you a character without waiting for the enter key. 使用函数_getch()可以为您提供一个字符,而无需等待回车键。 Just include conio.h and use it. 只需包含conio.h并使用它即可。

It works on Windows but it is listed as deprecated for visual C++. 它可以在Windows上运行,但对于Visual C ++已被弃用。 So it is a non standard way and not portable. 因此,这是一种非标准的方法,并且不便于移植。

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int character;

    character = getch_();
    if (character == 97)
        cout << "You pressed 'a'!\n";
    return 0;
}

If you want to return the character code of the key pressed and output the character pressed to the console then you can use getche_() 如果要返回所按下键的字符代码并将其输出到控制台,则可以使用getche_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM