c# - using InputSimulator -


try use inputsimulator simulate keyboard inputs. works fine except using sim.keyboard.modifiedkeystroke simulate input of ascii character.

i tried simulate alt down + numpad1 + numpad4 + numpad7 + alt up using following 2 different ways:

    sim.keyboard.modifiedkeystroke(virtualkeycode.lmenu, new[] { virtualkeycode.numpad1, virtualkeycode.numpad4, virtualkeycode.numpad7}); 

and

    sim.keyboard.keydown(virtualkeycode.lmenu);     sim.keyboard.keydown(virtualkeycode.numpad1);     sim.keyboard.keyup(virtualkeycode.numpad1);     sim.keyboard.keydown(virtualkeycode.numpad4);     sim.keyboard.keyup(virtualkeycode.numpad4);     sim.keyboard.keydown(virtualkeycode.numpad7);     sim.keyboard.keyup(virtualkeycode.numpad7);     sim.keyboard.keyup(virtualkeycode.lmenu); 

neither works. try print out key status in console, real key press , simulated key press both give same result:

   lmenu key down    numpad1 key down    numpad1 key    numpad4 key down    numpad4 key    numpad7 key down    numpad7 key    lmenu key 

i think should problems library: issue 1. me please? there other way this?

update 1

i found "alt+tab" not working in win8. thought may same reason try fix first. turns out 2 different problems:

  1. to make "alt+tab" working, need set uiaccess=true in "app.manifest" , sign ".exe" file using test digital signature;

  2. simulating input of ascii characters still not working.

use keybd_event mapvirtualkey or if using sendinput add input's scan mapvirtualkey.

here example both,

using system; using system.threading; using system.runtime.interopservices;  namespace kbd_events_example {     static class program     {         #pragma warning disable 649         internal struct input         {             public uint32 type;             public keyboardmousehardware data;         }         [structlayout(layoutkind.explicit)]         //this keyboard-mouse-hardware union input won't work if remove mouse or hardware         internal struct keyboardmousehardware         {             [fieldoffset(0)]             public keybdinput keyboard;             [fieldoffset(0)]             public hardwareinput hardware;             [fieldoffset(0)]             public mouseinput mouse;         }         internal struct keybdinput         {             public uint16 vk;             public uint16 scan;             public uint32 flags;             public uint32 time;             public intptr extrainfo;         }         internal struct mouseinput         {             public int32 x;             public int32 y;             public uint32 mousedata;             public uint32 flags;             public uint32 time;             public intptr extrainfo;         }         internal struct hardwareinput         {             public uint32 msg;             public uint16 paraml;             public uint16 paramh;         }         #pragma warning restore 649         [dllimport("user32.dll", charset = charset.auto, exactspelling = true, callingconvention = callingconvention.winapi)]         static extern void keybd_event(byte bvk, byte bscan, uint dwflags, uint extrainfo);         [dllimport("user32.dll", setlasterror = true)]         static extern int mapvirtualkey(uint ucode, uint umaptype);         [dllimport("user32.dll", setlasterror = true)]         static extern uint32 sendinput(uint32 numberofinputs, input[] inputs, int32 sizeofinputstructure);         enum vk         {             menu = 0x12,             numpad0 = 0x60,             numpad1 = 0x61,             numpad2 = 0x62,             numpad3 = 0x63,             numpad4 = 0x64,             numpad5 = 0x65,             numpad6 = 0x66,             numpad7 = 0x67,             numpad8 = 0x68,             numpad9 = 0x69         }         const uint keyeventf_keyup = 0x0002;         public const int input_keyboard = 1;         [stathread]         static void main()         {             thread.sleep(5000); //wait 5 seconds, can focus notepad             keybd_event((int)vk.menu, (byte)mapvirtualkey((uint)vk.menu, 0), 0, 0); //alt press               keybd_event((int)vk.numpad1, (byte)mapvirtualkey((uint)vk.numpad1, 0), 0, 0); // n1 press               keybd_event((int)vk.numpad1, (byte)mapvirtualkey((uint)vk.numpad1, 0), keyeventf_keyup, 0); // n1 release               keybd_event((int)vk.menu, (byte)mapvirtualkey((uint)vk.menu, 0), keyeventf_keyup, 0); // alt release                 thread.sleep(2000); //wait 2 seconds             input[] inputs = new input[] {                 new input {type = input_keyboard, data = new keyboardmousehardware { keyboard = new keybdinput { vk = (ushort)vk.menu, flags = 0, scan = (ushort)mapvirtualkey((uint)vk.menu, 0), extrainfo = intptr.zero, time = 0}}},                 new input {type = input_keyboard, data = new keyboardmousehardware { keyboard = new keybdinput { vk = (ushort)vk.numpad2, flags = 0, scan = (ushort)mapvirtualkey((uint)vk.numpad2, 0), extrainfo = intptr.zero, time = 0}}},                 new input {type = input_keyboard, data = new keyboardmousehardware { keyboard = new keybdinput { vk = (ushort)vk.numpad2, flags = 2, scan = (ushort)mapvirtualkey((uint)vk.numpad2, 0), extrainfo = intptr.zero, time = 0}}},                 new input {type = input_keyboard, data = new keyboardmousehardware { keyboard = new keybdinput { vk = (ushort)vk.menu, flags = 2, scan = (ushort)mapvirtualkey((uint)vk.menu, 0), extrainfo = intptr.zero, time = 0}}}             };             sendinput((uint)inputs.length, inputs, marshal.sizeof(typeof(input)));         }     } } 

as inputsimulator, need write own class using sendinput or keybd_event mapvirtualkey handle inputs...


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -