[C language] Console window graphical interface programming (7): mouse events

00. Table of Contents

01. INPUT_RECORD structure

Describe input events in the console input buffer. You can use the ReadConsoleInput or PeekConsoleInput function to read these records from the input buffer, or you can use the WriteConsoleInput function to write these records into the input buffer.

Type declaration:

typedef struct _INPUT_RECORD {
  WORD  EventType;
  union {
    KEY_EVENT_RECORD          KeyEvent;
    MOUSE_EVENT_RECORD        MouseEvent;
    WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
    MENU_EVENT_RECORD         MenuEvent;
    FOCUS_EVENT_RECORD        FocusEvent;
  } Event;
} INPUT_RECORD;

EventType

Enter the handle of the event type and the event record stored in the Event member.

This member can be one of the following values.

value meaning
FOCUS_EVENT 0x0010 The event member contains a FOCUS_EVENT_RECORD structure. These events are used internally and should be ignored.
KEY_EVENT 0x0001 The event member contains a KEY_EVENT_RECORD structure information about keyboard events.
MENU_EVENT 0x0008 The event member contains a MENU_EVENT_RECORD structure. These events are used internally and should be ignored.
MOUSE_EVENT 0x0002 The event component contains information about mouse movement or key press events in the MOUSE_EVENT_RECORD structure.
WINDOW_BUFFER_SIZE_EVENT 0x0004 The event member contains a WINDOW_BUFFER_SIZE_RECORD structure information about the new size of the console screen buffer.

Event
Event information. The format of this member depends on the event type specified by the EventType member.

02. MOUSE_EVENT_RECORD structure

Describe the mouse input events in the INPUT_RECORD structure of the console .

Type declaration:

typedef struct _MOUSE_EVENT_RECORD {
  COORD dwMousePosition;
  DWORD dwButtonState;
  DWORD dwControlKeyState;
  DWORD dwEventFlags;
} MOUSE_EVENT_RECORD;

Member description

dwMousePosition is
a COORD structure, which contains the position of the cursor according to the character cell coordinates of the console screen buffer.

dwButtonState
The state of the mouse button. The least significant bit corresponds to the leftmost mouse button. The next least significant bit corresponds to the rightmost mouse button. The next digit represents the mouse button from left to right. Then, these bits correspond to the mouse buttons from left to right. If the button is pressed, the bit is 1.

The following constants are defined for the first five mouse buttons.

value meaning
FROM_LEFT_1ST_BUTTON_PRESSED 0x0001 The leftmost mouse button. Generally speaking, the left mouse button
FROM_LEFT_2ND_BUTTON_PRESSED 0x0004 The second button from the left. Generally speaking, the middle mouse button is the scroll wheel button
FROM_LEFT_3RD_BUTTON_PRESSED 0x0008 The third button from the left.
FROM_LEFT_4TH_BUTTON_PRESSED 0x0010 The fourth button from the left.
RIGHTMOST_BUTTON_PRESSED 0x0002 The rightmost mouse button. Generally speaking, the right mouse button

dwControlKeyState
control key state. This member can be one or more of the following values.

value meaning
CAPSLOCK_ON 0x0080 Caps lock is turned on
ENHANCED_KEY 0x0100 The extension key is pressed
LEFT_ALT_PRESSED 0x0002 Press the left ALT key.
LEFT_CTRL_PRESSED 0x0008 Press the left CTRL key.
NUMLOCK_ON 0x0020 Digital lock is turned on
RIGHT_ALT_PRESSED 0x0001 Press the right ALT key.
RIGHT_CTRL_PRESSED 0x0004 Press the right CTRL key.
SCROLLLOCK_ON 0x0040 Scroll lock is turned on
SHIFT_PRESSED 0x0010 Press the SHIFT key.

dwEventFlags
mouse event type. If this value is zero, it means that the mouse button is being pressed or released. Otherwise, this member is one of the following values.

value meaning
DOUBLE_CLICK 0x0002 The second click (button press) of the double-click occurs. The first click is returned as a regular button event.
MOUSE_HWHEELED 0x0008 The horizontal mouse wheel has been moved. If the high-order word of the dwButtonState member contains a positive value, the wheel rotates to the right. Otherwise, the wheel rotates to the left.
MOUSE_MOVED 0x0001 The mouse position has changed.
MOUSE_WHEELED 0x0004 The vertical mouse wheel is moved. If the high-order word of the dwButtonState member contains a positive value, the wheel rotates forward, away from the user. Otherwise, the wheel rotates backward, towards the user.

03. ReadConsoleInput function

Read data from the console input buffer and delete it from the buffer.

Function declaration:

BOOL WINAPI ReadConsoleInput(
  _In_  HANDLE        hConsoleInput,
  _Out_ PINPUT_RECORD lpBuffer,
  _In_  DWORD         nLength,
  _Out_ LPDWORD       lpNumberOfEventsRead
);
功能:
	从控制台输入缓冲区读取数据并将其从缓冲区中删除。
参数:
	hConsoleInput 控制台输入缓冲区的句柄。句柄必须具有GENERIC_READ访问权限。
	lpBuffer 指向接收输入缓冲区数据的INPUT_RECORD结构数组的指针。
	nLength 数组元素中lpBuffer参数 指向的数组大小。
	lpNumberOfEventsRead 指向接收读取的输入记录数的变量的指针。
	
返回值:
	如果函数成功,则返回值为非零值。
	如果函数失败,则返回值为零。要获取扩展错误信息,请调用GetLastError。

Official reference URL: https://docs.microsoft.com/en-us/windows/console/readconsoleinput

04. Sample program

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include <Windows.h>
#include <conio.h>


int main(void)
{
	//定义句柄变量
	HANDLE hOut = NULL;
	HANDLE hIn = NULL;

	//定义输入事件结构体
	INPUT_RECORD mouseRecord;

	//用于存储读取记录
	DWORD res;

	//用于存储鼠标当前位置
	COORD pos;


	//获取标准输出句柄
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	//获取标准输入句柄
	hIn = GetStdHandle(STD_INPUT_HANDLE);
	
	while (1)
	{
		//读取输入事件
		ReadConsoleInput(hIn, &mouseRecord, 1, &res);

		//获取鼠标当前位置
		pos = mouseRecord.Event.MouseEvent.dwMousePosition;

		//如果当前事件是鼠标事件
		if (mouseRecord.EventType == MOUSE_EVENT)
		{
			//单击鼠标左键
			if (mouseRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
			{
				printf("鼠标左键单击 x: %d y: %d\n", pos.X, pos.Y);
			}

			//单击鼠标右键
			if (mouseRecord.Event.MouseEvent.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
			{
				printf("鼠标右键单击 x: %d y: %d\n", pos.X, pos.Y);
			}

			//如果是双击就退出循环
			if (mouseRecord.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
			{
				break;
			}

		}
	}

	//关闭句柄
	CloseHandle(hOut);
	CloseHandle(hIn);

	//system("pause");
	getchar();
	return 0;
}

Guess you like

Origin blog.csdn.net/dengjin20104042056/article/details/90552458