有大佬看一下我的代码出什么错吗?谢谢。

liuhuanyu 2024-09-12 13:51:37 15
#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#include <ctime>
using namespace std;
int main(){
	system("color a");
	cout<<"======================================交互中心======================================"<<endl;
	cout<<endl;
	cout<<"                                        你好"<<endl;
	cout<<"                                   请输入你的操作"<<endl; 
	cout<<"                        1.启动任务管理器    2.开始使用计算机"<<endl; 
	for(;;){
		if(KEY_DOWN(0x31)){
			system("start Taskmgr.exe");
			return 0;
		}
		if(KEY_DOWN(0x32)){
			system("Start explorer.exe");
			return 0;
		}
	}
	return 0; 
} 
{{ vote && vote.total.up }}

共 2 条回复

liuhuanyu

@Wind_Rises 多谢!问题修正了。

Wind_Rises 砂糖老师

1.使用 GetAsyncKeyState 函数:这是 Windows API 提供的函数,用于检测按键状态。GetAsyncKeyState(0x31) 会检测 '1' 键的状态,0x32 检测 '2' 键的状态。

  1. & 0x8000 检查按键是否被按下:使用按键状态与 0x8000 进行按位与操作,可以判断按键是否当前处于按下状态。
#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;

int main() {
    system("color a");
    cout << "======================================交互中心======================================" << endl;
    cout << endl;
    cout << "                                        你好" << endl;
    cout << "                                   请输入你的操作" << endl; 
    cout << "                        1.启动任务管理器    2.开始使用计算机" << endl; 

    for(;;) {
        // 检测 '1' 键是否被按下
        if(GetAsyncKeyState(0x31) & 0x8000) { // 0x31 是 '1' 的键盘码
            system("start Taskmgr.exe");
            return 0;
        }
        // 检测 '2' 键是否被按下
        if(GetAsyncKeyState(0x32) & 0x8000) { // 0x32 是 '2' 的键盘码
            system("start explorer.exe");
            return 0;
        }
    }
    return 0; 
}

咱也不知道对错 毕竟已经好几年过去了