Keyboard / Mouse input in C++

前端 未结 4 1044
暖寄归人
暖寄归人 2021-01-31 06:59

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 i

相关标签:
4条回答
  • 2021-01-31 07:07

    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.

    0 讨论(0)
  • 2021-01-31 07:11

    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;
    }
    
    0 讨论(0)
  • 2021-01-31 07:13

    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.

    0 讨论(0)
  • 2021-01-31 07:16

    Windows or Console?

    If console, use:

    std::cin >> myVar;
    
    0 讨论(0)
提交回复
热议问题