Gå til innhold

VB 2005, sendkeys problem...


Anbefalte innlegg

Videoannonse
Annonse
Hei!

 

Jeg skrev et lite program som brukte sendkeys til å sende cheatene til GTA: San andreas...

Men dette funker ikke jo ikke, har dette noe med directX og gjøre?

 

Svein. :)

5810885[/snapback]

 

Har du hørt om Freelancer, et spill der du er et romskip som skal gjøre diverse oppdrag i verdensrommet?

 

Uansett, så går jeg ut fra at dette også er directx, og her klarte jeg uten problemer å lage et program som skrev i chatten.

 

Har du husket å be programmet ditt om å trykke på knappen(e) som får opp boksen der du skal skrive inn kodene, viss det er en?

Lenke til kommentar

Det er noe greier med det å bruke vanlig SendKeys for å sende inn tastetrykk til spill. Alt etter hvordan spillene er laget så virker kanskje SendKeys, men for de fleste spill virker ikke dette og man må ned på et lavere nivå for å få sendt inn tastene (win32 api).

 

Den metoden jeg har funnet som virker omtrent på alt er

        [DllImport("user32", EntryPoint = "SendInput")]
       private static extern int SendInputK(int cInputs, ref KINPUT pInputs, int cbSize);

       [StructLayout(LayoutKind.Sequential)]
       private struct KEYBDINPUT {
           public Int16 wVk;
           public Int16 wScan;
           public Int32 dwFlags;
           public Int32 time;
           public Int32 dwExtraInfo;
           public Int32 __filler1;
           public Int32 __filler2;
       }

       [StructLayout(LayoutKind.Sequential)]
       private struct KINPUT {
           public Int16 dwType;
           public KEYBDINPUT ki;
       }

       private const int KEYEVENTF_KEYDOWN = 0x0000;
       private const int KEYEVENTF_KEYUP = 0x0002;

       private const short INPUT_KEYBOARD = 1;

       public static void KeyDown(Keys key) {
               KINPUT Down = new KINPUT();
               Down.ki.dwFlags = KEYEVENTF_KEYDOWN;
               Down.dwType = INPUT_KEYBOARD;
               Down.ki.wVk = (short)key;

               int geninplen = Marshal.SizeOf(typeof(KINPUT)); // Gen object size
               SendInputK(1, ref Down, geninplen);
       }


 

En del bakarbeid for å få'n til å funke.

Endret av wolf5
Lenke til kommentar

Glem forrige post. Det var den gamle koden. Virker en del, men har oppdaget at den sluttet å virke på maskinen min (virket på andre).

 

Her får du mange timers arbeid: :)

 

   public class SendInput {


       #region Declares
       [DllImport("user32.dll")]
       private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

       [StructLayout(LayoutKind.Sequential)]
       private struct KEYBDINPUT {
           public Int16 wVk;
           public Int16 wScan;
           public Int32 dwFlags;
           public Int32 time;
           public Int32 dwExtraInfo;
           public Int32 __filler1;
           public Int32 __filler2;
       }

       [StructLayout(LayoutKind.Sequential)]
       private struct KINPUT {
           public Int16 dwType;
           public KEYBDINPUT ki;
       }

       private const int KEYEVENTF_KEYDOWN = 0x0000;
       private const int KEYEVENTF_KEYUP = 0x0002;

       private const short INPUT_KEYBOARD = 1;

       #endregion

       #region KeyPress Sequences
       public static void KeyPress(string keys) {
           keys = keys.Replace("\\{", "<TEMP_TEMP>"); //Ignore "\{***}"'s. Only trigger on "{***}"
           //This function splits up normal and special keystrokes and calls a method for each type
           //string regex = @"[^\\]\{(.+?)\}"; //Finding special keystrokes
           string regex = @"\{(.+?)\}"; //Finding special keystrokes
           if (Regex.IsMatch(keys, regex)) {
               string[] str = Regex.Split(keys, regex);
               for (int i = 0; i < str.Length; i++) str[i] = str[i].Replace("<TEMP_TEMP>", "\\{");
               bool bNextIgnoreSpecial = false;
               if (str[0].EndsWith("\\")) {
                   str[0] = str[0].Substring(0, str[0].Length - 1);
                   bNextIgnoreSpecial = true;
               }
               KeyPressNormal(str[0], ""); //Do the first normal keys
               for (int i = 1; i < str.GetUpperBound(0); i += 2) {
                   //Every odd arrayitem is a special key
                   if (!bNextIgnoreSpecial) {
                       if (str[i] != "+" && str[i] != "%" && str[i] != "^") {
                           KeyPressSpecial(str[i]);
                           //Every even arrayitem is a normal key sequence

                           if (str[i + 1].EndsWith("\\")) {
                               str[i + 1] = str[i + 1].Substring(0, str[0].Length - 1);
                               bNextIgnoreSpecial = true;
                           }

                           KeyPressNormal(str[i + 1], "");
                       }
                       else {
                           //CTRL, SHIFT or ALT wanted before the next key
                           if (str[i + 1].EndsWith("\\")) {
                               str[i + 1] = str[i + 1].Substring(0, str[i + 1].Length - 1);
                               bNextIgnoreSpecial = true;
                           }

                           KeyPressNormal(str[i + 1], str[i]);
                       }


                   }
                   else {
                       KeyPressNormal("{" + str[i] + "}", "");
                       KeyPressNormal(str[i + 1], "");
                       bNextIgnoreSpecial = false;
                   }

               }
           }
           else {
               KeyPressNormal(keys, "");
           }
       }
       private static void KeyPressSpecial(string specialkey) {
           Keys SpecialKey = Keys.None;
           switch (specialkey.ToUpper()) {
               case "LBUTTON": SpecialKey = System.Windows.Forms.Keys.LButton; break;
               case "RBUTTON": SpecialKey = System.Windows.Forms.Keys.RButton; break;
               case "BREAK":
               case "CANCEL": SpecialKey = System.Windows.Forms.Keys.Cancel; break;
               case "MBUTTON": SpecialKey = System.Windows.Forms.Keys.MButton; break;
               case "BACKSPACE":
               case "BS":
               case "BKSP": SpecialKey = System.Windows.Forms.Keys.Back; break;
               case "TAB": SpecialKey = System.Windows.Forms.Keys.Tab; break;
               case "CLEAR": SpecialKey = System.Windows.Forms.Keys.Clear; break;
               case "~":
               case "ENTER": SpecialKey = System.Windows.Forms.Keys.Return; break;
               case "SHIFT": SpecialKey = System.Windows.Forms.Keys.ShiftKey; break;
               case "CONTROL": SpecialKey = System.Windows.Forms.Keys.ControlKey; break;
               case "MENU":
               case "ALT": SpecialKey = System.Windows.Forms.Keys.Menu; break;
               case "CAPSLOCK": SpecialKey = System.Windows.Forms.Keys.CapsLock; break;
               case "ESCAPE":
               case "ESC": SpecialKey = System.Windows.Forms.Keys.Escape; break;
               case "SPACE": SpecialKey = System.Windows.Forms.Keys.Space; break;
               case "PGUP": SpecialKey = System.Windows.Forms.Keys.PageUp; break;
               case "PGDN": SpecialKey = System.Windows.Forms.Keys.PageDown; break;
               case "END": SpecialKey = System.Windows.Forms.Keys.End; break;
               case "HOME": SpecialKey = System.Windows.Forms.Keys.Home; break;
               case "LEFT": SpecialKey = System.Windows.Forms.Keys.Left; break;
               case "UP": SpecialKey = System.Windows.Forms.Keys.Up; break;
               case "RIGHT": SpecialKey = System.Windows.Forms.Keys.Right; break;
               case "DOWN": SpecialKey = System.Windows.Forms.Keys.Down; break;
               case "SELECT": SpecialKey = System.Windows.Forms.Keys.Select; break;
               case "PRTSC": SpecialKey = System.Windows.Forms.Keys.PrintScreen; break;
               case "EXECUTE": SpecialKey = System.Windows.Forms.Keys.Execute; break;
               case "SNAPSHOT": SpecialKey = System.Windows.Forms.Keys.Snapshot; break;
               case "INSERT":
               case "INS": SpecialKey = System.Windows.Forms.Keys.Insert; break;
               case "DELETE":
               case "DEL": SpecialKey = System.Windows.Forms.Keys.Delete; break;
               case "HELP": SpecialKey = System.Windows.Forms.Keys.Help; break;
               case "NUMLOCK": SpecialKey = System.Windows.Forms.Keys.NumLock; break;
               case "SCROLLLOCK": SpecialKey = System.Windows.Forms.Keys.Scroll; break;
               case "NUMPAD0": SpecialKey = System.Windows.Forms.Keys.NumPad0; break;
               case "NUMPAD1": SpecialKey = System.Windows.Forms.Keys.NumPad1; break;
               case "NUMPAD2": SpecialKey = System.Windows.Forms.Keys.NumPad2; break;
               case "NUMPAD3": SpecialKey = System.Windows.Forms.Keys.NumPad3; break;
               case "NUMPAD4": SpecialKey = System.Windows.Forms.Keys.NumPad4; break;
               case "NUMPAD5": SpecialKey = System.Windows.Forms.Keys.NumPad5; break;
               case "NUMPAD6": SpecialKey = System.Windows.Forms.Keys.NumPad6; break;
               case "NUMPAD7": SpecialKey = System.Windows.Forms.Keys.NumPad7; break;
               case "NUMPAD8": SpecialKey = System.Windows.Forms.Keys.NumPad8; break;
               case "NUMPAD9": SpecialKey = System.Windows.Forms.Keys.NumPad9; break;
               case "MULTIPLY": SpecialKey = System.Windows.Forms.Keys.Multiply; break;
               case "ADD": SpecialKey = System.Windows.Forms.Keys.Add; break;
               case "SEPARATOR": SpecialKey = System.Windows.Forms.Keys.Separator; break;
               case "SUBTRACT": SpecialKey = System.Windows.Forms.Keys.Subtract; break;
               case "DECIMAL": SpecialKey = System.Windows.Forms.Keys.Decimal; break;
               case "DIVIDE": SpecialKey = System.Windows.Forms.Keys.Divide; break;
               case "F1": SpecialKey = System.Windows.Forms.Keys.F1; break;
               case "F2": SpecialKey = System.Windows.Forms.Keys.F2; break;
               case "F3": SpecialKey = System.Windows.Forms.Keys.F3; break;
               case "F4": SpecialKey = System.Windows.Forms.Keys.F4; break;
               case "F5": SpecialKey = System.Windows.Forms.Keys.F5; break;
               case "F6": SpecialKey = System.Windows.Forms.Keys.F6; break;
               case "F7": SpecialKey = System.Windows.Forms.Keys.F7; break;
               case "F8": SpecialKey = System.Windows.Forms.Keys.F8; break;
               case "F9": SpecialKey = System.Windows.Forms.Keys.F9; break;
               case "F10": SpecialKey = System.Windows.Forms.Keys.F10; break;
               case "F11": SpecialKey = System.Windows.Forms.Keys.F11; break;
               case "F12": SpecialKey = System.Windows.Forms.Keys.F12; break;
               case "F13": SpecialKey = System.Windows.Forms.Keys.F13; break;
               case "F14": SpecialKey = System.Windows.Forms.Keys.F14; break;
               case "F15": SpecialKey = System.Windows.Forms.Keys.F15; break;
               case "F16": SpecialKey = System.Windows.Forms.Keys.F16; break;
           }
           KeyPress(SpecialKey); //Send special key
           System.Threading.Thread.Sleep(10);
       }

       private static void KeyPressNormal(string keys, string ShiftCtrlAlt) {
           bool bShiftPressed = false;

           foreach (char c in keys) {
               VKType type = GetKeyCodeInfo(c.ToString());

               //Check if SHIFT, CTRL or ALT is explicitly wanted
               if (ShiftCtrlAlt == "+") type.Shift = true;
               if (ShiftCtrlAlt == "^") type.Control = true;
               if (ShiftCtrlAlt == "%") type.Alt = true;

               //Turn on shift, ctrl or alt
               if (type.Shift) KeyDown(Keys.ShiftKey);
               if (type.Control) KeyDown(Keys.ControlKey);
               if (type.Alt) KeyDown(Keys.Menu);

               KeyPress((Keys)type.VKCode); //Send key

               //Turn off shift, ctrl or alt
               if (type.Alt) KeyUp(Keys.Menu);
               if (type.Control) KeyUp(Keys.ControlKey);
               if (type.Shift) KeyUp(Keys.ShiftKey);

               ShiftCtrlAlt = ""; //Kun første tegn skal evt bruke denne
               System.Threading.Thread.Sleep(10);
           }

           //End shift key
           if (bShiftPressed) {
               KeyUp(Keys.ShiftKey);
               Console.WriteLine("SHIFT OFF");
           }
       }

       #endregion

       #region Win API stuff to get a table to get Keys-code from a character

       [DllImport("user32.dll")]
       static private extern int CharToOemA(byte[] lpszSrc, byte[] lpszDst);

       [DllImport("user32.dll")]
       static private extern int OemKeyScan(short wOemChar);

       [DllImport("user32.dll")]
       static private extern short VkKeyScanA(byte cChar);

       [DllImport("user32.dll")]
       private static extern int MapVirtualKeyA(int wCode, int wMapType);

       public struct VKType {
           public short VKCode;
           public short scanCode;
           public bool Control;
           public bool Shift;
           public bool Alt;
       }

       private static VKType[] asciiKeys = null;
       private static VKType[] virtualKeys = null;

       private static VKType[] AsciiKeys {
           get {
               if (asciiKeys == null) {
                   asciiKeys = new VKType[256];
                   short keyScan;
                   for (short iKey = 0; iKey < 256; iKey++) {
                       keyScan = VkKeyScanA((byte)iKey);
                       asciiKeys[iKey].VKCode = Convert.ToInt16(keyScan & 0xff); //low-byte of key scan code
                       asciiKeys[iKey].Shift = Convert.ToBoolean(keyScan & 0x100);
                       asciiKeys[iKey].Control = Convert.ToBoolean(keyScan & 0x200);
                       asciiKeys[iKey].Alt = Convert.ToBoolean(keyScan & 0x400);

                       // Get the ScanCode
                       string OEMChar = "  ";
                       string x = Chr(iKey).ToString();

                       byte[] a = System.Text.Encoding.ASCII.GetBytes(x);
                       byte[] b = System.Text.Encoding.ASCII.GetBytes(OEMChar);

                       CharToOemA(a, b);

                       OEMChar = System.Text.Encoding.ASCII.GetString(b);
                       short z = Asc(OEMChar);

                       asciiKeys[iKey].scanCode = Convert.ToInt16(OemKeyScan(z) & 0xff);
                       Console.WriteLine(asciiKeys[iKey].VKCode);

                   }
               }
               return asciiKeys;
           }
       }

       public static VKType[] VirtualKeys {
           get {
               if (virtualKeys == null) {
                   virtualKeys = new VKType[256];

                   for (short iKey = 0; iKey < 256; iKey++) {
                       virtualKeys[iKey].VKCode = iKey;

                       int ii = MapVirtualKeyA(iKey, 0);
                       virtualKeys[iKey].scanCode = Convert.ToInt16(ii);
                   }
               }
               return virtualKeys;
           }
       }

       public static VKType GetKeyCodeInfo(string key) {
           if (key.Length == 1) {
               return AsciiKeys[Asc(key)];
           }
           else {
               Console.WriteLine("Special codes: " + key);
           }
           return AsciiKeys[Asc(" ")];
       }



       private static short Asc(string ch) {
           return (short)System.Text.Encoding.Default.GetBytes(ch)[0];
       }

       private static char Chr(short i) {
           return Convert.ToChar(i);
       }

       #endregion

       #region KeyPress single key

       public static void KeyPress(Keys key) {
           KeyDown(key);
           System.Threading.Thread.Sleep(10);
           KeyUp(key);
       }


       public static void KeyDown(Keys key) {
           keybd_event((byte)VirtualKeys[(int)key].VKCode, (byte)VirtualKeys[(int)key].scanCode, KEYEVENTF_KEYDOWN, 0);
       }
       public static void KeyUp(Keys key) {
           keybd_event((byte)VirtualKeys[(int)key].VKCode, (byte)VirtualKeys[(int)key].scanCode, KEYEVENTF_KEYUP, 0);
       }


       #endregion

   }

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...