Simulating Keyboard with SendInput API in DirectInput applications

前端 未结 2 2063
独厮守ぢ
独厮守ぢ 2020-11-28 09:07

I\'m trying to simulate keyboard commands for a custom game controller application. Because I\'ll need to simulate commands in a DirectInput environment most of the usual m

2条回答
  •  醉话见心
    2020-11-28 09:40

    I was trying to simulate keyboards for a presentation in Flash, and was also having the same problem. It did work for apps like Notepad, but not for flash. After hours of googling I finally made it work:

    public static void GenerateKey(int vk, bool bExtended)
        {
            INPUT[] inputs = new INPUT[1];
            inputs[0].type = INPUT_KEYBOARD;
    
            KEYBDINPUT  kb = new KEYBDINPUT(); //{0};
            // generate down 
            if ( bExtended )
                kb.dwFlags  = KEYEVENTF_EXTENDEDKEY;
    
            kb.wVk  = (ushort)vk;  
            inputs[0].ki = kb;
            SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0]));
    
            // generate up 
            //ZeroMemory(&kb, sizeof(KEYBDINPUT));
            //ZeroMemory(&inputs,sizeof(inputs));
            kb.dwFlags  =  KEYEVENTF_KEYUP;
            if ( bExtended )
                kb.dwFlags  |= KEYEVENTF_EXTENDEDKEY;
    
            kb.wVk    =  (ushort)vk;
            inputs[0].type =  INPUT_KEYBOARD;
            inputs[0].ki  =  kb;
            SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0]));
        }
    

提交回复
热议问题