尝试在c ++程序中设置简单的键盘输入。使用变量存储true或false

时间:2022-02-28 00:32:28

So I don't know why I am having trouble with this. I have looked around and I haven't found an answer that made since to me. Many said you cin, but it's hard to make a Boolean storing whether a key is pressed or not and it pauses the project until input is received.

所以我不知道为什么我遇到这个问题。我环顾四周,自那以后我找不到答案。许多人说你自己,但是很难建立一个布尔值来存储是否按下一个键,它会暂停项目直到收到输入。

My ideal setup would be a function I can trigger in the update loop that stores whether or not a key is pressed inside a boolean variable... something like this:

我理想的设置是一个我可以在更新循环中触发的函数,它存储一个键是否在布尔变量中被按下......如下所示:

bool left;
bool right;
bool up;
bool down;

Update()
{
      Input();
}

Input()
{
    //Insert code for detecting whether each of the keys is up or down
}

If you could help me figure this out that would be great! Also my goal is to make a program with basically no major libraries (other then graphics and math) so if it can be from scratch only using c++ methods that would be great, but if that over-complicates things then go with the external library.

如果你能帮我解决这个问题那就太棒了!另外我的目标是创建一个基本上没有主要库的程序(除了图形和数学之外),所以如果它可以从头开始只使用c ++方法,这将是很好的,但如果这过于复杂的事情,那么去外部库。

Also as a bonus it would be nice to store the mouse x and y. I can figure out the window part (like where the Cartesian origin is), however I have no clue how to access the mouse.

另外作为奖励,存储鼠标x和y会很好。我可以弄清楚窗口部分(就像笛卡尔原点所在的那样),但是我不知道如何访问鼠标。

1 个解决方案

#1


Windows-specific:

You can use GetAsyncKeyState to determine whether the keys are pressed:

您可以使用GetAsyncKeyState来确定是否按下了键:

const bool up = GetAsyncKeyState(VK_UP);
const bool down = GetAsyncKeyState(VK_DOWN);
const bool left = GetAsyncKeyState(VK_LEFT);
const bool right = GetAsyncKeyState(VK_RIGHT);

#1


Windows-specific:

You can use GetAsyncKeyState to determine whether the keys are pressed:

您可以使用GetAsyncKeyState来确定是否按下了键:

const bool up = GetAsyncKeyState(VK_UP);
const bool down = GetAsyncKeyState(VK_DOWN);
const bool left = GetAsyncKeyState(VK_LEFT);
const bool right = GetAsyncKeyState(VK_RIGHT);