Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard / Mouse input in C++

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.

like image 948
Rohit Avatar asked Oct 17 '11 19:10

Rohit


People also ask

What is keyboard and mouse input?

The keyboard allows the user to type letters and numbers and the mouse allows the user to position the cursor, draw and execute program functions by clicking mouse buttons.

What is the Xbutton on a mouse?

The XBUTTON1 and XBUTTON2 buttons are often located on the sides of the mouse, near the base. These extra buttons are not present on all mice. If present, the XBUTTON1 and XBUTTON2 buttons are often mapped to an application function, such as forward and backward navigation in a Web browser.

What is a keyboard in C++?

The keyboard is used for several distinct types of input, including: Character input. Text that the user types into a document or edit box. Keyboard shortcuts. Key strokes that invoke application functions; for example, CTRL + O to open a file.


2 Answers

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;
}
like image 192
Software_Designer Avatar answered Oct 02 '22 16:10

Software_Designer


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.

like image 41
Emilio Garavaglia Avatar answered Oct 02 '22 15:10

Emilio Garavaglia