当前位置:   article > 正文

【C/C++学院】0805-语音识别控制QQ/语音控制游戏

c++语音控制游戏

语音识别控制QQ

QQ.cpp 单独测试控制qq的各种命令

  1. #include<Windows.h>
  2. void openqq()
  3. {
  4. ShellExecuteA(0, "open", "\"C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe\"", 0, 0, 1);
  5. }
  6. void closeqq()
  7. {
  8. system("taskkill /f /im QQ.exe");
  9. }
  10. void show()
  11. {
  12. HWND win = FindWindowA("TXGuiFoundation","QQ2013");
  13. if (win != NULL)
  14. {
  15. ShowWindow(win, SW_SHOW);
  16. }
  17. }
  18. void hide()
  19. {
  20. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  21. if (win != NULL)
  22. {
  23. ShowWindow(win, SW_HIDE);
  24. }
  25. }
  26. void shang()
  27. {
  28. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  29. if (win != NULL)
  30. {
  31. RECT rectwind;//区域,lefr ,right ,top,bottom
  32. GetWindowRect(win, &rectwind);//获取区域
  33. SetWindowPos(win, NULL, rectwind.left, rectwind.top-100, 300, 300, 1);
  34. }
  35. }
  36. void xia()
  37. {
  38. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  39. if (win != NULL)
  40. {
  41. RECT rectwind;//区域,lefr ,right ,top,bottom
  42. GetWindowRect(win, &rectwind);//获取区域
  43. SetWindowPos(win, NULL, rectwind.left, rectwind.top + 100, 300, 300, 1);
  44. }
  45. }
  46. void zuo()
  47. {
  48. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  49. if (win != NULL)
  50. {
  51. RECT rectwind;//区域,lefr ,right ,top,bottom
  52. GetWindowRect(win, &rectwind);//获取区域
  53. SetWindowPos(win, NULL, rectwind.left-100, rectwind.top , 300, 300, 1);
  54. }
  55. }
  56. void you()
  57. {
  58. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  59. if (win != NULL)
  60. {
  61. RECT rectwind;//区域,lefr ,right ,top,bottom
  62. GetWindowRect(win, &rectwind);//获取区域
  63. SetWindowPos(win, NULL, rectwind.left + 100, rectwind.top, 300, 300, 1);//设置区域位置
  64. }
  65. }
  66. void main()
  67. {
  68. openqq();
  69. Sleep(3000);
  70. /*show();
  71. Sleep(2000);
  72. hide();*/
  73. shang();
  74. Sleep(2000);
  75. shang();
  76. Sleep(2000);
  77. //closeqq();
  78. }

speechrecognition.cpp 结合到语音控制qq移动

  1. #include <windows.h>
  2. #include <atlstr.h>
  3. #include <sphelper.h>
  4. #include <sapi.h>
  5. #include<comutil.h>
  6. #include<string.h>
  7. #pragma comment(lib,"sapi.lib")
  8. #pragma comment(lib, "comsupp.lib")
  9. #define GID_CMD_GR 333333
  10. #define WM_RECOEVENT WM_USER+1
  11. LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
  12. void openqq();
  13. void you();
  14. void zuo();
  15. void shang();
  16. void xia();
  17. void closeqq();
  18. void show();
  19. void hide();
  20. char szAppName[] = "TsinghuaYincheng";
  21. BOOL b_initSR;
  22. BOOL b_Cmd_Grammar;
  23. CComPtr<ISpRecoContext>m_cpRecoCtxt; //语音识别程序接口
  24. CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法
  25. CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎
  26. int speak(wchar_t *str);
  27. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
  28. {
  29. HWND hwnd;
  30. MSG msg;
  31. WNDCLASS wndclass;
  32. wndclass.cbClsExtra =0;
  33. wndclass.cbWndExtra =0;
  34. wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
  35. wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
  36. wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
  37. wndclass.hInstance =hInstance;
  38. wndclass.lpfnWndProc =WndProc;
  39. wndclass.lpszClassName =szAppName;
  40. wndclass.lpszMenuName =NULL;
  41. wndclass.style =CS_HREDRAW|CS_VREDRAW;
  42. if(!RegisterClass(&wndclass))
  43. {
  44. MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
  45. return 0;
  46. }
  47. speak(L"谭胜是一个猥琐男");
  48. hwnd=CreateWindow(szAppName,
  49. TEXT("传智播客C/C++学院语音识别教程"),
  50. WS_OVERLAPPEDWINDOW,
  51. CW_USEDEFAULT,
  52. CW_USEDEFAULT,
  53. CW_USEDEFAULT,
  54. CW_USEDEFAULT,
  55. NULL,
  56. NULL,
  57. hInstance,
  58. NULL);
  59. ShowWindow(hwnd,iCmdShow);
  60. UpdateWindow(hwnd);
  61. while(GetMessage(&msg,NULL,0,0))
  62. {
  63. TranslateMessage(&msg);
  64. DispatchMessage(&msg);
  65. }
  66. return msg.wParam;
  67. }
  68. LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
  69. {
  70. HDC hdc;
  71. PAINTSTRUCT ps;
  72. switch(message)
  73. {
  74. case WM_CREATE:
  75. {
  76. //初始化COM端口
  77. ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
  78. //创建识别引擎COM实例为共享型
  79. HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
  80. //创建识别上下文接口
  81. if(SUCCEEDED(hr))
  82. {
  83. hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
  84. }
  85. else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);
  86. //设置识别消息,使计算机时刻监听语音消息
  87. if(SUCCEEDED(hr))
  88. {
  89. hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);
  90. }
  91. else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);
  92. //设置我们感兴趣的事件
  93. if(SUCCEEDED(hr))
  94. {
  95. ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);
  96. hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);
  97. }
  98. else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);
  99. //创建语法规则
  100. b_Cmd_Grammar=TRUE;
  101. if(FAILED(hr))
  102. {
  103. MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);
  104. }
  105. hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);
  106. WCHAR wszXMLFile[20]=L"er.xml";
  107. MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);
  108. hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);
  109. if(FAILED(hr))
  110. {
  111. MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);
  112. }
  113. b_initSR=TRUE;
  114. //在开始识别时,激活语法进行识别
  115. hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);
  116. return 0;
  117. }
  118. case WM_RECOEVENT:
  119. {
  120. RECT rect;
  121. GetClientRect(hwnd,&rect);
  122. hdc=GetDC(hwnd);
  123. USES_CONVERSION;
  124. CSpEvent event;
  125. while(event.GetFrom(m_cpRecoCtxt)==S_OK)
  126. {
  127. switch(event.eEventId)
  128. {
  129. case SPEI_RECOGNITION:
  130. {
  131. static const WCHAR wszUnrecognized[]=L"<Unrecognized>";
  132. CSpDynamicString dstrText;
  133. //取得识别结果
  134. if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))
  135. {
  136. dstrText=wszUnrecognized;
  137. }
  138. BSTR SRout;
  139. dstrText.CopyToBSTR(&SRout);
  140. char* lpszText2 = _com_util::ConvertBSTRToString(SRout);
  141. if(b_Cmd_Grammar)
  142. {
  143. //绘制文本
  144. DrawText(hdc, TEXT(lpszText2), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  145. if (strcmp("我是学霸",lpszText2)==0)
  146. {
  147. MessageBoxA(0, lpszText2, lpszText2, 0);
  148. }
  149. if (strcmp("清华土匪", lpszText2) == 0)
  150. {
  151. MessageBoxA(0, lpszText2, lpszText2, 0);
  152. }
  153. if (strcmp("传智播客", lpszText2) == 0)
  154. {
  155. MessageBoxA(0, lpszText2, lpszText2, 0);
  156. }
  157. if (strcmp("天下无双", lpszText2) == 0)
  158. {
  159. wchar_t * str = L"谭胜大爷吹牛皮的功夫天下无双";
  160. speak(str);
  161. }
  162. if (strcmp("给我上", lpszText2) == 0)
  163. {
  164. speak(L"亲爱的主人,好");
  165. shang();
  166. }
  167. if (strcmp("给我下", lpszText2) == 0)
  168. {
  169. speak(L"亲爱的主人,好");
  170. xia();
  171. }
  172. if (strcmp("给我左", lpszText2) == 0)
  173. {
  174. speak(L"亲爱的主人,好");
  175. zuo();
  176. }
  177. if (strcmp("给我右", lpszText2) == 0)
  178. {
  179. speak(L"亲爱的主人,好");
  180. you();
  181. }
  182. if (strcmp("打开企鹅", lpszText2) == 0)
  183. {
  184. speak(L"亲爱的主人,好");
  185. openqq();
  186. }
  187. if (strcmp("关闭企鹅", lpszText2) == 0)
  188. {
  189. speak(L"亲爱的主人,好");
  190. closeqq();
  191. }
  192. if (strcmp("显示", lpszText2) == 0)
  193. {
  194. speak(L"亲爱的主人,好");
  195. show();
  196. }
  197. if (strcmp("隐藏", lpszText2) == 0)
  198. {
  199. speak(L"亲爱的主人,好");
  200. hide();
  201. }
  202. if (strcmp("谭胜是谁", lpszText2) == 0)
  203. {
  204. speak(L"谭胜是横空出世的英雄,百年不遇的天才");
  205. }
  206. if (strcmp("你是谁", lpszText2) == 0)
  207. {
  208. speak(L"我是你们勇猛勤奋彪悍的谭胜大哥写的语音识别程序");
  209. }
  210. if (strcmp("你是笨蛋", lpszText2) == 0)
  211. {
  212. speak(L"我的创造者谭胜大哥聪明的惊天地泣鬼神");
  213. }
  214. if (strcmp("你是蠢猪", lpszText2) == 0)
  215. {
  216. speak(L"我固然很蠢,但是我的创造者谭胜大哥聪明的惊天地泣鬼神");
  217. }
  218. if (strcmp("你很好", lpszText2) == 0)
  219. {
  220. speak(L"我一直很牛逼,多亏谭大爷");
  221. }
  222. if (strcmp("中秋快乐", lpszText2) == 0)
  223. {
  224. speak(L"谭大爷叫你回家吃月饼");
  225. }
  226. }
  227. }
  228. }
  229. }
  230. return TRUE;
  231. }
  232. case WM_PAINT:
  233. hdc=BeginPaint(hwnd,&ps);
  234. EndPaint(hwnd,&ps);
  235. return 0;
  236. case WM_DESTROY:
  237. PostQuitMessage(0);
  238. return 0;
  239. }
  240. return DefWindowProc(hwnd,message,wParam,lParam);
  241. }
  242. #pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
  243. int speak(wchar_t *str)
  244. {
  245. ISpVoice * pVoice = NULL;
  246. ::CoInitialize(NULL);
  247. //获取ISpVoice接口:
  248. long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
  249. hr = pVoice->Speak(str, 0, NULL);
  250. pVoice->Release();
  251. pVoice = NULL;
  252. //千万不要忘记:
  253. ::CoUninitialize();
  254. return TRUE;
  255. }
  256. void openqq()
  257. {
  258. ShellExecuteA(0, "open", "\"C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe\"", 0, 0, 1);
  259. }
  260. void closeqq()
  261. {
  262. system("taskkill /f /im QQ.exe");
  263. }
  264. void show()
  265. {
  266. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  267. if (win != NULL)
  268. {
  269. ShowWindow(win, SW_SHOW);
  270. }
  271. }
  272. void hide()
  273. {
  274. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  275. if (win != NULL)
  276. {
  277. ShowWindow(win, SW_HIDE);
  278. }
  279. }
  280. void shang()
  281. {
  282. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  283. if (win != NULL)
  284. {
  285. RECT rectwind;//区域,lefr ,right ,top,bottom
  286. GetWindowRect(win, &rectwind);//获取区域
  287. SetWindowPos(win, NULL, rectwind.left, rectwind.top - 100, 300, 300, 1);
  288. }
  289. }
  290. void xia()
  291. {
  292. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  293. if (win != NULL)
  294. {
  295. RECT rectwind;//区域,lefr ,right ,top,bottom
  296. GetWindowRect(win, &rectwind);//获取区域
  297. SetWindowPos(win, NULL, rectwind.left, rectwind.top + 100, 300, 300, 1);
  298. }
  299. }
  300. void zuo()
  301. {
  302. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  303. if (win != NULL)
  304. {
  305. RECT rectwind;//区域,lefr ,right ,top,bottom
  306. GetWindowRect(win, &rectwind);//获取区域
  307. SetWindowPos(win, NULL, rectwind.left - 100, rectwind.top, 300, 300, 1);
  308. }
  309. }
  310. void you()
  311. {
  312. HWND win = FindWindowA("TXGuiFoundation", "QQ2013");
  313. if (win != NULL)
  314. {
  315. RECT rectwind;//区域,lefr ,right ,top,bottom
  316. GetWindowRect(win, &rectwind);//获取区域
  317. SetWindowPos(win, NULL, rectwind.left + 100, rectwind.top, 300, 300, 1);//设置区域位置
  318. }
  319. }

er.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GRAMMAR LANGID="804">
  3. <DEFINE>
  4. <ID NAME="CMD" VAL="10"/>
  5. </DEFINE>
  6. <RULE NAME="COMMAND" ID="CMD" TOPLEVEL="ACTIVE">
  7. <L>
  8. <P>我是学霸</P>
  9. <P>清华土匪</P>
  10. <P>传智播客</P>
  11. <P>天下无双</P>
  12. <P>给我上</P>
  13. <P>给我下</P>
  14. <P>给我左</P>
  15. <P>给我右</P>
  16. <P>打开企鹅</P>
  17. <P>关闭企鹅</P>
  18. <P>显示</P>
  19. <P>隐藏</P>
  20. <P>谭胜是谁</P>
  21. <P>你是谁</P>
  22. <P>你是笨蛋</P>
  23. <P>你是蠢猪</P>
  24. <P>你很好</P>
  25. <P>中秋快乐</P>
  26. </L>
  27. </RULE>
  28. </GRAMMAR>

语音控制游戏

  1. #include <windows.h>
  2. #include <atlstr.h>
  3. #include <sphelper.h>
  4. #include <sapi.h>
  5. #include<comutil.h>
  6. #include<string.h>
  7. #pragma comment(lib,"sapi.lib")
  8. #pragma comment(lib, "comsupp.lib")
  9. #define GID_CMD_GR 333333
  10. #define WM_RECOEVENT WM_USER+1
  11. LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
  12. char szAppName[] = "TsinghuaYincheng";
  13. BOOL b_initSR;
  14. BOOL b_Cmd_Grammar;
  15. CComPtr<ISpRecoContext>m_cpRecoCtxt; //语音识别程序接口
  16. CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法
  17. CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎
  18. int speak(wchar_t *str);
  19. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
  20. {
  21. HWND hwnd;
  22. MSG msg;
  23. WNDCLASS wndclass;
  24. wndclass.cbClsExtra =0;
  25. wndclass.cbWndExtra =0;
  26. wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
  27. wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
  28. wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
  29. wndclass.hInstance =hInstance;
  30. wndclass.lpfnWndProc =WndProc;
  31. wndclass.lpszClassName =szAppName;
  32. wndclass.lpszMenuName =NULL;
  33. wndclass.style =CS_HREDRAW|CS_VREDRAW;
  34. if(!RegisterClass(&wndclass))
  35. {
  36. MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
  37. return 0;
  38. }
  39. speak(L"谭胜是一个猥琐男");
  40. hwnd=CreateWindow(szAppName,
  41. TEXT("传智播客C/C++学院语音识别教程"),
  42. WS_OVERLAPPEDWINDOW,
  43. CW_USEDEFAULT,
  44. CW_USEDEFAULT,
  45. CW_USEDEFAULT,
  46. CW_USEDEFAULT,
  47. NULL,
  48. NULL,
  49. hInstance,
  50. NULL);
  51. ShowWindow(hwnd,iCmdShow);
  52. UpdateWindow(hwnd);
  53. while(GetMessage(&msg,NULL,0,0))
  54. {
  55. TranslateMessage(&msg);
  56. DispatchMessage(&msg);
  57. }
  58. return msg.wParam;
  59. }
  60. LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
  61. {
  62. HDC hdc;
  63. PAINTSTRUCT ps;
  64. switch(message)
  65. {
  66. case WM_CREATE:
  67. {
  68. //初始化COM端口
  69. ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
  70. //创建识别引擎COM实例为共享型
  71. HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
  72. //创建识别上下文接口
  73. if(SUCCEEDED(hr))
  74. {
  75. hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
  76. }
  77. else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);
  78. //设置识别消息,使计算机时刻监听语音消息
  79. if(SUCCEEDED(hr))
  80. {
  81. hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);
  82. }
  83. else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);
  84. //设置我们感兴趣的事件
  85. if(SUCCEEDED(hr))
  86. {
  87. ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);
  88. hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);
  89. }
  90. else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);
  91. //创建语法规则
  92. b_Cmd_Grammar=TRUE;
  93. if(FAILED(hr))
  94. {
  95. MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);
  96. }
  97. hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);
  98. WCHAR wszXMLFile[20]=L"er.xml";
  99. MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);
  100. hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);
  101. if(FAILED(hr))
  102. {
  103. MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);
  104. }
  105. b_initSR=TRUE;
  106. //在开始识别时,激活语法进行识别
  107. hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);
  108. return 0;
  109. }
  110. case WM_RECOEVENT:
  111. {
  112. RECT rect;
  113. GetClientRect(hwnd,&rect);
  114. hdc=GetDC(hwnd);
  115. USES_CONVERSION;
  116. CSpEvent event;
  117. while(event.GetFrom(m_cpRecoCtxt)==S_OK)
  118. {
  119. switch(event.eEventId)
  120. {
  121. case SPEI_RECOGNITION:
  122. {
  123. static const WCHAR wszUnrecognized[]=L"<Unrecognized>";
  124. CSpDynamicString dstrText;
  125. //取得识别结果
  126. if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))
  127. {
  128. dstrText=wszUnrecognized;
  129. }
  130. BSTR SRout;
  131. dstrText.CopyToBSTR(&SRout);
  132. char* lpszText2 = _com_util::ConvertBSTRToString(SRout);
  133. if(b_Cmd_Grammar)
  134. {
  135. DrawText(hdc, TEXT(lpszText2), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  136. if (strcmp("旋风刀",lpszText2)==0)
  137. {
  138. keybd_event('A', 0, 0, 0);//按下
  139. keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//弹起
  140. Sleep(10);
  141. keybd_event('D', 0, 0, 0);//按下
  142. keybd_event('D', 0, KEYEVENTF_KEYUP, 0);//弹起
  143. Sleep(10);
  144. keybd_event('W', 0, 0, 0);//按下
  145. keybd_event('W', 0, KEYEVENTF_KEYUP, 0);//弹起
  146. Sleep(10);
  147. mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  148. mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  149. }
  150. if (strcmp("大风吹", lpszText2) == 0)
  151. {
  152. //sww l
  153. keybd_event('A', 0, 0, 0);//按下
  154. keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//弹起
  155. Sleep(10);
  156. keybd_event('D', 0, 0, 0);//按下
  157. keybd_event('D', 0, KEYEVENTF_KEYUP, 0);//弹起
  158. Sleep(10);
  159. keybd_event('S', 0, 0, 0);//按下
  160. keybd_event('S', 0, KEYEVENTF_KEYUP, 0);//弹起
  161. Sleep(10);
  162. mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  163. mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  164. }
  165. if (strcmp("充气", lpszText2) == 0)
  166. {
  167. // \ang
  168. keybd_event(VK_OEM_102, 0, 0, 0);//按下
  169. keybd_event(VK_OEM_102, 0, KEYEVENTF_KEYUP, 0);//弹起
  170. Sleep(10);
  171. keybd_event('A', 0, 0, 0);//按下
  172. keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//弹起
  173. Sleep(10);
  174. keybd_event('N', 0, 0, 0);//按下
  175. keybd_event('N', 0, KEYEVENTF_KEYUP, 0);//弹起
  176. Sleep(10);
  177. keybd_event('G', 0, 0, 0);//按下6igrk;)移植
  178. keybd_event('G', 0, KEYEVENTF_KEYUP, 0);//弹起
  179. Sleep(10);
  180. keybd_event(VK_RETURN, 0, 0, 0);//按下6igrk;)移植
  181. keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);//弹起
  182. }
  183. if (strcmp("跳跃", lpszText2) == 0)
  184. {
  185. keybd_event(VK_SPACE, 0, 0, 0);//按下
  186. keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0);//弹起
  187. }
  188. if (strcmp("趴下", lpszText2) == 0)
  189. {
  190. }
  191. if (strcmp("前进", lpszText2) == 0)
  192. {
  193. }
  194. if (strcmp("后退", lpszText2) == 0)
  195. {
  196. }
  197. }
  198. }
  199. }
  200. }
  201. return TRUE;
  202. }
  203. case WM_PAINT:
  204. hdc=BeginPaint(hwnd,&ps);
  205. EndPaint(hwnd,&ps);
  206. return 0;
  207. case WM_DESTROY:
  208. PostQuitMessage(0);
  209. return 0;
  210. }
  211. return DefWindowProc(hwnd,message,wParam,lParam);
  212. }
  213. #pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
  214. int speak(wchar_t *str)
  215. {
  216. ISpVoice * pVoice = NULL;
  217. ::CoInitialize(NULL);
  218. //获取ISpVoice接口:
  219. long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
  220. hr = pVoice->Speak(str, 0, NULL);
  221. pVoice->Release();
  222. pVoice = NULL;
  223. //千万不要忘记:
  224. ::CoUninitialize();
  225. return TRUE;
  226. }

er.xml与cpp文件放在同一目录下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GRAMMAR LANGID="804">
  3. <DEFINE>
  4. <ID NAME="CMD" VAL="10"/>
  5. </DEFINE>
  6. <RULE NAME="COMMAND" ID="CMD" TOPLEVEL="ACTIVE">
  7. <L>
  8. <P>旋风刀</P>
  9. <P>大风吹</P>
  10. <P>充气</P>
  11. <P>跳跃</P>
  12. <P>趴下</P>
  13. <P>前进</P>
  14. <P>后退</P>
  15. </L>
  16. </RULE>
  17. </GRAMMAR>




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/457514
推荐阅读
相关标签
  

闽ICP备14008679号