//Keys.cs using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace Utilities { public static class Keys { const int WM_KEYDOWN = 0x100; const int WM_KEYUP = 0x101; [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lp1, string lp2); [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetForegroundWindow(IntPtr hWnd); [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true)] public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll", SetLastError = true)] static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); public static void SendToGame(string text) { Clipboard.SetText(text); IntPtr handle = FindWindow(null, "Guild Wars 2"); if (!handle.Equals(IntPtr.Zero)) { if (SetForegroundWindow(handle)) { SendEnter(handle); SendPaste(handle); SendEnter(handle); } } } static void SendEnter(IntPtr Handle) { //Enter Key Down PostMessage(Handle, WM_KEYDOWN, 0x0D, 0x101c0001); Thread.Sleep(100); //Enter Key Up PostMessage(Handle, WM_KEYUP, 0x0D, 0x101c0001); Thread.Sleep(100); } static void SendPaste(IntPtr Handle) { //Press and Hold Control keybd_event(0xA2, 0, 0, 0); //Send V PostMessage(Handle, WM_KEYDOWN, 0x56, 0x002f0001); Thread.Sleep(100); //Release Control keybd_event(0xA2, 0, 2, 0); Thread.Sleep(100); //Simulate backspace to remove the V from the paste :/ PostMessage(Handle, WM_KEYDOWN, 0x08, 0xe0001); Thread.Sleep(100); PostMessage(Handle, WM_KEYUP, 0x08, 0xe0001); } } } //Translation.cs using System; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.Script.Serialization; namespace Utilities { public static class Translator { public static string TranslateGoogle(string text, string fromCulture, string toCulture) { fromCulture = fromCulture.ToLower(); toCulture = toCulture.ToLower(); string[] tokens = fromCulture.Split('-'); if (tokens.Length > 1) fromCulture = tokens[0]; tokens = toCulture.Split('-'); if (tokens.Length > 1) toCulture = tokens[0]; string url = string.Format(@"http://translate.google.com/translate_a/t?client=j&text={0}&hl=en&sl={1}&tl={2}", HttpUtility.UrlEncode(text), fromCulture, toCulture); string html = null; try { WebClient web = new WebClient(); web.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0"); web.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8"); web.Encoding = Encoding.UTF8; html = web.DownloadString(url); } catch (Exception ex) { return null; } string result = Regex.Match(html, "trans\":(\".*?\"),\"", RegexOptions.IgnoreCase).Groups[1].Value; if (string.IsNullOrEmpty(result)) { return null; } JavaScriptSerializer ser = new JavaScriptSerializer(); return ser.Deserialize(result, typeof(string)) as string; } } } //MainWindow.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Translator { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { TranslationType.SelectionChanged+=TranslationType_SelectionChanged; } private void Button_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(TextToTranslate.Text)) return; var text = TextToTranslate.Text.Replace(".",""); var trans = TranslationType.SelectedIndex == 0 ? Utilities.Translator.TranslateGoogle(text, "en", "de") : Utilities.Translator.TranslateGoogle(text, "de", "en"); LastTranslation.Text = text + " : " + trans; if (SendToGame.IsChecked == true) { Utilities.Keys.SendToGame(trans); } } private void CloseButton_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void Grid_PreviewMouseDown_1(object sender, MouseButtonEventArgs e) { this.DragMove(); } private void TranslationType_SelectionChanged(object sender, SelectionChangedEventArgs e) { SendToGame.IsChecked = TranslationType.SelectedIndex == 0 ? true : false; } } }