C# hold key in a game application -


i'm trying make c# application, going control game. i'm trying example: hold key 150ms, hold left arrow 500ms , on. searching lot , found following code. program firstly target game , holding keys.

i'm holding keys way:  keyboard.holdkey(keys.left); thread.sleep(500); keyboard.releasekey(keys.left); 

here keyboard class:

public class keyboard  {     public keyboard()     {     }      [structlayout(layoutkind.explicit, size = 28)]     public struct input     {         [fieldoffset(0)]         public uint type;         [fieldoffset(4)]         public keyboardinput ki;     }      public struct keyboardinput     {         public ushort wvk;         public ushort wscan;         public uint dwflags;         public long time;         public uint dwextrainfo;     }      const int keyeventf_keyup = 0x0002;     const int input_keyboard = 1;      [dllimport("user32.dll")]     public static extern int sendinput(uint cinputs, ref input inputs, int cbsize);      [dllimport("user32.dll")]     static extern short getkeystate(int nvirtkey);      [dllimport("user32.dll")]     static extern ushort mapvirtualkey(int wcode, int wmaptype);       public static bool iskeydown(keys key)     {         return (getkeystate((int)key) & -128) == -128;     }      public static void holdkey(keys vk)     {         ushort nscan = mapvirtualkey((ushort)vk, 0);          input input = new input();         input.type = input_keyboard;         input.ki.wvk = (ushort)vk;         input.ki.wscan = nscan;         input.ki.dwflags = 0;         input.ki.time = 0;         input.ki.dwextrainfo = 0;         sendinput(1, ref input, marshal.sizeof(input)).tostring();     }      public static void releasekey(keys vk)     {         ushort nscan = mapvirtualkey((ushort)vk, 0);          input input = new input();         input.type = input_keyboard;         input.ki.wvk = (ushort)vk;         input.ki.wscan = nscan;         input.ki.dwflags = keyeventf_keyup;         input.ki.time = 0;         input.ki.dwextrainfo = 0;         sendinput(1, ref input, marshal.sizeof(input));     }      public static void presskey(keys vk)     {         holdkey(vk);         releasekey(vk);     } } 

and working in notepad/browser etc, not working in game, no matter fullscreen or window mode. can me figure out how can hold keys in full screen apps/games? thanks!

"hold key 150ms, hold left arrow 500ms"

see if works:

        keyboard.holdkey((byte)keys.a, 150);         keyboard.holdkey((byte)keys.left, 500); 

using:

public class keyboard {     [dllimport("user32.dll", setlasterror = true)]     static extern void keybd_event(byte bvk, byte bscan, int dwflags, int dwextrainfo);      const int key_down_event = 0x0001; //key down flag     const int key_up_event = 0x0002; //key flag      public static void holdkey(byte key, int duration)     {         int totalduration = 0;         while (totalduration < duration)         {             keybd_event(key, 0, key_down_event, 0);             keybd_event(key, 0, key_up_event, 0);             system.threading.thread.sleep(pausebetweenstrokes);             totalduration += pausebetweenstrokes;         }     } } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -