The last time we talked about keyboard events, this time we introduced mouse events. The following is a brief description of the structure of the mouse event and related information.
typedef struct _MOUSE_EVENT_RECORD/mouse event structure body {COORD dwmouseposition; The current mouse position in the console window buffer DWORD dwbuttonstate; The state of the mouse button DWORD dwcontrolkeystate; Control key state DWORD dweventflags;
Mouse event Type} Mouse_event_record; /* Where the mouse button status Dwbuttonstate possible values are ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FROM_LEFT_1ST_BUTTON_PR Essed the leftmost mouse button is generally the left mouse button from_left_2nd_button_pressed The second mouse button is pressed in general is the middle mouse button, is the wheel key from_left _3rd_button_pressed the third mouse button on the left is pressed from_left_4th_button_pressed the fourth mouse button is pressed rightmost_button_pressed The rightmost mouse button is pressed in general is the right mouse button ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Control key state Dwcontrolkeystate and The same as the keyboard event ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ enhanced_key extension keys are pressed left_alt_pressed The left ALT key is pressed down left_ctrl_pressed left CTRL key is pressed right_alt_pressed right ALT key is pressed right_ctrl_pressed right CTRL key is pressed
NUMLOCK_ON digital lock is turned on scrolllock_on Scroll lock is turned on capslock_on caps Lock is opened shift_pressed Sh The IFT key is pressed ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ mouse event type Dweventflags has the following ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Double_click Double-click, the first click only as a normal key event, the second hit as a double-click event mouse_hwheeled level Mouse wheel Mobile mouse_moved mouse movement mouse_wheeled vertical mouse wheel Move 0 when the mouse has a key is pressed or released * *
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/C/
The following is a sample program that displays the coordinates of the current mouse in the buffer at the bottom of the console window buffer, clicks the left arrow to output the letter A in the current mouse position, clicks the right button to output the letter B, and double-clicks any mouse button exit function. The procedure is as follows:
#include <stdio.h> #include <windows.h> #include <conio.h> HANDLE handle_in;
HANDLE handle_out; Console_screen_buffer_info Csbi; Defines the window buffer information structure void Displaymouseposition (COORD pos); Shows the location of the mouse in void gotoxy (int x, int y); Move the cursor to the position of coordinates (x,y) int main () {handle_in = GetStdHandle (Std_input_handle); Get the standard input device handle handle_out = GetStdHandle (Std_output_handle); Obtain the standard output device handle Input_record Mouserec; Defines the input event structure DWORD res; Used to store read records COORD POS; Used to store the mouse current position COORD size = {80, 25}; Window buffer size Getconsolescreenbufferinfo (handle_out, &CSBI); Gets the window buffer information setconsolescreenbuffersize (handle_out, size);
Sets the window buffer size for (;;) {Readconsoleinput (handle_in, &mouserec, 1, &res); Read input Event pos = Mouserec. Event.MouseEvent.dwMousePosition; Get current mouse position gotoxy (0, 24); Display mouse position displaymouseposition (POS) on line 25th; Displays the mouse position if (MOUSEREC). EventType = = mouse_event//If current is mouse event {gotoxy (POS). X, Pos.
Y); Click the left mouse button to output the character a if (Mouserec).
Event.MouseEvent.dwButtonState = = from_left_1st_button_pressed) {putchar (' A '); //Click the right mouse button to output the character B if (Mouserec).
Event.MouseEvent.dwButtonState = = rightmost_button_pressed) {putchar (' B '); //Double Repel out if (mouserec.
Event.MouseEvent.dwEventFlags = = Double_click) {break;
}} CloseHandle (Handle_out);
CloseHandle (handle_in);
return 0; } void Displaymouseposition (COORD pos) {COORD dis = {0, 24}; Show mouse position on line 24th WORD att = foreground_green | FOREground_intensity; Text Attributes Getconsolescreenbufferinfo (handle_out, &CSBI); Gets the window buffer information printf ("X =%3d, Y =%3d", (int) pos. X, (int) pos.
Y); Fillconsoleoutputattribute (handle_out, att, dis, NULL);
Fills the Text property return;
} void Gotoxy (int x, int y) {COORD pos = {x, y};
SetConsoleCursorPosition (Handle_out, POS); }
Enclose the Hello world! written in this procedure Diagram of: