Today is the first day to review C language implementation. Today I want to write C to learn Windows/Dos keyboard events. But I did not install the MFC framework when installing Visual Studio 2022. Today I recorded that VS++ added the MFC framework.
Visual Studio 2022 Additional MFC
1. Open vs++, click Create New Project, slide the sliding box on the right to the bottom, and find and install multiple tools and functions, as shown in the figure below:
2. Click the blue "Install multiple tools and features" and check the parts circled in the picture below:
3. Then click Modify in the lower right corner. After the installation is complete, MFC appears.
C language implements Windows/Dos keyboard monitoring event source code
C language source code
#include <conio.h>
#include <stdio.h>
int main()
{
while (!_kbhit()) {
printf("Hit me!! \r");
}
printf("\nKey struck was '%c'\n", _getch());
}
Show results
Keyboard event key function description
_kbhit()
It is used to determine whether there is key information. The return value is int type (because there is no bool type in C language). 0 means it has not been clicked, and non-0 means it has been clicked.- _getch() reads direct input from the keyboard but does not display it on the console until the Enter key is pressed.
C language keyboard event listening optimization
#include<stdio.h>
#include<conio.h>
int main()
{
int key;
while (1)
{
key = _getch();
if (key == 27) break;
if (key > 31 && key < 127) /*如果不是特殊键*/
{
printf("按了 %c 键 按 ESC退出!\n", key);
continue;
}
key = _getch();
if (key == 72) printf("按了 上 键 按 ESC退出!\n");
if (key == 80) printf("按了 下 键 按 ESC退出!\n");
if (key == 75) printf("按了 左 键 按 ESC退出!\n");
if (key == 77) printf("按了 右 键 按 ESC退出!\n");
}
return 0;
}