C++ 输入按键名按下对应按键

Posted 流楚丶格念

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 输入按键名按下对应按键相关的知识,希望对你有一定的参考价值。

#include <windows.h>
#include <iostream>
#include <string.h>

using namespace std;

typedef struct KeyInfo {
	char *info;
	int key;
};


KeyInfo ki[200] = {
"回退", 0x08,
"TAB", 0x09,
"CLEAR", 0x0C,
"回车", 0x0D,
"SHIFT", 0x10,
"CONTROL", 0x11,
"MENU", 0x12,
"PAUSE", 0x13,
"CAPITAL", 0x14,
"KANA", 0x15,
"ESCAPE", 0x1B,
"CONVERT", 0x1C,
"NONCONVERT", 0x1D,
"ACCEPT", 0x1E,
"MODECHANGE", 0x1F,
"SPACE", 0x20,
"PRIOR", 0x21,
"NEXT", 0x22,
"END", 0x23,
"HOME", 0x24,
"LEFT", 0x25,
"UP", 0x26,
"RIGHT", 0x27,
"DOWN", 0x28,
"SELECT", 0x29,
"PRINT", 0x2A,
"EXECUTE", 0x2B,
"SNAPSHOT", 0x2C,
"INSERT", 0x2D,
"DELETE", 0x2E,
"HELP", 0x2F,
//数字
"0",0x30,
"1",0x31,
"2",0x32,
"3",0x33,
"4",0x34,
"5",0x35,
"6",0x36,
"7",0x37,
"8",0x38,
"9",0x39,
//字母
"a",0x41,
"b",0x42,
"c",0x43,
"d",0x44,
"e",0x45,
"f",0x46,
"g",0x47,
"h",0x48,
"i",0x49,
"j",0x4a,
"k",0x4b,
"l",0x4c,
"m",0x4d,
"n",0x4e,
"o",0x4f,
"p",0x50,
"q",0x51,
"r",0x52,
"s",0x53,
"t",0x54,
"u",0x55,
"v",0x56,
"w",0x57,
"x",0x58,
"y",0x59,
"z",0x5a,
"LWIN", 0x5B,
"RWIN", 0x5C,
"APPS", 0x5D,
"SLEEP", 0x5F,
"NUMPAD0", 0x60,
"NUMPAD1", 0x61,
"NUMPAD2", 0x62,
"NUMPAD3", 0x63,
"NUMPAD4", 0x64,
"NUMPAD5", 0x65,
"NUMPAD6", 0x66,
"NUMPAD7", 0x67,
"NUMPAD8", 0x68,
"NUMPAD9", 0x69,
"MULTIPLY", 0x6A,
"ADD", 0x6B,
"SEPARATOR", 0x6C,
"SUBTRACT", 0x6D,
"DECIMAL", 0x6E,
"DIVIDE", 0x6F,
"F1", 0x70,
"F2", 0x71,
"F3", 0x72,
"F4", 0x73,
"F5", 0x74,
"F6", 0x75,
"F7", 0x76,
"F8", 0x77,
"F9", 0x78,
"F10", 0x79,
"F11", 0x7A,
"F12", 0x7B,
};


BOOL PushBtn(int key)
{
	HWND hwnd = GetForegroundWindow();
	// HWND hwnd = GetActiveWindow();
	//调用方法:提升软件系统的权限
	SendMessage(hwnd, WM_KEYDOWN, (WPARAM)key, 0);
	return 0;
}

int main(int argc, char *argv[])
{
	char *tmp = argv[0];

	// 单个键
	if (argc == 1)
	{
		int m_key;
		for (int i = 0; i < 200; i++)
		{
			if (strcmp(tmp, ki[i].info))
			{
				m_key = ki[i].key;
				PushBtn(m_key);
			}
		}
	}
}

以上是关于C++ 输入按键名按下对应按键的主要内容,如果未能解决你的问题,请参考以下文章

基于鸿蒙OS的按键驱动

基于鸿蒙OS的按键驱动

LVGL入门之按键驱动对接

在 C++ 中只检测一个按键

C ++:如果按下Windows,模拟按键并保持按键

GPIO输入——按键检测