程序有个问题暂时处理不到位,不够完善,以后再解决。 画图是多余的代码,当做是保存算法代码。。。。
借鉴了别人部分代码。
#include <stdio.h>
#include <windows.h>
//声明函数
void OutputString();
int ConsoleMessage();
int main()
{
::SetConsoleTitle("控制台键盘消息处理程序");//设置程序标题
OutputString();
ConsoleMessage();
return 0;
}
//函数的实现
void OutputString()//打印一串一定顺序的数字
{
for (int i=1,j;i<10;i+=2)
{
for (j=1;j<=i;j+=2)
{
putchar(j+48);
}
for (j=i-2;j>=1;j-=2)
{
putchar(j+48);
}
puts(" ");
}
}
int ConsoleMessage()//处理控制台程序的部分键盘和鼠标消息
{
DWORD dwRes,dwState=0;
bool bCaps,bNum,bScroll;
COORD crHome1={0,5},crHome2={0,7},crKeyPos,crMousePos;//实质是结构体
INPUT_RECORD inputRec;
CONSOLE_SCREEN_BUFFER_INFO bInfo;
//获取标准输入输出设备句柄
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hInt = GetStdHandle(STD_INPUT_HANDLE);
printf("\n鼠标的初始位置 X: %2lu Y: %2lu\n",0,6);
//处理控制台键盘和鼠标消息
while (1)
{
ReadConsoleInput(hInt,&inputRec,1,&dwRes);
switch(inputRec.EventType)
{
case KEY_EVENT://处理键盘事件
{
if (inputRec.Event.KeyEvent.bKeyDown)
{
//判断键盘灯状态
if (dwState != inputRec.Event.KeyEvent.dwControlKeyState)
{
dwState = inputRec.Event.KeyEvent.dwControlKeyState;
bCaps = bNum = bScroll = false;
if (dwState & CAPSLOCK_ON)
{
bCaps = true;
}
if (dwState & NUMLOCK_ON)
{
bNum = true;
}
if (dwState & SCROLLLOCK_ON)
{
bScroll = true;
}
//输出三个灯的状态
SetConsoleCursorPosition(hOut,crHome1);//在{0,5}位置输出
printf("键盘灯当前状态 CAPS LOCK:%s NUM LOCK:%s SCROLL LOCK:%s \n",
bCaps?"OPEN":"CLOSE",
bNum?"OPEN":"CLOSE",
bScroll?"OPEN":"CLOSE");
}
//处理个别功能键
switch(inputRec.Event.KeyEvent.wVirtualKeyCode)
{
case VK_RETURN:
printf("\n");
break;
case VK_SPACE:
printf("Π");
break;
case VK_BACK:
GetConsoleScreenBufferInfo(hOut,&bInfo);
crKeyPos = bInfo.dwCursorPosition;
if (crKeyPos.X != 0)
{
crKeyPos.X -= 1;
}
else
{
crKeyPos.Y -=1;
//crKeyPos.X = crOldPos.X; //如何将光标移动到上一行的尾部,暂时没搞清楚
}
SetConsoleCursorPosition(hOut,crKeyPos);
printf(" ");
SetConsoleCursorPosition(hOut,crKeyPos);
break;
case VK_ESCAPE://退出程序
CloseHandle(hOut);
CloseHandle(hInt);
return 0;
default:
break;
}
}
}
break;
case MOUSE_EVENT://处理鼠标事件
{
if (inputRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
if (inputRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)//退出程序
{
CloseHandle(hOut);
CloseHandle(hInt);
return 0;
}
}
crMousePos = inputRec.Event.MouseEvent.dwMousePosition;
SetConsoleCursorPosition(hOut,crHome2);//设置当前光标位置
printf("鼠标的当前位置 X: %2lu Y: %2lu \n",crMousePos.X, crMousePos.Y);
switch(inputRec.Event.MouseEvent.dwButtonState)
{
case FROM_LEFT_1ST_BUTTON_PRESSED:
FillConsoleOutputCharacter(hOut,'A',1,crMousePos,&dwRes);
break;
case RIGHTMOST_BUTTON_PRESSED:
FillConsoleOutputCharacter(hOut,'a',1,crMousePos,&dwRes);
break;
default:
break;
}
}
break;
default:
break;
}
}
return 0;
}