请问我模拟按键 我想模拟← → 用到keybd_event函数 那么我该怎么用呢? 是不是中间加个Sleep?但是我加了程序就未响应了
void CAjDlg::OnButton1() 
{
SetTimer(1,300,NULL);//启动时间控制器.时间为300豪秒
}
void CAjDlg::OnTimer(UINT nIDEvent) //时间控制器
{
    if(nIDEvent==1){
keybd_event(VK_LEFT,MapVirtualKey(VK_LEFT,0),0,0);
keybd_event(VK_LEFT,MapVirtualKey(VK_LEFT,0),KEYEVENTF_KEYUP,0);
Sleep(1000);
keybd_event(VK_RIGHT,MapVirtualKey(VK_RIGHT,0),0,0);
keybd_event(VK_RIGHT,MapVirtualKey(VK_RIGHT,0),KEYEVENTF_KEYUP,0);
    }    
    }
我用在当前程序 加了个文本框 试了未响应 不是在当前程序却没反映
而我用了自动按键 却可以用 这是想连续按两个键却不能用

解决方案 »

  1.   

    Windows NT/2000/XP: This function has been superseded. Use SendInput instead.在Windows NT/2000/XP,这个函数已经被SendInput取代
    SendInput
    The SendInput function synthesizes keystrokes, mouse motions, and button clicks.UINT SendInput(
      UINT nInputs,     // count of input events
      LPINPUT pInputs,  // array of input events
      int cbSize        // size of structure
    );
    Parameters
    nInputs 
    [in] Specifies the number of structures in the pInputs array. 
    pInputs 
    [in] Pointer to an array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream. 
    cbSize 
    [in] Specifies the size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function will fail. 
      

  2.   

    不如使用sendMessage或者postmessage函数来得方便。
      

  3.   

    你按多少次按钮就会设置多少次同一个定时器,这样没必要,而且不要用Sleep,这样会把主线程给挂起了,最好就是另外定义一个定时器,或用线程处理
      

  4.   

    我是写游戏模拟的。postmessage在那用不了 只能用keybd_event