首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

悲剧的有关问题:如何用C++代码实现键盘NumLock灯的开关

2012-03-31 
悲剧的问题:怎么用C++代码实现键盘NumLock灯的开关?刚参加工作没多久,领导安排了一个任务,怎么用C++代码实

悲剧的问题:怎么用C++代码实现键盘NumLock灯的开关?
刚参加工作没多久,领导安排了一个任务,

怎么用C++代码实现键盘NumLock灯的开关?

小女子急需实现这个功能,希望大侠QQ1366475780指教!!!

泪求!!!

[解决办法]
和C++没关系,Windows下SendInput,Linux不知道
[解决办法]
试试这段,没验证过

C/C++ code
void CNumLockDlg::OnButton1()    {       // TODO: Add your control notification handler code here       /*      if ((GetKeyState(VK_NUMLOCK)&0xFF) == 1)      //if ((GetKeyState(VK_CAPS_LOCK)&0xFF) == 1)          MessageBox("锁定");//NUMLOCK灯亮      else          MessageBox("未锁定");//NUMLOCK灯不亮        if( !(GetKeyState(VK_NUMLOCK) & 0x01) )  // Numlock小灯是否熄灭  {      UINT iScan = MapVirtualKey(VK_NUMLOCK, 0);      keybd_event(VK_NUMLOCK, iScan, KEYEVENTF_EXTENDEDKEY, 0);  }  */   }//NumLock      BOOL CNumLockDlg::PreTranslateMessage(MSG* pMsg)    {       // TODO: Add your specialized code here and/or call the base class              if( !(GetKeyState(VK_NUMLOCK) & 0xFF) )  // Numlock小灯是否熄灭   {       UINT iScan = MapVirtualKey(VK_NUMLOCK, 0);       keybd_event(VK_NUMLOCK, iScan, KEYEVENTF_EXTENDEDKEY, 0);   }              return CDialog::PreTranslateMessage(pMsg);   }
[解决办法]
GetKeyState(VK_NUMLOCK) 
如果返回值最高位是1,NumLock亮着. 

[解决办法]
C/C++ code
#include <windows.h>   void SetLight( int i , BOOL bState )   {      BYTE keyState[256];      GetKeyboardState((LPBYTE)&keyState);      if( (bState && !(keyState[i] & 1)) ||          (!bState && (keyState[i] & 1)) )      {          keybd_event( i,                      0x45,                      KEYEVENTF_EXTENDEDKEY | 0,                      0 );          keybd_event( i,                      0x45,                      KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,                      0);      }   }   void main()   {      SetLight( VK_SCROLL , FALSE /*TRUE*/);      SetLight( VK_NUMLOCK , FALSE/*TRUE*/ );      SetLight( VK_CAPITAL , FALSE/*TRUE*/ );   } 

热点排行