VC6.0平台上遥控器模拟键盘控制最前端的窗口(含组合键)
我是在基于单文档的程序中增加一个线程实现的:
UINT RCThread(LPVOID lpParam)
{
DW_InitRCCtl(); //初始化红外装置,即初始化遥控器
while(1)
{
HWND p=::GetForegroundWindow();
char key;
key=DW_ReadRCKey(); //读取遥控器键值
char pbuf[20];
sprintf(pbuf,"%d",key);
int i;
i=atoi(pbuf);
if(-1!=key)
{
switch(i)
{
case 0:
::PostMessage(p,WM_KEYDOWN,VK_UP,0);//UP
break;
case 1:
::PostMessage(p,WM_KEYDOWN,VK_DOWN,0);//向下
break;
//实现遥控按钮Cancel键对应键盘的VK_RIGHT键
case 2:
::PostMessage(p,WM_KEYDOWN,VK_RIGHT,0);
break;
//实现遥控按钮TAB键对应键盘的VK_LEFT键
case 3:
::PostMessage(p,WM_KEYDOWN,VK_LEFT,0);//向左
break;
//二:实现遥控按钮REC键对应键盘的VK_DELETE键
case 4:
::PostMessage(p,WM_KEYDOWN,VK_DELETE,0);//删除
break;
//实现遥控按钮CAP键对应键盘的VK_ESCAPE键
case 5:
::PostMessage(p,WM_KEYDOWN,VK_ESCAPE,0);//后退
break;
//实现遥控按钮TVMode键对应键盘的VK_CONTROL&& VK_RIGHT键
case 30:
::BringWindowToTop(p); //快进
keybd_event(VK_CONTROL,0,0,0);
keybd_event(VK_RIGHT,0,0,0);
keybd_event(VK_RIGHT,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
break;
//实现遥控按钮STOP键对应键盘的VK_CONTROL&&VK_M键
case 7:
::BringWindowToTop(p); //菜单
keybd_event(VK_CONTROL,0,0,0);
keybd_event('M',0,0,0);
keybd_event('M',0,KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
break;
//三:实现遥控按钮CH+键对应键盘的PAGEUP键
case 28:
::PostMessage(p,WM_KEYDOWN,0x21,0);//上一段
break;
//实现遥控按钮POWER键对应键盘的VK_ALT&& VK_F4键
case 10:
::BringWindowToTop(p); //菜单
keybd_event(VK_MENU,0,0,0);
keybd_event(VK_F4,0,0,0);
keybd_event(VK_F4,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
break;
//实现遥控按钮PAUSE,CH-键对应键盘的VK_SPACE键
case 11:
case 15:
::PostMessage(p,WM_KEYDOWN,VK_SPACE,0);//播放,暂停
break;
//实现遥控按钮MUTE键对应键盘的VK_CONTROL&&VK_Q键
case 12:
::BringWindowToTop(p); //静音
keybd_event(VK_CONTROL,0,0,0);
keybd_event('Q',0,0,0);
keybd_event('Q',0,KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
break;
//四:实现遥控按钮Vol-键对应键盘的VK_SUBTRACT键
case 14:
::PostMessage(p,WM_KEYDOWN,VK_SUBTRACT,0);//音量-
break;
//实现遥控按钮FULL键对应键盘的VK_CONTROL&& VK_LEFT键
case 64:
::BringWindowToTop(p); //快退
keybd_event(VK_CONTROL,0,0,0);
keybd_event(VK_LEFT,0,0,0);
keybd_event(VK_LEFT,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);
break;
//实现遥控按钮0键对应键盘的VK_0键
case 16:
::PostMessage(p,WM_KEYDOWN,0x30,0);//0
break;
//实现遥控按钮1键对应键盘的VK_1键
case 17:
::PostMessage(p,WM_KEYDOWN,0x31,0);//1
break;
//五:实现遥控按钮2键对应键盘的VK_2键
case 18:
::PostMessage(p,WM_KEYDOWN,0x32,0);//2
break;
//实现遥控按钮3键对应键盘的VK_3键
case 19:
::PostMessage(p,WM_KEYDOWN,0x33,0);//3
break;
//实现遥控按钮4键对应键盘的VK_4键
case 20:
::PostMessage(p,WM_KEYDOWN,0x34,0);//4
break;
//实现遥控按钮5键对应键盘的VK_5键
case 21:
::PostMessage(p,WM_KEYDOWN,0x35,0);//5
break;
//六:实现遥控按钮6键对应键盘的VK_6键
case 22:
::PostMessage(p,WM_KEYDOWN,0x36,0);//6
break;
//实现遥控按钮7键对应键盘的VK_7键
case 23:
::PostMessage(p,WM_KEYDOWN,0x37,0);//7
break;
//实现遥控按钮8键对应键盘的VK_8键
case 24:
::PostMessage(p,WM_KEYDOWN,0x38,0);//8
break;
//实现遥控按钮9键对应键盘的VK_9键
case 25:
::PostMessage(p,WM_KEYDOWN,0x39,0);//9
break;
//七:实现遥控按钮Vol+键对应键盘的VK_ADD键
case 26:
::PostMessage(p,WM_KEYDOWN,VK_ADD,0);//音量+
break;
//实现遥控按钮Rewind键对应键盘的PAGEDOWN键
case 8:
::PostMessage(p,WM_KEYDOWN,0x22,0);//下一段
break;
//实现遥控按钮Fast键对应键盘的VK_SHIFT&& VK_SPACE键
case 6:
::BringWindowToTop(p); //停止
keybd_event(VK_SHIFT,0,0,0);
keybd_event(VK_SPACE,0,0,0);
keybd_event(VK_SPACE,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);
break;
//实现遥控按钮OK键对应键盘的VK_ENTER键
case 31:
::PostMessage(p,WM_KEYDOWN,0xD,0);//确定
break;
default:
break;
}
}
}
return 0;
}