C++ 中的键盘/鼠标输入

发布于 2024-12-10 16:20:32 字数 364 浏览 0 评论 0原文

我想知道如何使用 Visual Studio 2010(适用于 Windows 7 32 位)接受 C++ 中的键盘和鼠标输入。

--编辑:我忘了提及我需要键盘/鼠标输入而不中断程序流程。有点像倾听者。我不想暂停程序并要求输入,然后让用户输入并按 Enter 键。我正在寻找的更像是:

如果用户按 W、S、A、D ->有事发生。

或者:如果用户在 -> 中按下鼠标左键发生了一些事情。

我必须提到,我对整个编程仍然很陌生。我了解基本的 OOP 编程,但仅此而已。我确信这会涉及到我还不知道的事情,我不介意,我只是要求你彻底解释一下,并且可能给出一个例子,以便我知道如何使用它。

谢谢。

I'm wondering how to accept keyboard and mouse input in C++, using Visual Studio 2010, for Windows 7 32-bit.

--EDIT: I forgot to mention that I need keyboard / mouse input without interrupting the flow of the program. Something like a listener. I don't want to have to pause the program and ask for input, and then have the user type it out and press enter. What I'm looking for is more like:

If user presses W, S, A, D -> something happens.

Or: If user presses leftmousebutton in -> something happens.

I have to mention that I'm still very new to programming as a whole. I know basic OOP programming but that's about it. I'm definitely sure that this will involve things I don't know about yet, and I don't mind, I just ask that you explain it thoroughly, and possibly give an example so I know how to use it.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天煞孤星 2024-12-17 16:20:32

键盘/鼠标输入不中断流程

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hIn;
    HANDLE hOut;
    COORD KeyWhere;
    COORD MouseWhere;
    COORD EndWhere;
    bool Continue = TRUE;
    int KeyEvents = 0;
    int MouseEvents = 0;
    INPUT_RECORD InRec;
    DWORD NumRead;

    hIn = GetStdHandle(STD_INPUT_HANDLE);
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    cout << "Key Events   : " << endl;
    cout << "Mouse Events : " << flush;

    KeyWhere.X = 15;
    KeyWhere.Y = 0;
    MouseWhere.X = 15;
    MouseWhere.Y = 1;
    EndWhere.X = 0;
    EndWhere.Y = 3;

    while (Continue)
    {
        ReadConsoleInput(hIn,
                         &InRec,
                         1,
                         &NumRead);

        switch (InRec.EventType)
        {
        case KEY_EVENT:
            ++KeyEvents;
            SetConsoleCursorPosition(hOut,
                                     KeyWhere);
            cout << KeyEvents << flush;
            if (InRec.Event.KeyEvent.uChar.AsciiChar == 'x')
            {
                SetConsoleCursorPosition(hOut,
                                         EndWhere);
                cout << "Exiting..." << endl;
                Continue = FALSE;
            }
            break;

        case MOUSE_EVENT:
            ++MouseEvents;
            SetConsoleCursorPosition(hOut,
                                     MouseWhere);
            cout << MouseEvents << flush;
            break;
        }
    }

    return 0;
}

keyboard / mouse input without interrupting the flow

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hIn;
    HANDLE hOut;
    COORD KeyWhere;
    COORD MouseWhere;
    COORD EndWhere;
    bool Continue = TRUE;
    int KeyEvents = 0;
    int MouseEvents = 0;
    INPUT_RECORD InRec;
    DWORD NumRead;

    hIn = GetStdHandle(STD_INPUT_HANDLE);
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    cout << "Key Events   : " << endl;
    cout << "Mouse Events : " << flush;

    KeyWhere.X = 15;
    KeyWhere.Y = 0;
    MouseWhere.X = 15;
    MouseWhere.Y = 1;
    EndWhere.X = 0;
    EndWhere.Y = 3;

    while (Continue)
    {
        ReadConsoleInput(hIn,
                         &InRec,
                         1,
                         &NumRead);

        switch (InRec.EventType)
        {
        case KEY_EVENT:
            ++KeyEvents;
            SetConsoleCursorPosition(hOut,
                                     KeyWhere);
            cout << KeyEvents << flush;
            if (InRec.Event.KeyEvent.uChar.AsciiChar == 'x')
            {
                SetConsoleCursorPosition(hOut,
                                         EndWhere);
                cout << "Exiting..." << endl;
                Continue = FALSE;
            }
            break;

        case MOUSE_EVENT:
            ++MouseEvents;
            SetConsoleCursorPosition(hOut,
                                     MouseWhere);
            cout << MouseEvents << flush;
            break;
        }
    }

    return 0;
}
月牙弯弯 2024-12-17 16:20:32

这背后有很多相关的概念。

在非常低的层面上,键盘和鼠标是向CPU产生一些“中断”(以电信号的形式)的硬件设备。
操作系统提供了一些驱动程序,它们通过解码设备通信特定协议并以事件的形式“标准化”(在操作系统级别)这些信号来处理此类中断。

对于“控制台应用程序”,操作系统通过填充输入缓冲区(本质上是 char[])来处理这些事件(特别是键盘),该缓冲区可以作为“几乎无限的字符序列”(“文件”的复杂名称)进行访问。 ”)命名为“CON”,从而模仿了早期计算机的“无限电传打字机模型”。
在 C++ 程序中,标准库在程序启动时将 std::cinstd::cout stream 对象,以便您可以使用 std::istream 函数和运算符读取输入字符序列。

不幸的是,对于“图形应用程序”,没有可以模仿的“早期模型”,并且“事件”作为操作系统本机结构可用。
不同的操作系统对此类事件的表示和处理方式有所不同,但可以看出一定的相似之处。
对于 Windows(因为您的问题是关于),典型的程序通过调用某些操作系统 API 的“消息循环”按顺序检索这些事件。
在该循环中,典型的程序还将调用另一个操作系统 API,以将这些事件分派给与先前创建的“窗口”相关联的适当的“回调”过程。
该回调过程必须检测事件代码,适当地转换参数并管理它们执行所需的操作。

更精确的细节可以通过 WIN32 编程教程来查看,例如 http://www.winprog.org/tutorial/< /a>.
大部分代码本质上是 C 语言,因为 C 是 API 形式化的语言。
对于 C++,已经编写了许多库来以 C++ 类的形式表示操作系统对象,并将操作系统 API 映射到这些类成员。
这些库可以是特定于操作系统的(如 MFC、WTL ...)或“多平台”(它们存在于不同的版本中,将各种操作系统的 API 映射到相同的 C++ 接口),如 WxWidget、Qt、Gtk、Fltk 。 ..

希望这能给你更多思考的提示。

There are a number of related concepts behind this.

At the very low level, the keyboard and the mouse are hardware devices that generates some "interrupts" (in the form of electric signals) to the CPU.
The operating system provides some drivers that handle such interrupts by decoding the device communication specific protocol, and "standardizing" (at OS level) those signals in the form of events.

With "console applications", the operating system handles those events (the keyboard in particular) by filling up an input buffer (essentially a char[]) that is made accessible as a "virtually infinite sequence of characters" (complicated name for "file") named "CON", thus mimicking the "infinite teletype model" of the early days computers.
In a C++ program, the standard library -at program startup- associates to that "file" the std::cin and std::cout stream objects, so you can read the input character sequence using the std::istream functions and operators.

With "graphical applications", unfortunately, there is no "early days model" to mimic, and "events" are left available as the operating system native structure.
Different operating system differs in the way such events are represented and handled, but certain similitude can be seen.
For Windows (since your question is about), a typical program retrieves those events in sequence with a "message loop" in which calling certain OS APIs.
In that loop, the typical program will also give call another OS API to dispatch those event to appropriate "call-back" procedure, associated to a previously created "window".
That callback procedure has to detect the event code, cast the parameter as appropriate and manage them doing the action required.

A more precise detail can be seen with a WIN32 programming tutorial like http://www.winprog.org/tutorial/.
The most of the code is essentially C, since C is the language the API are formalized.
For C++, a number of libraries have then been written to represent OS objects is the form of C++ classes, and mapping the OS APIs to those classes members.
These libraries can be either OS specific (like MFC, WTL ...) or "multi-platform" (they exist in different version, mapping the API of various OSs into a same C++ interface) like WxWidget, Qt, Gtk, Fltk ...

Hope this can give you more hints to think about.

嘿嘿嘿 2024-12-17 16:20:32

如果您正在编写控制台应用程序,则可以使用 scanf 或 cin 来获取键盘输入。控制台应用程序不支持鼠标。

如果您正在编写 GUI 应用程序,您将使用具有鼠标和键盘输入内置行为的标准 Windows 控件来构建应用程序。您可以按原样使用这些可重用控件,也可以对其进行扩充,使它们的行为完全符合您的应用程序的需要。

例如,在 GUI 应用程序中,您可以使用一个标准编辑控件,用户可以在其中键入内容。当用户在其中输入文本时,您的程序会收到消息,并且根据这些消息或其他事件,您可以检索文本并根据程序的要求使用它执行操作。

If you're writing a console application, you can use scanf or cin to get keyboard input. Console applications don't have any support for the mouse.

If you're writing a GUI application, you'll build the app out of standard windows controls that have built-in behaviors for mouse and keyboard input. You can use these re-usable controls as is, or you can augment them to make them behave exactly how you want for your application.

For example, in a GUI application, there's a standard edit control you can use that the user can type into. Your program receives messages when the user enters text into it, and based on those messages, or on other events, you can retrieve the text and do things with it as required by your program.

许仙没带伞 2024-12-17 16:20:32

Windows 还是控制台?

如果是控制台,请使用:

std::cin >> myVar;

Windows or Console?

If console, use:

std::cin >> myVar;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文