c# - how to programatically mouse move,click,right click and keypress, etc. in winform and wpf? -
how programatically mouse move,click,right click , keypress etc in winform , wpf. please me code snippet if possible.
if understand question correctly want simulate input. in case sendinput
way go. this link
pinvoke sendinput – official way simulate input. pushes input through of expected code paths, , indistinguishable real input.
an easy way use inputsimulator @ codeplex. adding reference inputsimulator.dll can simulate keystrokes like
// tab inputsimulator.simulatekeydown(virtualkeycode.tab); // shift+tab inputsimulator.simulatemodifiedkeystroke(virtualkeycode.shift, virtualkeycode.tab); // etc.
however, inputsimulator doesn't support mouse yet here starter mousesimulator. leftclick , rightclick far of following 2 links extent mousemove etc.
mousesimulator
public class mousesimulator { [dllimport("user32.dll", setlasterror = true)] static extern uint sendinput(uint ninputs, ref input pinputs, int cbsize); [structlayout(layoutkind.sequential)] struct input { public sendinputeventtype type; public mousekeybdhardwareinputunion mkhi; } [structlayout(layoutkind.explicit)] struct mousekeybdhardwareinputunion { [fieldoffset(0)] public mouseinputdata mi; [fieldoffset(0)] public keybdinput ki; [fieldoffset(0)] public hardwareinput hi; } [structlayout(layoutkind.sequential)] struct keybdinput { public ushort wvk; public ushort wscan; public uint dwflags; public uint time; public intptr dwextrainfo; } [structlayout(layoutkind.sequential)] struct hardwareinput { public int umsg; public short wparaml; public short wparamh; } struct mouseinputdata { public int dx; public int dy; public uint mousedata; public mouseeventflags dwflags; public uint time; public intptr dwextrainfo; } [flags] enum mouseeventflags : uint { mouseeventf_move = 0x0001, mouseeventf_leftdown = 0x0002, mouseeventf_leftup = 0x0004, mouseeventf_rightdown = 0x0008, mouseeventf_rightup = 0x0010, mouseeventf_middledown = 0x0020, mouseeventf_middleup = 0x0040, mouseeventf_xdown = 0x0080, mouseeventf_xup = 0x0100, mouseeventf_wheel = 0x0800, mouseeventf_virtualdesk = 0x4000, mouseeventf_absolute = 0x8000 } enum sendinputeventtype : int { inputmouse, inputkeyboard, inputhardware } public static void clickleftmousebutton() { input mousedowninput = new input(); mousedowninput.type = sendinputeventtype.inputmouse; mousedowninput.mkhi.mi.dwflags = mouseeventflags.mouseeventf_leftdown; sendinput(1, ref mousedowninput, marshal.sizeof(new input())); input mouseupinput = new input(); mouseupinput.type = sendinputeventtype.inputmouse; mouseupinput.mkhi.mi.dwflags = mouseeventflags.mouseeventf_leftup; sendinput(1, ref mouseupinput, marshal.sizeof(new input())); } public static void clickrightmousebutton() { input mousedowninput = new input(); mousedowninput.type = sendinputeventtype.inputmouse; mousedowninput.mkhi.mi.dwflags = mouseeventflags.mouseeventf_rightdown; sendinput(1, ref mousedowninput, marshal.sizeof(new input())); input mouseupinput = new input(); mouseupinput.type = sendinputeventtype.inputmouse; mouseupinput.mkhi.mi.dwflags = mouseeventflags.mouseeventf_rightup; sendinput(1, ref mouseupinput, marshal.sizeof(new input())); } }
Comments
Post a Comment